Skip to content

Commit e8d5bbf

Browse files
committed
chore: tweaks
1 parent 392d736 commit e8d5bbf

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

packages/runtime-vapor/__tests__/bench/domBinding.bench.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,17 +283,19 @@ describe('DOM binding effects', () => {
283283
'setEvent',
284284
createDiv,
285285
(el, source) => {
286+
const invoker = createInvoker(noop)
286287
renderEffect(() =>
287-
on(el, source.value & 1 ? 'click' : 'mouseover', createInvoker(noop), {
288+
on(el, source.value & 1 ? 'click' : 'mouseover', invoker, {
288289
effect: true,
289290
}),
290291
)
291292
},
292293
(el, source) => {
294+
const invoker = createInvoker(noop)
293295
setEventBinding(
294296
el,
295297
() => (source.value & 1 ? 'click' : 'mouseover'),
296-
createInvoker(noop),
298+
invoker,
297299
)
298300
},
299301
)

packages/runtime-vapor/__tests__/renderEffect.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,24 @@ describe('renderEffect', () => {
442442
expect(calls).toEqual(['mouseover'])
443443
})
444444

445+
test('setEventBinding does not mutate listener options', () => {
446+
const options = { once: true }
447+
const button = document.createElement('button')
448+
const scope = new EffectScope()
449+
450+
scope.run(() => {
451+
setEventBinding(
452+
button,
453+
() => 'click',
454+
() => {},
455+
options,
456+
)
457+
})
458+
scope.stop()
459+
460+
expect(options).toEqual({ once: true })
461+
})
462+
445463
test('should run with the scheduling order', async () => {
446464
const calls: string[] = []
447465

packages/runtime-vapor/src/dom/bindingEffect.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919

2020
type TextNodeWithCache = Text & { $txt?: string }
2121
type EventBindingOptions = AddEventListenerOptions & { effect: true }
22+
const eventBindingOptions: EventBindingOptions = { effect: true }
2223
type MergedDynamicPropsSource = Record<string, any> | null | undefined
2324
type DynamicPropsGetter = () => MergedDynamicPropsSource
2425

@@ -491,8 +492,9 @@ export function setEventBinding(
491492
handler: (e: Event) => any | ((e: Event) => any)[],
492493
options?: AddEventListenerOptions,
493494
): void {
494-
const effectOptions = (options || {}) as EventBindingOptions
495-
effectOptions.effect = true
495+
const effectOptions: EventBindingOptions = options
496+
? { ...options, effect: true }
497+
: eventBindingOptions
496498
if (inOnceSlot) {
497499
on(el, getter(), handler, effectOptions)
498500
return

0 commit comments

Comments
 (0)