0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00

Merge pull request #1988 from sveltejs/gh-1976

add nextTick lifecycle function - fixes #1976
This commit is contained in:
Rich Harris 2019-01-19 08:16:52 -05:00 committed by GitHub
commit c134ca2ee4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 8 deletions

View File

@ -3,5 +3,6 @@ export {
onDestroy,
beforeUpdate,
afterUpdate,
nextTick,
createEventDispatcher
} from './internal';

View File

@ -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;

View File

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

View File

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

View File

@ -0,0 +1,24 @@
<script>
import { nextTick } from 'svelte';
export let snapshots = [];
let count = 0;
let buttons = [];
function increment() {
count += 1;
log();
}
function log() {
snapshots.push(`before ${buttons[0].textContent}`);
nextTick(() => {
snapshots.push(`after ${buttons[0].textContent}`);
});
}
</script>
<button bind:this={buttons[0]} on:click={increment}>{count}</button>
<button bind:this={buttons[1]} on:click={log}>{count}</button>