Skip to content

Commit 987538a

Browse files
committed
feat(core): add hasListener to event hooks
1 parent 52ba092 commit 987538a

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

packages/core/src/types/hooks.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { EventHook, EventHookOn, EventHookTrigger } from '@vueuse/core'
1+
import type { EventHookOn, EventHookTrigger } from '@vueuse/core'
22
import type { D3ZoomEvent } from 'd3-zoom'
33
import type { GraphEdge } from './edge'
44
import type { GraphNode } from './node'
@@ -7,6 +7,7 @@ import type { ViewportTransform } from './zoom'
77
import type { EdgeChange, NodeChange } from './changes'
88
import type { VueFlowStore } from './store'
99
import type { VueFlowError } from '~/utils/errors'
10+
import type { EventHookExtended } from '~/utils'
1011

1112
export type MouseTouchEvent = MouseEvent | TouchEvent
1213

@@ -94,7 +95,7 @@ export interface FlowEvents {
9495
}
9596

9697
export type FlowHooks = Readonly<{
97-
[key in keyof FlowEvents]: EventHook<FlowEvents[key]>
98+
[key in keyof FlowEvents]: EventHookExtended<FlowEvents[key]>
9899
}>
99100

100101
export type FlowHooksOn = Readonly<{
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { Ref } from 'vue'
2-
import { ref } from 'vue'
31
import type { EventHook } from '@vueuse/core'
42
import { tryOnScopeDispose } from '@vueuse/core'
53

@@ -9,26 +7,28 @@ import { tryOnScopeDispose } from '@vueuse/core'
97
* Modified to be able to check if there are any event listeners
108
*/
119
export interface EventHookExtended<T> extends EventHook<T> {
12-
fns: Ref<Set<(param: T) => void>>
10+
hasListeners: () => boolean
1311
}
1412

1513
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
1717

1818
if (defaultHandler) {
19-
fns.value.add(defaultHandler)
19+
fns.add(defaultHandler)
2020
}
2121

2222
const off = (fn: (param: T) => void) => {
23-
fns.value.delete(fn)
23+
fns.delete(fn)
2424
}
2525

2626
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)
2929
}
3030

31-
fns.value.add(fn)
31+
fns.add(fn)
3232
const offFn = () => off(fn)
3333

3434
tryOnScopeDispose(offFn)
@@ -39,13 +39,13 @@ export function createExtendedEventHook<T = any>(defaultHandler: (param: T) => v
3939
}
4040

4141
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)))
4343
}
4444

4545
return {
4646
on,
4747
off,
4848
trigger,
49-
fns,
49+
hasListeners,
5050
}
5151
}

0 commit comments

Comments
 (0)