-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
145 lines (113 loc) · 2.94 KB
/
index.ts
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import {Callback, Effect, Owner, SignalNode} from './types';
const UNOWNED: Owner = {
owner: null,
owned: []
};
const runningEffects: Effect[] = [];
let Updates: Effect[];
const getRunningEffect = () => runningEffects[runningEffects.length - 1];
const addToOwner = (owner: Owner, effect: Effect) => {
owner.owned ||= [];
owner.owned.push(effect);
}
const flushUpdates = () => {
let flushedIndex = 0;
while (Updates.length !== 0 && flushedIndex !== Updates.length) {
let effect = Updates[flushedIndex];
if (effect) {
runEffect(effect);
flushedIndex++;
}
}
Updates = undefined;
};
const runUpdates = (fn: Callback) => {
if (Updates) {
return fn();
}
Updates = [];
fn();
flushUpdates();
};
const runEffect = (effect: Effect) => {
cleanup(effect);
runningEffects.push(effect);
try {
runUpdates(effect.fn);
} finally {
runningEffects.pop();
}
};
function trackEffectDependency(effect: Effect, signal: SignalNode) {
signal.observers.add(effect);
effect.dependencies.add(signal);
}
export function createSignal<T>(value?: T): [
() => T,
(value: T) => void
] {
const signal: SignalNode = {
current: value,
observers: new Set()
};
const read = (): T => {
const effect = getRunningEffect();
if (effect) trackEffectDependency(effect, signal);
return signal.current as T;
};
const write = (nextValue: T) => {
signal.current = nextValue;
if (signal.observers.size) {
runUpdates(() => {
for (const observer of [...signal.observers]) {
if (!observer.state) {
observer.state = 'pending'
Updates.push(observer);
}
}
})
}
};
return [read, write];
}
export function cleanup(effect: Effect) {
for (const dep of effect.dependencies) {
dep.observers.delete(effect);
}
if (effect.owned) {
effect.owned?.forEach((ownedEffect) => {
cleanup(ownedEffect);
});
effect.owned = [];
}
delete effect.state;
effect.dependencies.clear();
}
export function createEffect(fn: Callback) {
const owner = getRunningEffect();
const effect: Effect = {
run: () => runEffect(effect),
dependencies: new Set(),
fn,
owner
};
if (owner) {
addToOwner(owner, effect);
}
runEffect(effect);
}
export function createMemo(fn: Callback) {
const [memoReadOnlySignal, set] = createSignal();
createEffect(() => set(fn()));
return memoReadOnlySignal;
}
export const untrack = (fn: Callback) => {
let runningEffect = runningEffects.pop();
fn();
if (runningEffect) {
runningEffects.push(runningEffect)
}
}
export const batch = (fn: Callback) => {
runUpdates(fn);
};