cross-state 0.6.7 → 0.6.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/hash.cjs +712 -0
- package/dist/cjs/hash.cjs.map +1 -0
- package/dist/cjs/immer.cjs +7 -2
- package/dist/cjs/immer.cjs.map +1 -1
- package/dist/cjs/index.cjs +341 -19
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/react.cjs +289 -11
- package/dist/cjs/react.cjs.map +1 -1
- package/dist/es/hash.mjs +713 -0
- package/dist/es/hash.mjs.map +1 -0
- package/dist/es/{immer/immerActions.js → immer.mjs} +1 -1
- package/dist/es/immer.mjs.map +1 -0
- package/dist/es/{core/cache.js → index.mjs} +126 -8
- package/dist/es/index.mjs.map +1 -0
- package/dist/es/react.mjs +292 -0
- package/dist/es/react.mjs.map +1 -0
- package/package.json +8 -8
- package/dist/cjs/_virtual/use-sync-external-store-with-selector.development.cjs +0 -103
- package/dist/cjs/_virtual/use-sync-external-store-with-selector.development.cjs.map +0 -1
- package/dist/cjs/_virtual/use-sync-external-store-with-selector.development2.cjs +0 -5
- package/dist/cjs/_virtual/use-sync-external-store-with-selector.development2.cjs.map +0 -1
- package/dist/cjs/_virtual/use-sync-external-store-with-selector.production.min.cjs +0 -71
- package/dist/cjs/_virtual/use-sync-external-store-with-selector.production.min.cjs.map +0 -1
- package/dist/cjs/_virtual/use-sync-external-store-with-selector.production.min2.cjs +0 -5
- package/dist/cjs/_virtual/use-sync-external-store-with-selector.production.min2.cjs.map +0 -1
- package/dist/cjs/_virtual/with-selector.cjs +0 -12
- package/dist/cjs/_virtual/with-selector.cjs.map +0 -1
- package/dist/cjs/_virtual/with-selector2.cjs +0 -13
- package/dist/cjs/_virtual/with-selector2.cjs.map +0 -1
- package/dist/cjs/core/cache.cjs +0 -227
- package/dist/cjs/core/cache.cjs.map +0 -1
- package/dist/cjs/core/resourceGroup.cjs +0 -53
- package/dist/cjs/core/resourceGroup.cjs.map +0 -1
- package/dist/cjs/core/store.cjs +0 -259
- package/dist/cjs/core/store.cjs.map +0 -1
- package/dist/cjs/immer/immerActions.cjs +0 -10
- package/dist/cjs/immer/immerActions.cjs.map +0 -1
- package/dist/cjs/lib/calcDuration.cjs +0 -9
- package/dist/cjs/lib/calcDuration.cjs.map +0 -1
- package/dist/cjs/lib/calculationHelper.cjs +0 -140
- package/dist/cjs/lib/calculationHelper.cjs.map +0 -1
- package/dist/cjs/lib/callable.cjs +0 -11
- package/dist/cjs/lib/callable.cjs.map +0 -1
- package/dist/cjs/lib/clone.cjs +0 -19
- package/dist/cjs/lib/clone.cjs.map +0 -1
- package/dist/cjs/lib/debounce.cjs +0 -28
- package/dist/cjs/lib/debounce.cjs.map +0 -1
- package/dist/cjs/lib/equals.cjs +0 -5
- package/dist/cjs/lib/equals.cjs.map +0 -1
- package/dist/cjs/lib/forwardError.cjs +0 -9
- package/dist/cjs/lib/forwardError.cjs.map +0 -1
- package/dist/cjs/lib/hash.cjs +0 -19
- package/dist/cjs/lib/hash.cjs.map +0 -1
- package/dist/cjs/lib/instanceCache.cjs +0 -68
- package/dist/cjs/lib/instanceCache.cjs.map +0 -1
- package/dist/cjs/lib/makeSelector.cjs +0 -14
- package/dist/cjs/lib/makeSelector.cjs.map +0 -1
- package/dist/cjs/lib/propAccess.cjs +0 -66
- package/dist/cjs/lib/propAccess.cjs.map +0 -1
- package/dist/cjs/lib/queue.cjs +0 -61
- package/dist/cjs/lib/queue.cjs.map +0 -1
- package/dist/cjs/lib/standardMethods.cjs +0 -78
- package/dist/cjs/lib/standardMethods.cjs.map +0 -1
- package/dist/cjs/lib/throttle.cjs +0 -25
- package/dist/cjs/lib/throttle.cjs.map +0 -1
- package/dist/cjs/lib/trackingProxy.cjs +0 -39
- package/dist/cjs/lib/trackingProxy.cjs.map +0 -1
- package/dist/cjs/react/read.cjs +0 -15
- package/dist/cjs/react/read.cjs.map +0 -1
- package/dist/cjs/react/storeScope.cjs +0 -31
- package/dist/cjs/react/storeScope.cjs.map +0 -1
- package/dist/cjs/react/useCache.cjs +0 -31
- package/dist/cjs/react/useCache.cjs.map +0 -1
- package/dist/cjs/react/useProp.cjs +0 -9
- package/dist/cjs/react/useProp.cjs.map +0 -1
- package/dist/cjs/react/useStore.cjs +0 -51
- package/dist/cjs/react/useStore.cjs.map +0 -1
- package/dist/es/_virtual/use-sync-external-store-with-selector.development.js +0 -103
- package/dist/es/_virtual/use-sync-external-store-with-selector.development.js.map +0 -1
- package/dist/es/_virtual/use-sync-external-store-with-selector.development2.js +0 -5
- package/dist/es/_virtual/use-sync-external-store-with-selector.development2.js.map +0 -1
- package/dist/es/_virtual/use-sync-external-store-with-selector.production.min.js +0 -71
- package/dist/es/_virtual/use-sync-external-store-with-selector.production.min.js.map +0 -1
- package/dist/es/_virtual/use-sync-external-store-with-selector.production.min2.js +0 -5
- package/dist/es/_virtual/use-sync-external-store-with-selector.production.min2.js.map +0 -1
- package/dist/es/_virtual/with-selector.js +0 -11
- package/dist/es/_virtual/with-selector.js.map +0 -1
- package/dist/es/_virtual/with-selector2.js +0 -14
- package/dist/es/_virtual/with-selector2.js.map +0 -1
- package/dist/es/core/cache.js.map +0 -1
- package/dist/es/core/resourceGroup.js +0 -53
- package/dist/es/core/resourceGroup.js.map +0 -1
- package/dist/es/core/store.js +0 -259
- package/dist/es/core/store.js.map +0 -1
- package/dist/es/immer/immerActions.js.map +0 -1
- package/dist/es/immer.js +0 -5
- package/dist/es/immer.js.map +0 -1
- package/dist/es/index.js +0 -22
- package/dist/es/index.js.map +0 -1
- package/dist/es/lib/calcDuration.js +0 -9
- package/dist/es/lib/calcDuration.js.map +0 -1
- package/dist/es/lib/calculationHelper.js +0 -140
- package/dist/es/lib/calculationHelper.js.map +0 -1
- package/dist/es/lib/callable.js +0 -11
- package/dist/es/lib/callable.js.map +0 -1
- package/dist/es/lib/clone.js +0 -19
- package/dist/es/lib/clone.js.map +0 -1
- package/dist/es/lib/debounce.js +0 -28
- package/dist/es/lib/debounce.js.map +0 -1
- package/dist/es/lib/equals.js +0 -5
- package/dist/es/lib/equals.js.map +0 -1
- package/dist/es/lib/forwardError.js +0 -9
- package/dist/es/lib/forwardError.js.map +0 -1
- package/dist/es/lib/hash.js +0 -19
- package/dist/es/lib/hash.js.map +0 -1
- package/dist/es/lib/instanceCache.js +0 -68
- package/dist/es/lib/instanceCache.js.map +0 -1
- package/dist/es/lib/makeSelector.js +0 -14
- package/dist/es/lib/makeSelector.js.map +0 -1
- package/dist/es/lib/propAccess.js +0 -66
- package/dist/es/lib/propAccess.js.map +0 -1
- package/dist/es/lib/queue.js +0 -61
- package/dist/es/lib/queue.js.map +0 -1
- package/dist/es/lib/standardMethods.js +0 -78
- package/dist/es/lib/standardMethods.js.map +0 -1
- package/dist/es/lib/throttle.js +0 -25
- package/dist/es/lib/throttle.js.map +0 -1
- package/dist/es/lib/trackingProxy.js +0 -39
- package/dist/es/lib/trackingProxy.js.map +0 -1
- package/dist/es/react/read.js +0 -15
- package/dist/es/react/read.js.map +0 -1
- package/dist/es/react/storeScope.js +0 -31
- package/dist/es/react/storeScope.js.map +0 -1
- package/dist/es/react/useCache.js +0 -31
- package/dist/es/react/useCache.js.map +0 -1
- package/dist/es/react/useProp.js +0 -9
- package/dist/es/react/useProp.js.map +0 -1
- package/dist/es/react/useStore.js +0 -51
- package/dist/es/react/useStore.js.map +0 -1
- package/dist/es/react.js +0 -14
- package/dist/es/react.js.map +0 -1
|
@@ -0,0 +1,712 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const calcDuration = (t) => {
|
|
3
|
+
if (typeof t === "number")
|
|
4
|
+
return t;
|
|
5
|
+
return (t.milliseconds ?? 0) + (t.seconds ?? 0) * 1e3 + (t.minutes ?? 0) * 60 * 1e3 + (t.hours ?? 0) * 60 * 60 * 1e3 + (t.days ?? 0) * 24 * 60 * 60 * 1e3;
|
|
6
|
+
};
|
|
7
|
+
function queue() {
|
|
8
|
+
const q = [];
|
|
9
|
+
const completionListeners = /* @__PURE__ */ new Set();
|
|
10
|
+
let active = false;
|
|
11
|
+
const notify = () => {
|
|
12
|
+
for (const listener of completionListeners) {
|
|
13
|
+
listener();
|
|
14
|
+
}
|
|
15
|
+
completionListeners.clear();
|
|
16
|
+
};
|
|
17
|
+
const run = async () => {
|
|
18
|
+
if (!active) {
|
|
19
|
+
active = true;
|
|
20
|
+
let next;
|
|
21
|
+
while (next = q.shift()) {
|
|
22
|
+
try {
|
|
23
|
+
let result = next.action();
|
|
24
|
+
if (result instanceof Promise) {
|
|
25
|
+
result = await result;
|
|
26
|
+
}
|
|
27
|
+
next.resolve(result);
|
|
28
|
+
} catch (error) {
|
|
29
|
+
next.reject(error);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
active = false;
|
|
33
|
+
notify();
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
return Object.assign(
|
|
37
|
+
(action, ref) => {
|
|
38
|
+
return new Promise((resolve, reject) => {
|
|
39
|
+
q.push({ action, resolve, reject, ref });
|
|
40
|
+
run();
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
clear() {
|
|
45
|
+
q.length = 0;
|
|
46
|
+
},
|
|
47
|
+
whenDone() {
|
|
48
|
+
if (!active) {
|
|
49
|
+
return Promise.resolve();
|
|
50
|
+
}
|
|
51
|
+
return new Promise((resolve) => {
|
|
52
|
+
completionListeners.add(resolve);
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
get size() {
|
|
56
|
+
return q.length;
|
|
57
|
+
},
|
|
58
|
+
getRefs() {
|
|
59
|
+
return q.map((item) => item.ref).filter((x) => x !== void 0);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
const ProxyKeys = [
|
|
65
|
+
"get",
|
|
66
|
+
"getOwnPropertyDescriptor",
|
|
67
|
+
"getPrototypeOf",
|
|
68
|
+
"has",
|
|
69
|
+
"isExtensible",
|
|
70
|
+
"ownKeys"
|
|
71
|
+
];
|
|
72
|
+
const isPlainObject = (value) => typeof value === "object" && value !== null && Object.getPrototypeOf(value) === Object.prototype;
|
|
73
|
+
function trackingProxy(value) {
|
|
74
|
+
if (!isPlainObject(value) && !Array.isArray(value)) {
|
|
75
|
+
return [value, (other) => other === value];
|
|
76
|
+
}
|
|
77
|
+
const deps = new Array();
|
|
78
|
+
const proxy = new Proxy(
|
|
79
|
+
value,
|
|
80
|
+
Object.fromEntries(
|
|
81
|
+
ProxyKeys.map((key) => [
|
|
82
|
+
key,
|
|
83
|
+
(currentValue, ...args) => {
|
|
84
|
+
const function_ = Reflect[key];
|
|
85
|
+
const [proxiedValue, equals] = trackingProxy(function_(currentValue, ...args));
|
|
86
|
+
deps.push((otherValue) => {
|
|
87
|
+
if (!isPlainObject(otherValue) && !Array.isArray(otherValue)) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
return equals(function_(otherValue, ...args));
|
|
91
|
+
});
|
|
92
|
+
return proxiedValue;
|
|
93
|
+
}
|
|
94
|
+
])
|
|
95
|
+
)
|
|
96
|
+
);
|
|
97
|
+
return [proxy, (other) => !!other && deps.every((equals) => equals(other))];
|
|
98
|
+
}
|
|
99
|
+
class CalculationHelper {
|
|
100
|
+
constructor(options) {
|
|
101
|
+
this.options = options;
|
|
102
|
+
options.addEffect(() => {
|
|
103
|
+
if (this.current) {
|
|
104
|
+
this.current.check();
|
|
105
|
+
} else {
|
|
106
|
+
this.execute();
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
execute() {
|
|
111
|
+
this.stop();
|
|
112
|
+
const { calculate, addEffect, getValue, setValue, setError, onInvalidate } = this.options;
|
|
113
|
+
const checks = new Array();
|
|
114
|
+
const deps = /* @__PURE__ */ new Map();
|
|
115
|
+
const q = queue();
|
|
116
|
+
let isActive = false;
|
|
117
|
+
let isCancled = false;
|
|
118
|
+
const cancelEffect = addEffect(() => {
|
|
119
|
+
isActive = true;
|
|
120
|
+
for (const dep of deps.values()) {
|
|
121
|
+
dep.on();
|
|
122
|
+
}
|
|
123
|
+
return () => {
|
|
124
|
+
isActive = false;
|
|
125
|
+
for (const dep of deps.values()) {
|
|
126
|
+
dep.off();
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
});
|
|
130
|
+
const cancel = () => {
|
|
131
|
+
isCancled = true;
|
|
132
|
+
cancelSubscription == null ? void 0 : cancelSubscription();
|
|
133
|
+
cancelEffect();
|
|
134
|
+
delete this.current;
|
|
135
|
+
};
|
|
136
|
+
const checkAll = () => {
|
|
137
|
+
if (!checks.every((check) => check())) {
|
|
138
|
+
cancel();
|
|
139
|
+
onInvalidate == null ? void 0 : onInvalidate();
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
const invalidateDependencies = () => {
|
|
143
|
+
for (const dep of deps.values()) {
|
|
144
|
+
dep.invalidate();
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
const use = (store, { disableProxy } = {}) => {
|
|
148
|
+
if (isCancled) {
|
|
149
|
+
return store.get();
|
|
150
|
+
}
|
|
151
|
+
let value = store.get();
|
|
152
|
+
let equals = (newValue) => {
|
|
153
|
+
return newValue === value;
|
|
154
|
+
};
|
|
155
|
+
if (!disableProxy) {
|
|
156
|
+
[value, equals] = trackingProxy(value);
|
|
157
|
+
}
|
|
158
|
+
let sub;
|
|
159
|
+
const dep = {
|
|
160
|
+
on() {
|
|
161
|
+
this.off();
|
|
162
|
+
sub = store.sub(checkAll, { runNow: false });
|
|
163
|
+
},
|
|
164
|
+
off() {
|
|
165
|
+
sub == null ? void 0 : sub();
|
|
166
|
+
sub = void 0;
|
|
167
|
+
},
|
|
168
|
+
invalidate() {
|
|
169
|
+
if ("invalidate" in store && store.invalidate instanceof Function) {
|
|
170
|
+
store.invalidate();
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
if (isActive) {
|
|
175
|
+
dep.on();
|
|
176
|
+
}
|
|
177
|
+
checks.push(() => equals(store.get()));
|
|
178
|
+
deps.set(store, dep);
|
|
179
|
+
return value;
|
|
180
|
+
};
|
|
181
|
+
const updateValue = (update) => q(async () => {
|
|
182
|
+
if (isCancled) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
if (update instanceof Function) {
|
|
186
|
+
try {
|
|
187
|
+
update = update(getValue == null ? void 0 : getValue());
|
|
188
|
+
} catch (error) {
|
|
189
|
+
setError == null ? void 0 : setError(error);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (update instanceof Promise) {
|
|
194
|
+
try {
|
|
195
|
+
update = await update;
|
|
196
|
+
} catch (error) {
|
|
197
|
+
if (!isCancled) {
|
|
198
|
+
setError == null ? void 0 : setError(error);
|
|
199
|
+
}
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (!isCancled) {
|
|
204
|
+
setValue == null ? void 0 : setValue(update);
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
const updateError = (error) => q(() => {
|
|
208
|
+
if (!isCancled) {
|
|
209
|
+
setError == null ? void 0 : setError(error);
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
let cancelSubscription;
|
|
213
|
+
try {
|
|
214
|
+
cancelSubscription = calculate({ use, updateValue, updateError });
|
|
215
|
+
} catch (error) {
|
|
216
|
+
setError == null ? void 0 : setError(error);
|
|
217
|
+
}
|
|
218
|
+
this.current = { cancel, check: checkAll, invalidateDependencies };
|
|
219
|
+
}
|
|
220
|
+
stop() {
|
|
221
|
+
var _a;
|
|
222
|
+
(_a = this.current) == null ? void 0 : _a.cancel();
|
|
223
|
+
}
|
|
224
|
+
check() {
|
|
225
|
+
var _a;
|
|
226
|
+
(_a = this.current) == null ? void 0 : _a.check();
|
|
227
|
+
}
|
|
228
|
+
invalidateDependencies() {
|
|
229
|
+
var _a;
|
|
230
|
+
(_a = this.current) == null ? void 0 : _a.invalidateDependencies();
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
class Callable extends Function {
|
|
234
|
+
constructor(_call) {
|
|
235
|
+
super("...args", "return this._call(...args)");
|
|
236
|
+
this._call = _call;
|
|
237
|
+
return this.bind(this);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
function debounce(action, options) {
|
|
241
|
+
const wait = typeof options === "object" && "wait" in options ? calcDuration(options.wait) : calcDuration(options);
|
|
242
|
+
const maxWait = typeof options === "object" && "maxWait" in options && options.maxWait !== void 0 ? calcDuration(options.maxWait) : void 0;
|
|
243
|
+
let timeout;
|
|
244
|
+
let timeoutStarted;
|
|
245
|
+
return (...args) => {
|
|
246
|
+
const now = Date.now();
|
|
247
|
+
timeoutStarted ?? (timeoutStarted = now);
|
|
248
|
+
const deadline = Math.min(
|
|
249
|
+
//
|
|
250
|
+
now + wait,
|
|
251
|
+
timeoutStarted + (maxWait ?? Number.POSITIVE_INFINITY)
|
|
252
|
+
);
|
|
253
|
+
if (timeout !== void 0) {
|
|
254
|
+
clearTimeout(timeout);
|
|
255
|
+
}
|
|
256
|
+
timeout = setTimeout(() => {
|
|
257
|
+
timeout = void 0;
|
|
258
|
+
timeoutStarted = void 0;
|
|
259
|
+
action(...args);
|
|
260
|
+
}, deadline - now);
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
const defaultEquals = (a, b) => a === b;
|
|
264
|
+
function forwardError(error) {
|
|
265
|
+
setTimeout(() => {
|
|
266
|
+
throw error;
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
function flatClone(object) {
|
|
270
|
+
if (object instanceof Map) {
|
|
271
|
+
return new Map(object);
|
|
272
|
+
}
|
|
273
|
+
if (object instanceof Set) {
|
|
274
|
+
return new Set(object);
|
|
275
|
+
}
|
|
276
|
+
if (Array.isArray(object)) {
|
|
277
|
+
return [...object];
|
|
278
|
+
}
|
|
279
|
+
if (object instanceof Object) {
|
|
280
|
+
return { ...object };
|
|
281
|
+
}
|
|
282
|
+
return object;
|
|
283
|
+
}
|
|
284
|
+
function castArrayPath(path) {
|
|
285
|
+
if (Array.isArray(path)) {
|
|
286
|
+
return path;
|
|
287
|
+
}
|
|
288
|
+
if (path === "") {
|
|
289
|
+
return [];
|
|
290
|
+
}
|
|
291
|
+
return path.split(".");
|
|
292
|
+
}
|
|
293
|
+
function get(object, path) {
|
|
294
|
+
const _path = castArrayPath(path);
|
|
295
|
+
const [first, ...rest] = _path;
|
|
296
|
+
if (first === void 0 || !object) {
|
|
297
|
+
return object;
|
|
298
|
+
}
|
|
299
|
+
if (object instanceof Map) {
|
|
300
|
+
return get(object.get(first), rest);
|
|
301
|
+
}
|
|
302
|
+
if (object instanceof Set) {
|
|
303
|
+
return get(Array.from(object)[Number(first)], rest);
|
|
304
|
+
}
|
|
305
|
+
if (object instanceof Object) {
|
|
306
|
+
return get(object[first], rest);
|
|
307
|
+
}
|
|
308
|
+
throw new Error(`Could not get ${path} of ${object}`);
|
|
309
|
+
}
|
|
310
|
+
function set(object, path, value, rootPath = path) {
|
|
311
|
+
const _path = castArrayPath(path);
|
|
312
|
+
const [first, ...rest] = _path;
|
|
313
|
+
if (first === void 0) {
|
|
314
|
+
return value;
|
|
315
|
+
}
|
|
316
|
+
const updateChild = (child) => {
|
|
317
|
+
if (!child && rest.length > 0) {
|
|
318
|
+
const _rootPath = castArrayPath(rootPath);
|
|
319
|
+
const prefix = _rootPath.slice(0, -rest.length);
|
|
320
|
+
throw new Error(`Cannot set ${rootPath} because ${prefix.join(".")} is ${child}`);
|
|
321
|
+
}
|
|
322
|
+
return set(child, rest, value, rootPath);
|
|
323
|
+
};
|
|
324
|
+
if (object instanceof Map) {
|
|
325
|
+
const copy = flatClone(object);
|
|
326
|
+
const child = copy.get(first);
|
|
327
|
+
copy.set(first, updateChild(child));
|
|
328
|
+
return copy;
|
|
329
|
+
}
|
|
330
|
+
if (object instanceof Set) {
|
|
331
|
+
const copy = [...object];
|
|
332
|
+
const child = copy[Number(first)];
|
|
333
|
+
copy[Number(first)] = updateChild(child);
|
|
334
|
+
return new Set(copy);
|
|
335
|
+
}
|
|
336
|
+
if (object instanceof Object) {
|
|
337
|
+
const copy = flatClone(object);
|
|
338
|
+
copy[first] = updateChild(copy[first]);
|
|
339
|
+
return copy;
|
|
340
|
+
}
|
|
341
|
+
throw new Error(`Could not set ${path} of ${object}`);
|
|
342
|
+
}
|
|
343
|
+
function makeSelector(selector) {
|
|
344
|
+
if (!selector) {
|
|
345
|
+
return (x) => x;
|
|
346
|
+
}
|
|
347
|
+
if (selector instanceof Function) {
|
|
348
|
+
return selector;
|
|
349
|
+
}
|
|
350
|
+
return (x) => get(x, selector);
|
|
351
|
+
}
|
|
352
|
+
const createArrayAction = (prop) => function arrayAction(...args) {
|
|
353
|
+
const newArray = this.get().slice();
|
|
354
|
+
const result = newArray[prop](...args);
|
|
355
|
+
this.set(newArray);
|
|
356
|
+
return result;
|
|
357
|
+
};
|
|
358
|
+
const arrayMethods = {
|
|
359
|
+
splice: /* @__PURE__ */ createArrayAction("splice"),
|
|
360
|
+
push: /* @__PURE__ */ createArrayAction("push"),
|
|
361
|
+
pop: /* @__PURE__ */ createArrayAction("pop"),
|
|
362
|
+
shift: /* @__PURE__ */ createArrayAction("shift"),
|
|
363
|
+
unshift: /* @__PURE__ */ createArrayAction("unshift"),
|
|
364
|
+
reverse: /* @__PURE__ */ createArrayAction("reverse"),
|
|
365
|
+
sort: /* @__PURE__ */ createArrayAction("sort")
|
|
366
|
+
};
|
|
367
|
+
const recordMethods = {
|
|
368
|
+
// set<T extends Record<any, any>, P extends Path<T>>(
|
|
369
|
+
// this: Store<T>,
|
|
370
|
+
// path: P,
|
|
371
|
+
// value: Update<Value<T, P>>,
|
|
372
|
+
// ) {
|
|
373
|
+
// if (value instanceof Function) {
|
|
374
|
+
// value = value(get(this.get(), path));
|
|
375
|
+
// }
|
|
376
|
+
// this.set(set(this.get(), path, value));
|
|
377
|
+
// return this;
|
|
378
|
+
// },
|
|
379
|
+
delete(key) {
|
|
380
|
+
const copy = { ...this.get() };
|
|
381
|
+
delete copy[key];
|
|
382
|
+
this.set(copy);
|
|
383
|
+
},
|
|
384
|
+
clear() {
|
|
385
|
+
this.set({});
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
const mapMethods = {
|
|
389
|
+
// set<K, V>(this: Store<Map<K, V>>, key: K, value: UpdateFrom<V, [V | undefined]>) {
|
|
390
|
+
// if (value instanceof Function) {
|
|
391
|
+
// value = value(this.get().get(key));
|
|
392
|
+
// }
|
|
393
|
+
// const newMap = new Map(this.get());
|
|
394
|
+
// newMap.set(key, value);
|
|
395
|
+
// this.set(newMap);
|
|
396
|
+
// return this;
|
|
397
|
+
// },
|
|
398
|
+
delete(key) {
|
|
399
|
+
const newMap = new Map(this.get());
|
|
400
|
+
const result = newMap.delete(key);
|
|
401
|
+
this.set(newMap);
|
|
402
|
+
return result;
|
|
403
|
+
},
|
|
404
|
+
clear() {
|
|
405
|
+
this.set(/* @__PURE__ */ new Map());
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
const setMethods = {
|
|
409
|
+
add(value) {
|
|
410
|
+
const newSet = new Set(this.get());
|
|
411
|
+
newSet.add(value);
|
|
412
|
+
this.set(newSet);
|
|
413
|
+
},
|
|
414
|
+
delete(value) {
|
|
415
|
+
const newSet = new Set(this.get());
|
|
416
|
+
newSet.delete(value);
|
|
417
|
+
this.set(newSet);
|
|
418
|
+
},
|
|
419
|
+
clear() {
|
|
420
|
+
this.set(/* @__PURE__ */ new Set());
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
function throttle(action, duration) {
|
|
424
|
+
const ms = calcDuration(duration);
|
|
425
|
+
let t = 0;
|
|
426
|
+
let timeout;
|
|
427
|
+
return (...args) => {
|
|
428
|
+
if (timeout !== void 0) {
|
|
429
|
+
clearTimeout(timeout);
|
|
430
|
+
}
|
|
431
|
+
const dt = t + ms - Date.now();
|
|
432
|
+
if (dt <= 0) {
|
|
433
|
+
action(...args);
|
|
434
|
+
t = Date.now();
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
timeout = setTimeout(() => {
|
|
438
|
+
action(...args);
|
|
439
|
+
t = Date.now();
|
|
440
|
+
}, dt);
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
const noop = () => void 0;
|
|
444
|
+
class Store extends Callable {
|
|
445
|
+
constructor(getter, options = {}, derivedFrom, _call = () => void 0) {
|
|
446
|
+
super(_call);
|
|
447
|
+
this.getter = getter;
|
|
448
|
+
this.options = options;
|
|
449
|
+
this.derivedFrom = derivedFrom;
|
|
450
|
+
this._call = _call;
|
|
451
|
+
this.listeners = /* @__PURE__ */ new Map();
|
|
452
|
+
this.effects = /* @__PURE__ */ new Map();
|
|
453
|
+
this.notifyId = {};
|
|
454
|
+
this.calculationHelper = new CalculationHelper({
|
|
455
|
+
calculate: ({ use }) => {
|
|
456
|
+
if (this.getter instanceof Function) {
|
|
457
|
+
const value = this.getter.apply({ use }, [{ use }]);
|
|
458
|
+
this._value = { v: value };
|
|
459
|
+
this.notify();
|
|
460
|
+
}
|
|
461
|
+
},
|
|
462
|
+
addEffect: this.addEffect.bind(this),
|
|
463
|
+
onInvalidate: this.reset.bind(this)
|
|
464
|
+
});
|
|
465
|
+
this.get = this.get.bind(this);
|
|
466
|
+
this.set = this.set.bind(this);
|
|
467
|
+
this.sub = this.sub.bind(this);
|
|
468
|
+
this.once = this.once.bind(this);
|
|
469
|
+
this.map = this.map.bind(this);
|
|
470
|
+
this.addEffect = this.addEffect.bind(this);
|
|
471
|
+
this.isActive = this.isActive.bind(this);
|
|
472
|
+
if (!(getter instanceof Function)) {
|
|
473
|
+
this._value = { v: getter };
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
get() {
|
|
477
|
+
this.calculationHelper.check();
|
|
478
|
+
if (!this._value) {
|
|
479
|
+
this.calculationHelper.execute();
|
|
480
|
+
return this.get();
|
|
481
|
+
}
|
|
482
|
+
return this._value.v;
|
|
483
|
+
}
|
|
484
|
+
set(...args) {
|
|
485
|
+
const path = args.length > 1 ? args[0] : [];
|
|
486
|
+
let update = args.length > 1 ? args[1] : args[0];
|
|
487
|
+
if (update instanceof Function) {
|
|
488
|
+
const before = this.get();
|
|
489
|
+
const valueBefore = get(before, path);
|
|
490
|
+
const valueAfter = update(valueBefore);
|
|
491
|
+
update = set(before, path, valueAfter);
|
|
492
|
+
} else if (path.length > 0) {
|
|
493
|
+
update = set(this.get(), path, update);
|
|
494
|
+
}
|
|
495
|
+
if (this.derivedFrom && this.derivedFrom.selectors.every((selector) => typeof selector === "string")) {
|
|
496
|
+
const derivationPath = this.derivedFrom.selectors.join(".");
|
|
497
|
+
this.derivedFrom.store.set((before) => set(before, derivationPath, update));
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
if (this.derivedFrom) {
|
|
501
|
+
throw new TypeError(
|
|
502
|
+
"Can only updated computed stores that are derived from other stores using string selectors"
|
|
503
|
+
);
|
|
504
|
+
}
|
|
505
|
+
this._value = { v: update };
|
|
506
|
+
this.notify();
|
|
507
|
+
}
|
|
508
|
+
reset() {
|
|
509
|
+
this._value = void 0;
|
|
510
|
+
if (this.isActive()) {
|
|
511
|
+
this.calculationHelper.execute();
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
sub(listener, options) {
|
|
515
|
+
var _a;
|
|
516
|
+
const {
|
|
517
|
+
passive,
|
|
518
|
+
runNow = true,
|
|
519
|
+
throttle: throttleOption,
|
|
520
|
+
debounce: debounceOption,
|
|
521
|
+
equals = defaultEquals
|
|
522
|
+
} = options ?? {};
|
|
523
|
+
let compareToValue = (_a = this._value) == null ? void 0 : _a.v;
|
|
524
|
+
let previousValue;
|
|
525
|
+
let hasRun = false;
|
|
526
|
+
let innerListener = (force) => {
|
|
527
|
+
if (!this._value) {
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
const value = this._value.v;
|
|
531
|
+
if (!force && equals(value, compareToValue)) {
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
compareToValue = value;
|
|
535
|
+
const _previousValue = previousValue;
|
|
536
|
+
previousValue = value;
|
|
537
|
+
hasRun = true;
|
|
538
|
+
try {
|
|
539
|
+
listener(value, _previousValue);
|
|
540
|
+
} catch (error) {
|
|
541
|
+
forwardError(error);
|
|
542
|
+
}
|
|
543
|
+
};
|
|
544
|
+
if (throttleOption) {
|
|
545
|
+
innerListener = throttle(innerListener, throttleOption);
|
|
546
|
+
} else if (debounceOption) {
|
|
547
|
+
innerListener = debounce(innerListener, debounceOption);
|
|
548
|
+
}
|
|
549
|
+
this.listeners.set(innerListener, !passive);
|
|
550
|
+
if (!passive) {
|
|
551
|
+
this.onSubscribe();
|
|
552
|
+
}
|
|
553
|
+
if (runNow && !hasRun) {
|
|
554
|
+
innerListener(true);
|
|
555
|
+
}
|
|
556
|
+
return () => {
|
|
557
|
+
this.listeners.delete(innerListener);
|
|
558
|
+
if (!passive) {
|
|
559
|
+
this.onUnsubscribe();
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
once(condition = (value) => !!value) {
|
|
564
|
+
return new Promise((resolve) => {
|
|
565
|
+
let stopped = false;
|
|
566
|
+
const cancel = this.sub(
|
|
567
|
+
(value) => {
|
|
568
|
+
if (stopped || condition && !condition(value)) {
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
resolve(value);
|
|
572
|
+
stopped = true;
|
|
573
|
+
setTimeout(() => cancel());
|
|
574
|
+
},
|
|
575
|
+
{
|
|
576
|
+
runNow: !!condition
|
|
577
|
+
}
|
|
578
|
+
);
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
map(_selector, options) {
|
|
582
|
+
const selector = makeSelector(_selector);
|
|
583
|
+
const derivedFrom = {
|
|
584
|
+
store: this.derivedFrom ? this.derivedFrom.store : this,
|
|
585
|
+
selectors: this.derivedFrom ? [...this.derivedFrom.selectors, _selector] : [_selector]
|
|
586
|
+
};
|
|
587
|
+
return new Store(
|
|
588
|
+
({ use }) => {
|
|
589
|
+
return selector(use(this, options));
|
|
590
|
+
},
|
|
591
|
+
this.options,
|
|
592
|
+
derivedFrom
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
/** Add an effect that will be executed when the store becomes active, which means when it has at least one subscriber.
|
|
596
|
+
* @param effect
|
|
597
|
+
* If there is already a subscriber, the effect will be executed immediately.
|
|
598
|
+
* Otherweise it will be executed as soon as the first subscription is created.
|
|
599
|
+
* Every time all subscriptions are removed and the first is created again, the effect will be executed again.
|
|
600
|
+
* @param retain
|
|
601
|
+
* If provided, delay tearing down effects when the last subscriber is removed. This is useful if a short gap in subscriber coverage is supposed to be ignored. E.g. when switching pages, the old page might unsubscribe, while the new page subscribes immediately after.
|
|
602
|
+
* @returns
|
|
603
|
+
* The effect can return a teardown callback, which will be executed when the last subscription is removed and potentially the ratain time has passed.
|
|
604
|
+
*/
|
|
605
|
+
addEffect(effect, retain) {
|
|
606
|
+
this.effects.set(effect, {
|
|
607
|
+
handle: this.isActive() ? effect() ?? noop : void 0,
|
|
608
|
+
retain: retain !== void 0 ? calcDuration(retain) : void 0
|
|
609
|
+
});
|
|
610
|
+
return () => {
|
|
611
|
+
const { handle, timeout } = this.effects.get(effect) ?? {};
|
|
612
|
+
handle == null ? void 0 : handle();
|
|
613
|
+
if (timeout !== void 0) {
|
|
614
|
+
clearTimeout(timeout);
|
|
615
|
+
}
|
|
616
|
+
this.effects.delete(effect);
|
|
617
|
+
};
|
|
618
|
+
}
|
|
619
|
+
/** Return whether the store is currently active, which means whether it has at least one subscriber. */
|
|
620
|
+
isActive() {
|
|
621
|
+
return [...this.listeners.values()].some(Boolean);
|
|
622
|
+
}
|
|
623
|
+
onSubscribe() {
|
|
624
|
+
if ([...this.listeners.values()].filter(Boolean).length > 1)
|
|
625
|
+
return;
|
|
626
|
+
for (const [effect, { handle, retain, timeout }] of this.effects.entries()) {
|
|
627
|
+
if (timeout !== void 0) {
|
|
628
|
+
clearTimeout(timeout);
|
|
629
|
+
}
|
|
630
|
+
this.effects.set(effect, {
|
|
631
|
+
handle: handle ?? effect() ?? noop,
|
|
632
|
+
retain,
|
|
633
|
+
timeout: void 0
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
onUnsubscribe() {
|
|
638
|
+
if ([...this.listeners.values()].some(Boolean))
|
|
639
|
+
return;
|
|
640
|
+
for (const [effect, { handle, retain, timeout }] of this.effects.entries()) {
|
|
641
|
+
if (!retain) {
|
|
642
|
+
handle == null ? void 0 : handle();
|
|
643
|
+
}
|
|
644
|
+
if (timeout !== void 0) {
|
|
645
|
+
clearTimeout(timeout);
|
|
646
|
+
}
|
|
647
|
+
this.effects.set(effect, {
|
|
648
|
+
handle: retain ? handle : void 0,
|
|
649
|
+
retain,
|
|
650
|
+
timeout: retain && handle ? setTimeout(handle, retain) : void 0
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
notify() {
|
|
655
|
+
const n = {};
|
|
656
|
+
this.notifyId = n;
|
|
657
|
+
const snapshot = [...this.listeners.keys()];
|
|
658
|
+
for (const listener of snapshot) {
|
|
659
|
+
listener();
|
|
660
|
+
if (n !== this.notifyId)
|
|
661
|
+
break;
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
const defaultOptions = {};
|
|
666
|
+
function create(initialState, options) {
|
|
667
|
+
const store = new Store(initialState, options);
|
|
668
|
+
if (initialState instanceof Function) {
|
|
669
|
+
return store;
|
|
670
|
+
}
|
|
671
|
+
let methods = options == null ? void 0 : options.methods;
|
|
672
|
+
if (initialState instanceof Map) {
|
|
673
|
+
methods = { ...mapMethods, ...methods };
|
|
674
|
+
} else if (initialState instanceof Set) {
|
|
675
|
+
methods = { ...setMethods, ...methods };
|
|
676
|
+
} else if (Array.isArray(initialState)) {
|
|
677
|
+
methods = { ...arrayMethods, ...methods };
|
|
678
|
+
} else if (initialState instanceof Object) {
|
|
679
|
+
methods = { ...recordMethods, ...methods };
|
|
680
|
+
}
|
|
681
|
+
const boundMethods = Object.fromEntries(
|
|
682
|
+
Object.entries(methods ?? {}).filter(([name]) => !(name in store)).map(([name, action]) => [name, action.bind(store)])
|
|
683
|
+
);
|
|
684
|
+
return Object.assign(store, boundMethods);
|
|
685
|
+
}
|
|
686
|
+
const createStore = /* @__PURE__ */ Object.assign(create, { defaultOptions });
|
|
687
|
+
function hash(value) {
|
|
688
|
+
if (value instanceof Set) {
|
|
689
|
+
return `s[${[...value].map(hash).sort().join(",")}]`;
|
|
690
|
+
}
|
|
691
|
+
if (value instanceof Map) {
|
|
692
|
+
return `m[${[...value.entries()].map(hash).sort().join(",")}]`;
|
|
693
|
+
}
|
|
694
|
+
if (Array.isArray(value)) {
|
|
695
|
+
return `[${value.map(hash).join(",")}]`;
|
|
696
|
+
}
|
|
697
|
+
if (value instanceof Object) {
|
|
698
|
+
return `o[${Object.entries(value).map(hash).sort().join(",")}]`;
|
|
699
|
+
}
|
|
700
|
+
return JSON.stringify(value);
|
|
701
|
+
}
|
|
702
|
+
exports.Store = Store;
|
|
703
|
+
exports.arrayMethods = arrayMethods;
|
|
704
|
+
exports.calcDuration = calcDuration;
|
|
705
|
+
exports.createStore = createStore;
|
|
706
|
+
exports.hash = hash;
|
|
707
|
+
exports.makeSelector = makeSelector;
|
|
708
|
+
exports.mapMethods = mapMethods;
|
|
709
|
+
exports.recordMethods = recordMethods;
|
|
710
|
+
exports.setMethods = setMethods;
|
|
711
|
+
exports.trackingProxy = trackingProxy;
|
|
712
|
+
//# sourceMappingURL=hash.cjs.map
|