File tree Expand file tree Collapse file tree 2 files changed +27
-2
lines changed
packages/shared/createEventHook Expand file tree Collapse file tree 2 files changed +27
-2
lines changed Original file line number Diff line number Diff line change @@ -55,6 +55,31 @@ describe('createEventHook', () => {
55
55
expect ( listener ) . toHaveBeenCalledTimes ( 2 )
56
56
} )
57
57
58
+ it ( 'should await trigger' , async ( ) => {
59
+ let message = ''
60
+
61
+ const myFunction = ( ) => {
62
+ const resultEvent = createEventHook < string > ( )
63
+ const exec = ( ) => resultEvent . trigger ( 'Hello World' )
64
+ return {
65
+ exec,
66
+ onResult : resultEvent . on ,
67
+ }
68
+ }
69
+
70
+ const { exec, onResult } = myFunction ( )
71
+ onResult ( result => new Promise < number > ( ( resolve ) => {
72
+ setTimeout ( ( ) => {
73
+ message = result
74
+ resolve ( 2 )
75
+ } , 100 )
76
+ } ) )
77
+ const result = await exec ( )
78
+
79
+ expect ( message ) . toBe ( 'Hello World' )
80
+ expect ( result ) . toEqual ( [ 2 ] )
81
+ } )
82
+
58
83
it ( 'the same listener should fire only once' , ( ) => {
59
84
const listener = vitest . fn ( )
60
85
const { on, trigger, off } = createEventHook < string > ( )
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ import { tryOnScopeDispose } from '../tryOnScopeDispose'
6
6
7
7
export type EventHookOn < T = any > = ( fn : ( param : T ) => void ) => { off : ( ) => void }
8
8
export type EventHookOff < T = any > = ( fn : ( param : T ) => void ) => void
9
- export type EventHookTrigger < T = any > = ( param : T ) => void
9
+ export type EventHookTrigger < T = any > = ( param : T ) => Promise < unknown [ ] >
10
10
11
11
export interface EventHook < T = any > {
12
12
on : EventHookOn < T >
@@ -38,7 +38,7 @@ export function createEventHook<T = any>(): EventHook<T> {
38
38
}
39
39
40
40
const trigger = ( param : T ) => {
41
- fns . forEach ( fn => fn ( param ) )
41
+ return Promise . all ( fns . map ( fn => fn ( param ) ) )
42
42
}
43
43
44
44
return {
You can’t perform that action at this time.
0 commit comments