From dce01441fcf672127ece262ffcb08d8e2fac29df Mon Sep 17 00:00:00 2001 From: Levente Balogh Date: Tue, 30 May 2023 10:15:47 +0200 Subject: [PATCH] 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 --- packages/grafana-runtime/src/services/EchoSrv.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/grafana-runtime/src/services/EchoSrv.ts b/packages/grafana-runtime/src/services/EchoSrv.ts index 3bfa36576de..2804574af85 100644 --- a/packages/grafana-runtime/src/services/EchoSrv.ts +++ b/packages/grafana-runtime/src/services/EchoSrv.ts @@ -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> = []; + buffer: Array<{ event: Omit; meta?: {} | undefined }> = []; flush(): void { - this.events = []; + this.buffer = []; } addBackend(backend: EchoBackend): void {} addEvent(event: Omit, meta?: {} | undefined): void { - this.events.push(event); + this.buffer.push({ event, meta }); } }