-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
157 lines (136 loc) · 3.33 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
145
146
147
148
149
150
151
152
153
154
155
156
157
import { signal, effect, computed, batch, Signal } from "@preact/signals-core";
import {
createGraph,
getAdministration,
getInternalNode,
getObservable,
getObservableClassInstance,
getSource,
setAdministrationType,
ObjectAdministration,
} from "nu-observables";
export { isObservable } from "nu-observables";
class PreactObjectAdministration<
T extends object
> extends ObjectAdministration<T> {
static proxyTraps: ProxyHandler<object> = Object.assign(
{},
ObjectAdministration.proxyTraps,
{
get(target, prop, proxy) {
if (
!(prop in target) &&
(typeof prop === "string" || typeof prop === "number") &&
String(prop)[0] === "$"
) {
return getSignal(proxy, prop.substring(1) as keyof typeof target);
}
return ObjectAdministration.proxyTraps.get?.apply(
null,
arguments as any
);
},
} as ProxyHandler<object>
);
}
export const graph = createGraph({
batch,
effect,
createComputed(fn, context) {
const c = computed(fn.bind(context));
return {
node: c,
get() {
return c.value;
},
};
},
createAtom() {
let value = 0;
const s = signal(value);
return {
node: s,
reportChanged() {
s.value = ++value;
return value;
},
reportObserved() {
return s.value;
},
};
},
});
setAdministrationType({ object: PreactObjectAdministration }, graph);
export type PreactObservable<T> =
T extends Function ? T :
T extends Map<infer K, infer V> ? Map<K, PreactObservable<V>> :
T extends Array<infer V> ? Array<PreactObservable<V>> :
T extends Set<infer V> ? Set<PreactObservable<V>> :
T extends WeakSet<infer V> ? WeakSet<PreactObservable<V>> :
T extends WeakMap<infer K, infer V> ? WeakMap<K, PreactObservable<V>> :
{
[key in keyof T]: T[key] extends object
? PreactObservable<T[key]>
: T[key];
} & {
readonly [key in keyof T as T[key] extends object
? never
: `$${string & key}`]?: Signal<T[key]>;
};
export function observable<T>(
obj: T
): PreactObservable<T> {
return getObservable(obj, graph) as any;
}
export function source<T>(obj: PreactObservable<T> | T): T {
return getSource(obj) as T;
}
export class Observable {
constructor() {
return getObservableClassInstance(this, graph);
}
}
export function reportChanged<T extends object>(obj: T): T {
const adm = getAdministration(obj);
adm.reportChanged();
return obj;
}
export function reportObserved<T extends object>(
obj: T,
opts?: { deep?: boolean }
): T {
const adm = getAdministration(obj);
adm.reportObserved(opts?.deep);
return obj;
}
const signalMap: WeakMap<Signal, Signal> = new WeakMap();
export function getSignal<T extends object>(
obj: T,
key: keyof T
): Signal<T[keyof T]> {
const node = getInternalNode(obj, key);
if (node instanceof Signal) {
let signal = signalMap.get(node);
if (!signal) {
signal = new Signal();
Object.defineProperties(signal, {
value: {
get() {
return obj[key];
},
set(v) {
return (obj[key] = v);
},
},
peek: {
value() {
return source(obj)[key];
},
},
});
signalMap.set(node, signal);
}
return signal!;
}
return node;
}