From 5a91d00f52093ebf0d7b951ae6083e29d8286e88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 25 Sep 2023 10:35:30 +0200 Subject: [PATCH] EventBus: Fix error in ScopedEventBus (#75349) --- .../grafana-data/src/events/EventBus.test.ts | 29 +++++++++++++++---- packages/grafana-data/src/events/EventBus.ts | 2 +- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/grafana-data/src/events/EventBus.test.ts b/packages/grafana-data/src/events/EventBus.test.ts index 72716fa7eef..6de48b5d54e 100644 --- a/packages/grafana-data/src/events/EventBus.test.ts +++ b/packages/grafana-data/src/events/EventBus.test.ts @@ -47,7 +47,7 @@ describe('EventBus', () => { expect(events.length).toBe(1); }); - describe('EventBusWithSource', () => { + describe('ScopedEventBus', () => { it('can add sources to the source path', () => { const bus = new EventBusSrv(); const busWithSource = bus.newScopedBus('foo'); @@ -56,15 +56,34 @@ describe('EventBus', () => { it('adds the source to the event payload', () => { const bus = new EventBusSrv(); - let events: BusEvent[] = []; + const events: BusEvent[] = []; bus.subscribe(DataHoverEvent, (event) => events.push(event)); - const busWithSource = bus.newScopedBus('foo'); - busWithSource.publish({ type: DataHoverEvent.type }); + const scopedBus = bus.newScopedBus('foo'); + scopedBus.publish({ type: DataHoverEvent.type }); expect(events.length).toEqual(1); - expect(events[0].origin).toEqual(busWithSource); + expect(events[0].origin).toEqual(scopedBus); + }); + + it('Can subscribe to only local events', () => { + const bus = new EventBusSrv(); + const allEvents: BusEvent[] = []; + const scopedEvents: BusEvent[] = []; + + bus.subscribe(DataHoverEvent, (event) => allEvents.push(event)); + + const scopedBus1 = bus.newScopedBus('foo', { onlyLocal: true }); + const scopedBus2 = bus.newScopedBus('foo', { onlyLocal: true }); + + scopedBus1.subscribe(DataHoverEvent, (event) => scopedEvents.push(event)); + + scopedBus1.publish({ type: DataHoverEvent.type }); + scopedBus2.publish({ type: DataHoverEvent.type }); + + expect(allEvents.length).toEqual(2); + expect(scopedEvents.length).toEqual(1); }); }); diff --git a/packages/grafana-data/src/events/EventBus.ts b/packages/grafana-data/src/events/EventBus.ts index a842d567f4b..b155319ecc1 100644 --- a/packages/grafana-data/src/events/EventBus.ts +++ b/packages/grafana-data/src/events/EventBus.ts @@ -136,7 +136,7 @@ class ScopedEventBus implements EventBus { } getStream(eventType: BusEventType): Observable { - return this.eventBus.getStream(eventType).pipe(filter(this.filter)); + return this.eventBus.getStream(eventType).pipe(filter(this.filter.bind(this))); } // syntax sugar