Merge pull request #2030 from sveltejs/next-tick-promise

Return promise from nextTick
pull/2043/head
Rich Harris 7 years ago committed by GitHub
commit 5876f0b45b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,6 +5,6 @@ export {
afterUpdate,
setContext,
getContext,
nextTick,
tick,
createEventDispatcher
} from './internal';

@ -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();
});
}

@ -1,5 +1,5 @@
<script>
import { nextTick } from 'svelte';
import { tick } from 'svelte';
export let snapshots = [];
@ -14,7 +14,7 @@
function log() {
snapshots.push(`before ${buttons[0].textContent}`);
nextTick(() => {
tick().then(() => {
snapshots.push(`after ${buttons[0].textContent}`);
});
}

Loading…
Cancel
Save