summaryrefslogtreecommitdiff
path: root/node_modules/svelte/src/runtime/internal/loop.js
diff options
context:
space:
mode:
authorrxliuli <rxliuli@gmail.com>2025-11-04 05:03:50 +0800
committerrxliuli <rxliuli@gmail.com>2025-11-04 05:03:50 +0800
commitbce557cc2dc767628bed6aac87301a1be7c5431b (patch)
treeb51a051228d01fe3306cd7626d4a96768aadb944 /node_modules/svelte/src/runtime/internal/loop.js
init commit
Diffstat (limited to 'node_modules/svelte/src/runtime/internal/loop.js')
-rw-r--r--node_modules/svelte/src/runtime/internal/loop.js45
1 files changed, 45 insertions, 0 deletions
diff --git a/node_modules/svelte/src/runtime/internal/loop.js b/node_modules/svelte/src/runtime/internal/loop.js
new file mode 100644
index 0000000..5a14565
--- /dev/null
+++ b/node_modules/svelte/src/runtime/internal/loop.js
@@ -0,0 +1,45 @@
+import { raf } from './environment.js';
+
+const tasks = new Set();
+
+/**
+ * @param {number} now
+ * @returns {void}
+ */
+function run_tasks(now) {
+ tasks.forEach((task) => {
+ if (!task.c(now)) {
+ tasks.delete(task);
+ task.f();
+ }
+ });
+ if (tasks.size !== 0) raf(run_tasks);
+}
+
+/**
+ * For testing purposes only!
+ * @returns {void}
+ */
+export function clear_loops() {
+ tasks.clear();
+}
+
+/**
+ * Creates a new task that runs on each raf frame
+ * until it returns a falsy value or is aborted
+ * @param {import('./private.js').TaskCallback} callback
+ * @returns {import('./private.js').Task}
+ */
+export function loop(callback) {
+ /** @type {import('./private.js').TaskEntry} */
+ let task;
+ if (tasks.size === 0) raf(run_tasks);
+ return {
+ promise: new Promise((fulfill) => {
+ tasks.add((task = { c: callback, f: fulfill }));
+ }),
+ abort() {
+ tasks.delete(task);
+ }
+ };
+}