-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathtracking.ts
More file actions
186 lines (152 loc) · 4.58 KB
/
tracking.ts
File metadata and controls
186 lines (152 loc) · 4.58 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
import { use, useState, type Context } from "react";
const LOCK = Symbol("react-native-css-lock");
const IS_EQUAL = Symbol("react-native-css-is-equal");
interface IsEqualObject {
[IS_EQUAL]: (other: unknown) => boolean;
}
export type GetFunction = <T>(sig: Signal<T>) => T;
type Subscriber = () => void;
export interface Signal<T> {
_get(): T;
_subs: Set<Subscriber>;
set(value: T): void;
subscribe(fn: Subscriber): () => void;
}
function cleanupSubscriptions(subscriptions: Set<() => void>): void {
for (const dispose of subscriptions) {
dispose();
}
subscriptions.clear();
}
/**
* A custom built watcher for the library
*/
export function useDeepWatcher<
T extends object,
Configs extends object,
Props extends object,
Variables extends object,
Containers extends object,
>(
fn: (get: GetFunction, ...args: [Configs, Props, Variables, Containers]) => T,
deps: [
Configs,
Props | undefined | null,
Context<Variables>,
Context<Containers>,
],
) {
const [state, setState] = useState(() => {
const subscriptions = new Set<Subscriber>();
const configs = deps[0];
const props = makeAccessTreeProxy(deps[1] ?? ({} as Props));
const lazyVariables = makeLazyContext(deps[2]);
const variables = makeAccessTreeProxy(lazyVariables);
const lazyContainers = makeLazyContext(deps[3]);
const containers = makeAccessTreeProxy(lazyContainers);
const get: GetFunction = (signal) => {
const dispose = signal.subscribe(() => {
cleanupSubscriptions(subscriptions);
lazyVariables[LOCK] = true;
lazyContainers[LOCK] = true;
setState((s) => ({
...s,
value: fn(get, configs, props, variables, containers),
}));
});
subscriptions.add(dispose);
return signal._get();
};
const value = fn(get, configs, props, variables, containers);
return {
value,
subscriptions,
deps: [configs, props, variables, containers],
};
});
if (
state.deps.some((dep, index) => {
return !(dep as IsEqualObject)[IS_EQUAL](deps[index]);
})
) {
setState((s) => {
const subscriptions = s.subscriptions;
cleanupSubscriptions(subscriptions);
const configs = deps[0];
const props = makeAccessTreeProxy(deps[1] ?? ({} as Props));
const lazyVariables = makeLazyContext(deps[2]);
const variables = makeAccessTreeProxy(lazyVariables);
const lazyContainers = makeLazyContext(deps[3]);
const containers = makeAccessTreeProxy(lazyContainers);
const get: GetFunction = (signal) => {
const dispose = signal.subscribe(() => {
cleanupSubscriptions(subscriptions);
lazyVariables[LOCK] = true;
lazyContainers[LOCK] = true;
setState((s) => ({
...s,
value: fn(get, configs, props, variables, containers),
}));
});
subscriptions.add(dispose);
return signal._get();
};
return {
value: fn(get, configs, props, variables, containers),
subscriptions: s.subscriptions,
deps: [configs, props, variables, containers],
};
});
}
return state.value;
}
function makeLazyContext<T extends object>(context: React.Context<T>) {
let locked = false;
let ctx: T | undefined;
return new Proxy(
{},
{
get(_, prop, receiver) {
if (prop === LOCK) {
locked = true;
return undefined;
}
if (locked) {
if (ctx === undefined) {
return;
}
return Reflect.get(ctx, prop, receiver);
}
ctx ??= makeAccessTreeProxy(use(context));
return Reflect.get(ctx, prop, receiver);
},
},
) as T & { [LOCK]: true };
}
function makeAccessTreeProxy<T extends object>(value: T): T {
const branches = new Map<keyof T, object>();
return new Proxy(value, {
get(target, prop, receiver) {
if (prop === IS_EQUAL) {
return (other: T) => {
return (
Object.is(target, other) ||
Array.from(branches).every(([key, child]) => {
return typeof child === "object" && IS_EQUAL in child
? (child as IsEqualObject)[IS_EQUAL](other[key])
: Object.is(child, other[key]);
})
);
};
}
const value = Reflect.get(target, prop, receiver);
if (typeof value === "object" && value !== null) {
const proxy = makeAccessTreeProxy(value);
branches.set(prop as keyof T, proxy);
return proxy;
} else {
return value;
}
},
});
}