diff --git a/index.mjs b/index.mjs index 768b53f626..ee5b575171 100644 --- a/index.mjs +++ b/index.mjs @@ -5,6 +5,6 @@ export { afterUpdate, setContext, getContext, - nextTick, + tick, createEventDispatcher } from './internal'; diff --git a/src/internal/scheduler.js b/src/internal/scheduler.js index 4d9f9ad388..6c62af905c 100644 --- a/src/internal/scheduler.js +++ b/src/internal/scheduler.js @@ -4,14 +4,14 @@ import { set_current_component } from './lifecycle.js'; export let dirty_components = []; export const intros = { enabled: false }; -let update_scheduled = false; +let update_promise; const binding_callbacks = []; const render_callbacks = []; export function schedule_update() { - if (!update_scheduled) { - update_scheduled = true; - queue_microtask(flush); + if (!update_promise) { + update_promise = Promise.resolve(); + update_promise.then(flush); } } @@ -19,9 +19,9 @@ export function add_render_callback(fn) { render_callbacks.push(fn); } -export function nextTick(fn) { - add_render_callback(fn); +export function tick() { schedule_update(); + return update_promise; } export function add_binding_callback(fn) { @@ -56,7 +56,7 @@ export function flush() { } } while (dirty_components.length); - update_scheduled = false; + update_promise = null; } function update($$) { @@ -68,10 +68,4 @@ function update($$) { $$.after_render.forEach(add_render_callback); } -} - -function queue_microtask(callback) { - Promise.resolve().then(() => { - if (update_scheduled) callback(); - }); } \ No newline at end of file diff --git a/test/runtime/samples/lifecycle-next-tick/main.html b/test/runtime/samples/lifecycle-next-tick/main.html index f7a889d433..0c577c20e8 100644 --- a/test/runtime/samples/lifecycle-next-tick/main.html +++ b/test/runtime/samples/lifecycle-next-tick/main.html @@ -1,5 +1,5 @@