-
Notifications
You must be signed in to change notification settings - Fork 5
/
local-copy.ts
193 lines (162 loc) · 4.62 KB
/
local-copy.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
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
import { signal } from "./index.ts";
class Meta<Value> {
prevRemote?: Value;
peek?: () => Value;
@signal accessor value: Value | undefined;
}
function getOrCreateMeta<Value>(
instance: WeakKey,
metas: WeakMap<WeakKey, Meta<Value>>,
initializer?: Value | Function,
) {
let meta = metas.get(instance);
if (meta === undefined) {
meta = new Meta<Value>();
metas.set(instance, meta);
meta.value = meta.peek =
typeof initializer === "function"
? initializer.call(instance)
: initializer;
}
return meta;
}
function get(obj: any, path: string) {
let current = obj;
let parts = path.split(".");
for (let part of parts) {
if (!current) return current;
if (!(part in current)) {
throw new Error(
`sub-path ${part} (from ${path}) does not exist on ${JSON.stringify(current)}.`,
);
}
current = current[part];
}
return current;
}
/**
* Forks remote state for local modification
*
* ```js
* import { Signal } from 'signal-polyfill';
* import { localCopy } from 'signal-utils';
*
* const remote = new Signal.State(0);
*
* const local = localCopy(() => remote.get());
* ```
*/
export function localCopy<Value = any>(
fn: () => Value,
): { get(): Value; set(v: Value): void };
/**
* Forks remote state for local modification
*
* ```js
* import { localCopy } from 'signal-utils';
*
* class Demo {
* @localCopy('remote.value') accessor localValue;
* }
* ```
*/
export function localCopy<Value = any, This extends WeakKey = WeakKey>(
memo: string,
initializer?: Value | (() => Value),
): (
_target: ClassAccessorDecoratorTarget<This, Value>,
_context: ClassAccessorDecoratorContext<This, Value>,
) => ClassAccessorDecoratorResult<This, Value>;
/**
* Forks remote state for local modification
*
* ```js
* import { localCopy } from 'signal-utils';
*
* class Demo {
* @localCopy('remote.value') accessor localValue;
* }
* ```
*/
export function localCopy<Value = any, This extends WeakKey = WeakKey>(
...args: any[]
) {
if (typeof args[0] === "function") {
return localCopyFn<Value, This>(args[0]);
}
let [first, second] = args;
return localCopyDecorator<Value, This>(
first,
second as undefined | Value | (() => Value),
);
}
function localCopyFn<Value = any, This extends WeakKey = WeakKey>(
memoFn: () => Value,
) {
let metas = new WeakMap<WeakKey, Meta<Value>>();
return {
get(this: This): Value {
let meta = getOrCreateMeta<Value>(this, metas);
let { prevRemote } = meta;
let incomingValue = memoFn();
if (prevRemote !== incomingValue) {
// If the incoming value is not the same as the previous incoming value,
// update the local value to match the new incoming value, and update
// the previous incoming value.
meta.value = meta.prevRemote = incomingValue;
}
return meta.value as Value;
},
set(this: WeakKey, value: Value) {
if (!metas.has(this)) {
let meta = getOrCreateMeta(this, metas);
meta.prevRemote = memoFn();
meta.value = value;
return;
}
getOrCreateMeta(this, metas).value = value;
},
};
}
function localCopyDecorator<Value = any, This extends WeakKey = WeakKey>(
memo: string,
initializer: undefined | Value | (() => Value),
) {
if (typeof memo !== "string") {
throw new Error(
`@localCopy() must be given a memo path as its first argument, received \`${String(
memo,
)}\``,
);
}
let metas = new WeakMap<WeakKey, Meta<Value>>();
return function localCopyDecorator(
_target: ClassAccessorDecoratorTarget<This, Value>,
_context: ClassAccessorDecoratorContext<This, Value>,
): ClassAccessorDecoratorResult<This, Value> {
let memoFn = (obj: any) => get(obj, memo);
return {
get(this: This): Value {
let meta = getOrCreateMeta<Value>(this, metas, initializer);
let { prevRemote } = meta;
let incomingValue = memoFn(this);
if (prevRemote !== incomingValue) {
// If the incoming value is not the same as the previous incoming value,
// update the local value to match the new incoming value, and update
// the previous incoming value.
meta.value = meta.prevRemote = incomingValue;
}
return meta.value as Value;
},
set(this: WeakKey, value: Value) {
if (!metas.has(this)) {
let meta = getOrCreateMeta(this, metas, initializer);
meta.prevRemote = memoFn(this);
meta.value = value;
return;
}
getOrCreateMeta(this, metas, initializer).value = value;
},
};
};
}