EchoSrv: Capture early events (#67977)

* feat: catch any events that were reported before the EchoSrv got initialised

* fix: add events to the new echo service

* chore: update comment

* refactor: use `instanceof` to check if it is a FakeEchoSrv
This commit is contained in:
Levente Balogh 2023-05-30 10:15:47 +02:00 committed by GitHub
parent 1ff5170c83
commit dce01441fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -120,6 +120,13 @@ let singletonInstance: EchoSrv;
* @internal
*/
export function setEchoSrv(instance: EchoSrv) {
// Check if there were any events reported to the FakeEchoSrv (before the main EchoSrv was initialized), and track them
if (singletonInstance instanceof FakeEchoSrv) {
for (const item of singletonInstance.buffer) {
instance.addEvent(item.event, item.meta);
}
}
singletonInstance = instance;
}
@ -148,15 +155,15 @@ export const registerEchoBackend = (backend: EchoBackend) => {
};
export class FakeEchoSrv implements EchoSrv {
events: Array<Omit<EchoEvent, 'meta'>> = [];
buffer: Array<{ event: Omit<EchoEvent, 'meta'>; meta?: {} | undefined }> = [];
flush(): void {
this.events = [];
this.buffer = [];
}
addBackend(backend: EchoBackend): void {}
addEvent<T extends EchoEvent>(event: Omit<T, 'meta'>, meta?: {} | undefined): void {
this.events.push(event);
this.buffer.push({ event, meta });
}
}