1
- import type { Ref } from 'vue'
2
- import { ref } from 'vue'
3
1
import type { EventHook } from '@vueuse/core'
4
2
import { tryOnScopeDispose } from '@vueuse/core'
5
3
@@ -9,26 +7,28 @@ import { tryOnScopeDispose } from '@vueuse/core'
9
7
* Modified to be able to check if there are any event listeners
10
8
*/
11
9
export interface EventHookExtended < T > extends EventHook < T > {
12
- fns : Ref < Set < ( param : T ) => void > >
10
+ hasListeners : ( ) => boolean
13
11
}
14
12
15
13
export function createExtendedEventHook < T = any > ( defaultHandler : ( param : T ) => void = ( ) => { } ) : EventHookExtended < T > {
16
- const fns = ref ( new Set < ( param : T ) => void > ( ) )
14
+ const fns = new Set < ( param : T ) => void > ( )
15
+
16
+ const hasListeners = ( ) => fns . size > 0
17
17
18
18
if ( defaultHandler ) {
19
- fns . value . add ( defaultHandler )
19
+ fns . add ( defaultHandler )
20
20
}
21
21
22
22
const off = ( fn : ( param : T ) => void ) => {
23
- fns . value . delete ( fn )
23
+ fns . delete ( fn )
24
24
}
25
25
26
26
const on = ( fn : ( param : T ) => void ) => {
27
- if ( fns . value . has ( defaultHandler ) ) {
28
- fns . value . delete ( defaultHandler )
27
+ if ( fns . has ( defaultHandler ) ) {
28
+ fns . delete ( defaultHandler )
29
29
}
30
30
31
- fns . value . add ( fn )
31
+ fns . add ( fn )
32
32
const offFn = ( ) => off ( fn )
33
33
34
34
tryOnScopeDispose ( offFn )
@@ -39,13 +39,13 @@ export function createExtendedEventHook<T = any>(defaultHandler: (param: T) => v
39
39
}
40
40
41
41
const trigger = ( param : T ) => {
42
- return Promise . all ( Array . from ( fns . value ) . map ( ( fn ) => fn ( param ) ) )
42
+ return Promise . all ( Array . from ( fns ) . map ( ( fn ) => fn ( param ) ) )
43
43
}
44
44
45
45
return {
46
46
on,
47
47
off,
48
48
trigger,
49
- fns ,
49
+ hasListeners ,
50
50
}
51
51
}
0 commit comments