Skip to content

Commit

Permalink
fix(createEventHook): trigger should not ignore falsy values (#3561)
Browse files Browse the repository at this point in the history
Co-authored-by: 丶远方 <yangpanteng@gmail.com>
  • Loading branch information
romansp and Alfred-Skyblue committed Dec 4, 2023
1 parent 4b15938 commit 3733b8e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
19 changes: 19 additions & 0 deletions packages/shared/createEventHook/index.test.ts
Expand Up @@ -39,6 +39,25 @@ describe('createEventHook', () => {
expect(timesFired).toBe(2)
})

it('should trigger event and pass falsy values', () => {
let timesFired = 0

type Falsy = false | 0 | '' | null | undefined
const { on: onResult, trigger } = createEventHook<Falsy>()

const values: Falsy[] = [false, 0, '', null, undefined]
const results: Falsy[] = []
onResult((value: Falsy) => {
timesFired++
results.push(value)
})
for (const value of values)
trigger(value)

expect(timesFired).toBe(values.length)
expect(results).toMatchObject(values)
})

it('should add and remove event listener', () => {
const listener = vi.fn()
const { on, off, trigger } = createEventHook<string>()
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/createEventHook/index.ts
Expand Up @@ -48,8 +48,8 @@ export function createEventHook<T = any>(): EventHook<T> {
}
}

const trigger: EventHookTrigger<T> = (param?: T) => {
return Promise.all(Array.from(fns).map(fn => param ? fn(param) : (fn as Callback<void>)()))
const trigger: EventHookTrigger<T> = (...args) => {
return Promise.all(Array.from(fns).map(fn => fn(...(args as [T]))))
}

return {
Expand Down

0 comments on commit 3733b8e

Please sign in to comment.