-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.tsx
More file actions
203 lines (167 loc) · 5.51 KB
/
index.tsx
File metadata and controls
203 lines (167 loc) · 5.51 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import {
useCallback,
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
} from 'react';
import type { Dispatch, MutableRefObject, SetStateAction } from 'react';
import ReactDOM from 'react-dom';
type Getter<T> = () => T;
type Setter<T> = Dispatch<SetStateAction<T>>;
type Flow = {
nextUnsubMap: Map<MutableRefObject<unknown>, () => void>;
willUnsubMap: Map<MutableRefObject<unknown>, () => void>;
runFlowFn: (isInit?: 'isInit') => void;
};
type Props = {
path?: string[];
mapFn?: (...args: unknown[]) => unknown;
};
const isSSR = typeof window === 'undefined';
const useIsomorphicLayoutEffect = isSSR ? useEffect : useLayoutEffect;
const batch = ReactDOM.unstable_batchedUpdates || ((fn: () => void) => fn());
const isPrimitive = (data: unknown) => typeof data !== 'object';
let curFlow: Flow | null = null;
const uniqueFlows = new Set();
const runValFlows = (valFlows: Set<Flow>) => {
batch(() => {
// A's valFlows: [flow1, flow2]
// B's valFlows: [flow1]
valFlows.forEach((flow) => {
if (!uniqueFlows.has(flow)) {
uniqueFlows.add(flow); // use uniqueFlows to only run flow once
// flow1.nextUnsubMap -> { A: unsubFlow1FromA, B: unsubFlow1FromB }
// flow1.nextUnsubMap is set in prev `flow1.runFlowFn()` cycle
flow.willUnsubMap = flow.nextUnsubMap;
flow.nextUnsubMap = new Map();
// run below `flow1.runFlowFn()`
// if access A, means A is still used in flow, so needs to keep A
// remove A from willUnsubMap, willUnsubMap -> { B: unsubB }, A will keep, only unsub B
flow.runFlowFn();
// flow1.willUnsubMap's size, will only reduce, no increase
// if accessed both A & B, flow1.willUnsubMap -> {}, unsub nothing
flow.willUnsubMap.forEach((unsubFlowFromVal) => unsubFlowFromVal());
}
});
});
setTimeout(() => uniqueFlows.clear());
};
export function useSignal<T>(initialValue: T): [Getter<T>, Setter<T>] {
const valRef = useRef<T>(initialValue);
const setters = useRef<Setter<T>[]>([]);
const valFlows = useRef<Set<Flow>>(new Set());
const valUpdated = useRef(false);
const View = ({ path, mapFn }: Props) => {
const [proVal, setProVal] = useState(valRef.current);
useIsomorphicLayoutEffect(() => {
setters.current.push(setProVal);
return () => {
const index = setters.current.indexOf(setProVal);
setters.current.splice(index, 1);
};
}, []);
useIsomorphicLayoutEffect(() => {
if (valUpdated.current) {
valUpdated.current = false;
runValFlows(valFlows.current);
}
});
if (Array.isArray(path)) {
const realVal = path.reduce((obj: any, key: string) => obj[key], proVal);
if (mapFn) return realVal.map(mapFn);
return realVal;
}
return proVal;
};
const getDeep = useCallback((obj: any, path: string[]): T => {
return new Proxy(obj, {
get: (target, key: string) => {
const val = target[key];
if (key === 'map' && val === Array.prototype.map) {
return (mapFn: Props['mapFn']) => <View path={path} mapFn={mapFn} />;
}
path.push(key);
if (isPrimitive(val)) {
return <View path={path} />;
}
return getDeep(val, path);
},
}) as T;
}, []);
return useMemo(() => {
valFlows.current.clear();
return [
() => {
try {
// in `runFlowFn()` has access several value getters,
// if access current signal's value, will reach here
if (curFlow) {
const flow = curFlow;
valFlows.current.add(flow);
flow.nextUnsubMap.set(valRef, () => valFlows.current.delete(flow)); // use for next `runValFlows()`
flow.willUnsubMap.delete(valRef); // delete value from map, so will not unsub value
return valRef.current;
}
// eslint-disable-next-line react-hooks/rules-of-hooks
useState();
if (isPrimitive(valRef.current)) {
return (<View />) as T;
}
return getDeep(valRef.current, []);
} catch (e) {
return valRef.current;
}
},
(v: SetStateAction<T>) => {
valRef.current = v instanceof Function ? v(valRef.current) : v;
if (setters.current.length > 0) {
batch(() => setters.current.forEach((set) => set(valRef.current)));
valUpdated.current = true;
} else {
runValFlows(valFlows.current);
}
},
];
}, [getDeep]);
}
export function useUpdate(fn: () => void) {
const flowRef = useRef({
nextUnsubMap: new Map(),
willUnsubMap: new Map(),
runFlowFn: () => {
curFlow = flowRef.current;
fn();
curFlow = null;
},
});
useIsomorphicLayoutEffect(() => {
flowRef.current.runFlowFn();
}, []);
}
export function useAuto<T>(fn: Getter<T>): Getter<T> {
const flowRef = useRef({
nextUnsubMap: new Map(),
willUnsubMap: new Map(),
runFlowFn: (isInit?: 'isInit') => {
curFlow = flowRef.current;
const val = fn();
if (!isInit) setAutoVal(val);
curFlow = null;
return val;
},
});
const initialVal = useMemo(() => flowRef.current.runFlowFn('isInit'), []);
const [autoVal, setAutoVal] = useSignal<T>(initialVal);
return autoVal;
}
export function useMount(fn: () => void) {
useIsomorphicLayoutEffect(() => fn(), []);
}
export function useCleanup(fn: () => void) {
useIsomorphicLayoutEffect(() => () => fn(), []);
}
export function Run<T>(fn: Getter<T>): T {
return useAuto(fn)();
}