perf: Reuse the same promise instance in the scheduler

- Scheduling an update no longer allocates two new promises, only one via `then`.
- The `tick` method was really returning an empty, resolved promise, so just reusing the same promise there as well. It was a little confusing because it _looked_ like it would return a promise to when the flush had completed, but in fact it was returning a promise that would resolve before the flush completed, as it was the promise that was "thenned" for the flush.
pull/2555/head
Ben Lesh 6 years ago
parent 93f7ecca1d
commit 1519a96b52

@ -4,21 +4,22 @@ import { set_current_component } from './lifecycle.js';
export const dirty_components = []; export const dirty_components = [];
export const intros = { enabled: false }; export const intros = { enabled: false };
let update_promise; const resolved_promise = Promise.resolve();
let update_scheduled = false;
const binding_callbacks = []; const binding_callbacks = [];
const render_callbacks = []; const render_callbacks = [];
const flush_callbacks = []; const flush_callbacks = [];
export function schedule_update() { export function schedule_update() {
if (!update_promise) { if (!update_scheduled) {
update_promise = Promise.resolve(); update_scheduled = true;
update_promise.then(flush); resolved_promise.then(flush);
} }
} }
export function tick() { export function tick() {
schedule_update(); schedule_update();
return update_promise; return resolved_promise;
} }
export function add_binding_callback(fn) { export function add_binding_callback(fn) {
@ -65,7 +66,7 @@ export function flush() {
flush_callbacks.pop()(); flush_callbacks.pop()();
} }
update_promise = null; update_scheduled = false;
} }
function update($$) { function update($$) {
@ -77,4 +78,4 @@ function update($$) {
$$.after_render.forEach(add_render_callback); $$.after_render.forEach(add_render_callback);
} }
} }

Loading…
Cancel
Save