Skip to content

Commit

Permalink
feat(config): expose config.useEventDelegation and default to false
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 10, 2019
1 parent 02f897a commit 3be1c5d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type Config = {
warnHandler: ?(msg: string, vm: Component, trace: string) => void;
ignoredElements: Array<string | RegExp>;
keyCodes: { [key: string]: number | Array<number> };
useEventDelegation: boolean;

// platform
isReservedTag: (x?: string) => boolean;
Expand Down Expand Up @@ -83,6 +84,15 @@ export default ({
// $flow-disable-line
keyCodes: Object.create(null),

/**
* Use event delegation - this works around a few async edge cases caused by
* microtask / DOM event racing conditions, and should in theory save some
* memory.
*
* Off by default for backwards compatibility.
*/
useEventDelegation: false,

/**
* Check if a tag is reserved so that it cannot be registered as a
* component. This is platform-dependent and may be overwritten.
Expand Down
10 changes: 8 additions & 2 deletions src/platforms/web/runtime/modules/events.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* @flow */

import config from 'core/config'
import { isDef, isUndef } from 'shared/util'
import { updateListeners } from 'core/vdom/helpers/index'
import { isIE, isPhantomJS, supportsPassive } from 'core/util/index'
Expand Down Expand Up @@ -50,7 +51,12 @@ function add (
capture: boolean,
passive: boolean
) {
if (!capture && !passive && delegateRE.test(name)) {
if (
!capture &&
!passive &&
config.useEventDelegation &&
delegateRE.test(name)
) {
const count = eventCounts[name]
let store = target.__events
if (!count) {
Expand Down Expand Up @@ -150,7 +156,7 @@ function remove (
_target?: HTMLElement
) {
const el: any = _target || target
if (!capture && delegateRE.test(name)) {
if (!capture && config.useEventDelegation && delegateRE.test(name)) {
el.__events[name] = null
if (--eventCounts[name] === 0) {
removeGlobalHandler(name)
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/specs/async-edge-cases.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<script src="../../../dist/vue.min.js"></script>
</head>
<body>
<script>
// this is necessary to make these cases pass
Vue.config.useEventDelegation = true
</script>

<!-- #4510 click and change event on checkbox -->
<div id="case-1">
Expand Down
1 change: 1 addition & 0 deletions types/test/vue-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class Test extends Vue {
config.keyCodes = { esc: 27 };
config.ignoredElements = ['foo', /^ion-/];
config.async = false
config.useEventDelegation = true
}

static testMethods() {
Expand Down
1 change: 1 addition & 0 deletions types/vue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export interface VueConfiguration {
warnHandler(msg: string, vm: Vue, trace: string): void;
ignoredElements: (string | RegExp)[];
keyCodes: { [key: string]: number | number[] };
useEventDelegation: boolean;
async: boolean;
}

Expand Down

0 comments on commit 3be1c5d

Please sign in to comment.