-
Notifications
You must be signed in to change notification settings - Fork 2
/
signal.js
48 lines (40 loc) · 1.13 KB
/
signal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// @preact/signals compatible wrapper for signal standard
import { Signal } from 'signal-polyfill'
const { untrack, Watcher } = Signal.subtle
export const signal = v => wrap(new Signal.State(v))
export const computed = fn => wrap(new Signal.Computed(fn))
export { untrack as untracked }
const wrap = (s) => {
let get = () => s.get()
Object.defineProperty(s, 'value', {
get,
set(v) { s.set(v) }
})
s.peek = () => untrack(get)
s.toJSON = s.then = s.toString = s.valueOf = get
return s
}
let pending = false;
let watcher = new Watcher(() => {
if (!pending) {
pending = true;
queueMicrotask(() => {
pending = false;
for (const signal of watcher.getPending()) {
signal.get();
watcher.watch(signal);
}
});
}
});
/**
* ⚠️ WARNING: Nothing unwatches ⚠️
* This will produce a memory leak.
*/
export function effect(cb) {
let destructor;
let c = new Signal.Computed(() => { typeof destructor === 'function' && destructor(); destructor = cb(); });
watcher.watch(c);
c.get();
return () => { typeof destructor === 'function' && destructor(); watcher.unwatch(c) };
}