|
1 | | -import { hyphenate, isArray } from '@vue/shared' |
| 1 | +import { hyphenate, isArray, isString, isFunction } from '@vue/shared' |
2 | 2 | import { |
3 | 3 | ErrorCodes, |
4 | 4 | ComponentInternalInstance, |
5 | | - callWithAsyncErrorHandling |
| 5 | + callWithAsyncErrorHandling, warn |
6 | 6 | } from '@vue/runtime-core' |
7 | 7 |
|
8 | 8 | interface Invoker extends EventListener { |
@@ -81,7 +81,7 @@ const getNow = () => |
81 | 81 | cachedNow || (p.then(() => (cachedNow = 0)), (cachedNow = Date.now())) |
82 | 82 |
|
83 | 83 | function createInvoker( |
84 | | - initialValue: EventValue, |
| 84 | + initialValue: EventValue | unknown, |
85 | 85 | instance: ComponentInternalInstance | null |
86 | 86 | ) { |
87 | 87 | const invoker: Invoker = (e: Event & { _vts?: number }) => { |
@@ -109,23 +109,34 @@ function createInvoker( |
109 | 109 | [e] |
110 | 110 | ) |
111 | 111 | } |
112 | | - invoker.value = initialValue |
| 112 | + invoker.value = sanitizeEventValue(initialValue) |
113 | 113 | invoker.attached = getNow() |
114 | 114 | return invoker |
115 | 115 | } |
116 | 116 |
|
| 117 | +function sanitizeEventValue(value: unknown): EventValue { |
| 118 | + if (isFunction(value) || isArray(value)) { |
| 119 | + return value as EventValue |
| 120 | + } |
| 121 | + |
| 122 | + if (__DEV__) { |
| 123 | + warn('Wrong type passed to the event invoker, did you maybe forget @ or : in front of your prop? Received ' + (isString(value) ? value : typeof value)) |
| 124 | + } |
| 125 | + return () => {} |
| 126 | +} |
| 127 | + |
117 | 128 | function patchStopImmediatePropagation( |
118 | 129 | e: Event, |
119 | | - value: EventValue |
| 130 | + value: EventValue | unknown |
120 | 131 | ): EventValue { |
121 | 132 | if (isArray(value)) { |
122 | 133 | const originalStop = e.stopImmediatePropagation |
123 | 134 | e.stopImmediatePropagation = () => { |
124 | 135 | originalStop.call(e) |
125 | 136 | ;(e as any)._stopped = true |
126 | 137 | } |
127 | | - return value.map(fn => (e: Event) => !(e as any)._stopped && fn && fn(e)) |
| 138 | + return (value as Function[]).map(fn => (e: Event) => !(e as any)._stopped && fn && fn(e)) |
128 | 139 | } else { |
129 | | - return value |
| 140 | + return sanitizeEventValue(value) |
130 | 141 | } |
131 | 142 | } |
0 commit comments