From c2660518116122e6e6710210153551139dc2be47 Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Fri, 18 Jan 2019 21:06:24 -0500 Subject: [PATCH] rename afterFlush to nextTick, schedule update if necessary --- index.mjs | 2 +- src/internal/Component.js | 5 ++-- src/internal/scheduler.js | 15 ++++++---- .../samples/lifecycle-after-flush/_config.js | 20 ------------- .../samples/lifecycle-after-flush/main.html | 19 ------------ .../samples/lifecycle-next-tick/_config.js | 30 +++++++++++++++++++ .../samples/lifecycle-next-tick/main.html | 24 +++++++++++++++ 7 files changed, 67 insertions(+), 48 deletions(-) delete mode 100644 test/runtime/samples/lifecycle-after-flush/_config.js delete mode 100644 test/runtime/samples/lifecycle-after-flush/main.html create mode 100644 test/runtime/samples/lifecycle-next-tick/_config.js create mode 100644 test/runtime/samples/lifecycle-next-tick/main.html diff --git a/index.mjs b/index.mjs index 55f227749e..1ba5890537 100644 --- a/index.mjs +++ b/index.mjs @@ -3,6 +3,6 @@ export { onDestroy, beforeUpdate, afterUpdate, - add_render_callback as afterFlush, + nextTick, createEventDispatcher } from './internal'; diff --git a/src/internal/Component.js b/src/internal/Component.js index 8df1d7aee5..4eac8d57af 100644 --- a/src/internal/Component.js +++ b/src/internal/Component.js @@ -1,4 +1,4 @@ -import { add_render_callback, flush, intros, schedule_update } from './scheduler.js'; +import { add_render_callback, flush, intros, schedule_update, dirty_components } from './scheduler.js'; import { current_component, set_current_component } from './lifecycle.js' import { is_function, run, run_all, noop } from './utils.js'; import { blankObject } from './utils.js'; @@ -46,7 +46,8 @@ function destroy(component, detach) { function make_dirty(component, key) { if (!component.$$.dirty) { - schedule_update(component); + dirty_components.push(component); + schedule_update(); component.$$.dirty = {}; } component.$$.dirty[key] = true; diff --git a/src/internal/scheduler.js b/src/internal/scheduler.js index 962c321fd6..0626db2a97 100644 --- a/src/internal/scheduler.js +++ b/src/internal/scheduler.js @@ -1,15 +1,13 @@ import { run_all } from './utils.js'; -let update_scheduled = false; +export let dirty_components = []; +export const intros = { enabled: false }; -let dirty_components = []; +let update_scheduled = false; const binding_callbacks = []; const render_callbacks = []; -export const intros = { enabled: false }; - -export function schedule_update(component) { - dirty_components.push(component); +export function schedule_update() { if (!update_scheduled) { update_scheduled = true; queue_microtask(flush); @@ -20,6 +18,11 @@ export function add_render_callback(fn) { render_callbacks.push(fn); } +export function nextTick(fn) { + add_render_callback(fn); + schedule_update(); +} + export function add_binding_callback(fn) { binding_callbacks.push(fn); } diff --git a/test/runtime/samples/lifecycle-after-flush/_config.js b/test/runtime/samples/lifecycle-after-flush/_config.js deleted file mode 100644 index 222dd26286..0000000000 --- a/test/runtime/samples/lifecycle-after-flush/_config.js +++ /dev/null @@ -1,20 +0,0 @@ -export default { - async test({ assert, component, target, window }) { - const button = target.querySelector('button'); - const click = new window.MouseEvent('click'); - - await button.dispatchEvent(click); - assert.deepEqual(component.snapshots, [ - 'before 0', - 'after 1' - ]); - - await button.dispatchEvent(click); - assert.deepEqual(component.snapshots, [ - 'before 0', - 'after 1', - 'before 1', - 'after 2' - ]); - } -}; diff --git a/test/runtime/samples/lifecycle-after-flush/main.html b/test/runtime/samples/lifecycle-after-flush/main.html deleted file mode 100644 index 0e6b76d0f3..0000000000 --- a/test/runtime/samples/lifecycle-after-flush/main.html +++ /dev/null @@ -1,19 +0,0 @@ - - - \ No newline at end of file diff --git a/test/runtime/samples/lifecycle-next-tick/_config.js b/test/runtime/samples/lifecycle-next-tick/_config.js new file mode 100644 index 0000000000..35a66f7a09 --- /dev/null +++ b/test/runtime/samples/lifecycle-next-tick/_config.js @@ -0,0 +1,30 @@ +export default { + async test({ assert, component, target, window }) { + const buttons = target.querySelectorAll('button'); + const click = new window.MouseEvent('click'); + + await buttons[0].dispatchEvent(click); + assert.deepEqual(component.snapshots, [ + 'before 0', + 'after 1' + ]); + + await buttons[0].dispatchEvent(click); + assert.deepEqual(component.snapshots, [ + 'before 0', + 'after 1', + 'before 1', + 'after 2' + ]); + + await buttons[1].dispatchEvent(click); + assert.deepEqual(component.snapshots, [ + 'before 0', + 'after 1', + 'before 1', + 'after 2', + 'before 2', + 'after 2' + ]); + } +}; diff --git a/test/runtime/samples/lifecycle-next-tick/main.html b/test/runtime/samples/lifecycle-next-tick/main.html new file mode 100644 index 0000000000..f7a889d433 --- /dev/null +++ b/test/runtime/samples/lifecycle-next-tick/main.html @@ -0,0 +1,24 @@ + + + + \ No newline at end of file