From b3c821672399957bd3a776912811762817b31f82 Mon Sep 17 00:00:00 2001 From: Bastien Granger Date: Wed, 20 Jul 2022 15:05:03 -0400 Subject: [PATCH] fix(events): omit ignored events in event bus creation --- src/Events.ts | 33 +++++++++++++------------------ src/index.ts | 3 +-- test/Event.spec.ts | 49 +++++++++++++++++++++------------------------- 3 files changed, 37 insertions(+), 48 deletions(-) diff --git a/src/Events.ts b/src/Events.ts index dd1eded..bf05cdf 100644 --- a/src/Events.ts +++ b/src/Events.ts @@ -34,23 +34,14 @@ export function combineEvents< return Object.assign({}, ...args) } -export function omitEvents< - Events extends EventDeclaration, - OmittedEvents extends keyof Events ->(events: Events, omittedEvents: OmittedEvents[]): Omit { - return Object.keys(events).reduce((acc, event) => { - if (!omittedEvents.includes(event as OmittedEvents)) { - acc[event as keyof Events] = events[event] - } - return acc - }, {} as any) -} - -export function createEventBus(args: { - events: C - channels?: Channel[] - ignoredEvents?: Array -}): C { +export function createEventBus< + C extends EventDeclaration, + T extends Array +>(args: { + events: C; + channels?: Channel[]; + ignoredEvents?: T; +}): Omit { const transports = (args.channels || []).map( (c) => new Transport(c, (args.ignoredEvents as string[])) @@ -58,7 +49,11 @@ export function createEventBus(args: { const eventBus: Partial = {} for (const event in args.events) { - if (args.events.hasOwnProperty(event)) { + if ( + args.events.hasOwnProperty(event) && + (!args.ignoredEvents || + (args.ignoredEvents && !args.ignoredEvents.includes(event))) + ) { eventBus[event] = connectSlot( event, transports, @@ -67,5 +62,5 @@ export function createEventBus(args: { } } - return eventBus as C + return eventBus as Omit } diff --git a/src/index.ts b/src/index.ts index b6e779f..9777a82 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,8 +2,7 @@ export { slot, Slot } from './Slot' export { EventDeclaration, combineEvents, - createEventBus, - omitEvents + createEventBus } from './Events' export { Channel } from './Channel' export { GenericChannel } from './Channels/GenericChannel' diff --git a/test/Event.spec.ts b/test/Event.spec.ts index 8e56381..912212f 100644 --- a/test/Event.spec.ts +++ b/test/Event.spec.ts @@ -41,36 +41,11 @@ describe('combineEvents()', () => { }) -describe('omitEvent()', () => { - it('should omit events from the the list', () => { - const events = { - hello: slot<{ name: string }>(), - how: slot<{ mode: 'simple' | 'advanced' }>(), - are: slot<{ tense: number }>(), - you: slot<{ reflective: boolean }>(), - } - - const filteredList = omitEvents(events, ['hello']) - expect(Object.keys(filteredList)).toEqual(['how', 'are', 'you']) - }) - - it('should return the list of all event when omitted list is empty', () => { - const events = { - hello: slot<{ name: string }>(), - how: slot<{ mode: 'simple' | 'advanced' }>(), - are: slot<{ tense: number }>(), - you: slot<{ reflective: boolean }>(), - } - - const filteredList = omitEvents(events, []) - expect(Object.keys(filteredList)).toEqual(Object.keys(events)) - }) -}) - describe('createEventBus()', () => { const events = { - numberToString: slot() + numberToString: slot(), + eventToIgnore: slot() } const param = DEFAULT_PARAM @@ -127,4 +102,24 @@ describe('createEventBus()', () => { data: '5' }) }) + + describe('when a ignored list is passed to createEventBus()', () => { + it('should not connect the ignored slot and should return a filtered eventBus', () => { + const channel = new TestChannel() + const eventBus = createEventBus({ + events, + channels: [channel], + ignoredEvents: ['eventToIgnore'] + }) + channel.callConnected() + const isIncluded = Object.keys(eventBus).includes('eventToIgnore') + expect(isIncluded).toBe(false) + // An event_list message should have been received with the list of + // ignoredEvents + expect(channel.sendSpy).toHaveBeenCalledWith({ + type: 'event_list', + ignoredEvents: ['eventToIgnore'] + }) + }) + }) })