mirror of
https://github.com/zoriya/v10.git
synced 2026-08-16 02:45:09 +00:00
build(spf): build26 from 45504a2b
This commit is contained in:
Vendored
+41
@@ -0,0 +1,41 @@
|
||||
import { Signal } from "signal-polyfill";
|
||||
//#region src/core/signals/effect.ts
|
||||
const pending = /* @__PURE__ */ new Set();
|
||||
const watcher = new Signal.subtle.Watcher(() => {
|
||||
queueMicrotask(runPending);
|
||||
});
|
||||
function runPending() {
|
||||
for (const c of watcher.getPending()) pending.add(c);
|
||||
watcher.watch();
|
||||
for (const c of pending) {
|
||||
pending.delete(c);
|
||||
c.get();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Run a side effect whenever its signal dependencies change.
|
||||
*
|
||||
* Executes immediately (synchronous initial run), then re-runs on the next
|
||||
* microtask after any dependency changes. If the callback returns a function,
|
||||
* it is called before each re-run and when the effect is stopped — the same
|
||||
* cleanup contract as Preact Signals, Maverick Signals, and Svelte 5 $effect.
|
||||
*
|
||||
* Returns a cleanup function that stops the effect.
|
||||
*/
|
||||
function effect(fn) {
|
||||
let cleanup;
|
||||
const c = new Signal.Computed(() => {
|
||||
if (typeof cleanup === "function") cleanup();
|
||||
cleanup = fn();
|
||||
});
|
||||
watcher.watch(c);
|
||||
c.get();
|
||||
return () => {
|
||||
watcher.unwatch(c);
|
||||
if (typeof cleanup === "function") cleanup();
|
||||
};
|
||||
}
|
||||
//#endregion
|
||||
export { effect };
|
||||
|
||||
//# sourceMappingURL=effect.js.map
|
||||
Reference in New Issue
Block a user