diff --git a/src/runtime/internal/keyed_each.ts b/src/runtime/internal/keyed_each.ts index b7991d2f9f..88db365ebe 100644 --- a/src/runtime/internal/keyed_each.ts +++ b/src/runtime/internal/keyed_each.ts @@ -47,7 +47,7 @@ export const update_keyed_each = ( const insert = (block) => { transition_in(block, 1); - block.m(node, next, lookup.has(block.key)); + block.m(node, next); lookup.set(block.key, block); next = block.first; n--; diff --git a/src/runtime/internal/loop.ts b/src/runtime/internal/loop.ts index 95abd518fa..b4c1c895af 100644 --- a/src/runtime/internal/loop.ts +++ b/src/runtime/internal/loop.ts @@ -118,5 +118,40 @@ export const onEachFrame = ( }; /** tests only */ -export const clear_loops = () => - void (next_frame.length = running_frame.length = timed_tasks.length = pending_insert_timed.length = n = i = j = +(running_timed = pending_inserts = false)); +export const clear_loops = () => { + next_frame.length = running_frame.length = timed_tasks.length = pending_insert_timed.length = n = i = j = +(running_timed = pending_inserts = false); + tasks.clear(); +}; + +/** legacy loop for svelte/motion */ + +export interface MotionTask { abort(): void; promise: Promise } +type MotionTaskCallback = (now: number) => boolean | void; +type MotionTaskEntry = { c: MotionTaskCallback; f: () => void }; + +const tasks = new Set(); + +function run_tasks(now: number) { + tasks.forEach(task => { + if (!task.c(now)) { + tasks.delete(task); + task.f(); + } + }); + + if (tasks.size !== 0) raf(run_tasks); +} +export function motion_loop(callback: MotionTaskCallback): MotionTask { + let task: MotionTaskEntry; + + if (tasks.size === 0) raf(run_tasks); + + return { + promise: new Promise(fulfill => { + tasks.add(task = { c: callback, f: fulfill }); + }), + abort() { + tasks.delete(task); + } + }; +} \ No newline at end of file diff --git a/src/runtime/motion/spring.ts b/src/runtime/motion/spring.ts index 5845a13a57..d78926aadb 100644 --- a/src/runtime/motion/spring.ts +++ b/src/runtime/motion/spring.ts @@ -1,5 +1,5 @@ import { Readable, writable } from 'svelte/store'; -import { loop, now, Task } from 'svelte/internal'; +import { motion_loop as loop, now, MotionTask as Task } from 'svelte/internal'; import { is_date } from './utils'; interface TickContext { diff --git a/src/runtime/motion/tweened.ts b/src/runtime/motion/tweened.ts index c802604c0e..0c4cf55d81 100644 --- a/src/runtime/motion/tweened.ts +++ b/src/runtime/motion/tweened.ts @@ -1,5 +1,5 @@ import { Readable, writable } from 'svelte/store'; -import { assign, loop, now, Task } from 'svelte/internal'; +import { assign, motion_loop as loop, now, MotionTask as Task } from 'svelte/internal'; import { linear } from 'svelte/easing'; import { is_date } from './utils';