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, afterUpdate,
setContext, setContext,
getContext, getContext,
nextTick, tick,
createEventDispatcher createEventDispatcher
} from './internal'; } from './internal';

@ -4,14 +4,14 @@ import { set_current_component } from './lifecycle.js';
export let dirty_components = []; export let dirty_components = [];
export const intros = { enabled: false }; export const intros = { enabled: false };
let update_scheduled = false; let update_promise;
const binding_callbacks = []; const binding_callbacks = [];
const render_callbacks = []; const render_callbacks = [];
export function schedule_update() { export function schedule_update() {
if (!update_scheduled) { if (!update_promise) {
update_scheduled = true; update_promise = Promise.resolve();
queue_microtask(flush); update_promise.then(flush);
} }
} }
@ -19,9 +19,9 @@ export function add_render_callback(fn) {
render_callbacks.push(fn); render_callbacks.push(fn);
} }
export function nextTick(fn) { export function tick() {
add_render_callback(fn);
schedule_update(); schedule_update();
return update_promise;
} }
export function add_binding_callback(fn) { export function add_binding_callback(fn) {
@ -56,7 +56,7 @@ export function flush() {
} }
} while (dirty_components.length); } while (dirty_components.length);
update_scheduled = false; update_promise = null;
} }
function update($$) { function update($$) {
@ -68,10 +68,4 @@ function update($$) {
$$.after_render.forEach(add_render_callback); $$.after_render.forEach(add_render_callback);
} }
}
function queue_microtask(callback) {
Promise.resolve().then(() => {
if (update_scheduled) callback();
});
} }

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

Loading…
Cancel
Save