Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use microtick outside of events #3879

Merged
merged 1 commit into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { assign } from './util';
import { diff, commitRoot } from './diff/index';
import options from './options';
import { Fragment } from './create-element';
import { inEvent } from './diff/props';

/**
* Base Component class. Provides `setState()` and `forceUpdate()`, which
Expand Down Expand Up @@ -183,6 +184,18 @@ let rerenderQueue = [];

let prevDebounce;

const microTick =
typeof Promise == 'function'
? Promise.prototype.then.bind(Promise.resolve())
: setTimeout;
function defer(cb) {
if (inEvent) {
setTimeout(cb);
} else {
microTick(cb);
}
}

/**
* Enqueue a rerender of a component
* @param {import('./internal').Component} c The component to rerender
Expand All @@ -196,7 +209,7 @@ export function enqueueRender(c) {
prevDebounce !== options.debounceRendering
) {
prevDebounce = options.debounceRendering;
(prevDebounce || setTimeout)(process);
(prevDebounce || defer)(process);
}
}

Expand Down
18 changes: 16 additions & 2 deletions src/diff/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,29 @@ export function setProperty(dom, name, value, oldValue, isSvg) {
}
}

export let inEvent = false;

/**
* Proxy an event to hooked event handlers
* @param {Event} e The event object from the browser
* @private
*/
function eventProxy(e) {
return this._listeners[e.type + false](options.event ? options.event(e) : e);
inEvent = true;
try {
return this._listeners[e.type + false](
options.event ? options.event(e) : e
);
} finally {
inEvent = false;
}
}

function eventProxyCapture(e) {
return this._listeners[e.type + true](options.event ? options.event(e) : e);
inEvent = true;
try {
return this._listeners[e.type + true](options.event ? options.event(e) : e);
} finally {
inEvent = false;
}
}