Skip to content

Commit

Permalink
fix(events): omit ignored events in event bus creation
Browse files Browse the repository at this point in the history
  • Loading branch information
bastienGranger committed Jul 22, 2022
1 parent 2b86a22 commit b3c8216
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 48 deletions.
33 changes: 14 additions & 19 deletions src/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,26 @@ export function combineEvents<
return Object.assign({}, ...args)
}

export function omitEvents<
Events extends EventDeclaration,
OmittedEvents extends keyof Events
>(events: Events, omittedEvents: OmittedEvents[]): Omit<Events, OmittedEvents> {
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<C extends EventDeclaration>(args: {
events: C
channels?: Channel[]
ignoredEvents?: Array<keyof C>
}): C {
export function createEventBus<
C extends EventDeclaration,
T extends Array<keyof C>
>(args: {
events: C;
channels?: Channel[];
ignoredEvents?: T;
}): Omit<C, T[number]> {

const transports = (args.channels || []).map(
(c) => new Transport(c, (args.ignoredEvents as string[]))
)

const eventBus: Partial<C> = {}
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,
Expand All @@ -67,5 +62,5 @@ export function createEventBus<C extends EventDeclaration>(args: {
}
}

return eventBus as C
return eventBus as Omit<C, T[number]>
}
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
49 changes: 22 additions & 27 deletions test/Event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number, string>()
numberToString: slot<number, string>(),
eventToIgnore: slot<void, void>()
}

const param = DEFAULT_PARAM
Expand Down Expand Up @@ -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']
})
})
})
})

0 comments on commit b3c8216

Please sign in to comment.