EventBus: Fix error in ScopedEventBus (#75349)

This commit is contained in:
Torkel Ödegaard 2023-09-25 10:35:30 +02:00 committed by GitHub
parent f4de29dd87
commit 5a91d00f52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 6 deletions

View File

@ -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);
});
});

View File

@ -136,7 +136,7 @@ class ScopedEventBus implements EventBus {
}
getStream<T extends BusEvent>(eventType: BusEventType<T>): Observable<T> {
return this.eventBus.getStream(eventType).pipe(filter(this.filter));
return this.eventBus.getStream(eventType).pipe(filter(this.filter.bind(this)));
}
// syntax sugar