cross-state 0.33.2 → 0.33.5
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/cache.cjs +94 -118
- package/dist/cjs/cache.cjs.map +1 -1
- package/dist/cjs/immer/index.cjs +3 -21
- package/dist/cjs/immer/index.cjs.map +1 -1
- package/dist/cjs/immer/register.cjs +2 -2
- package/dist/cjs/immer/register.cjs.map +1 -1
- package/dist/cjs/immerMethods.cjs +23 -0
- package/dist/cjs/immerMethods.cjs.map +1 -0
- package/dist/cjs/index.cjs +0 -114
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/react/index.cjs +0 -11
- package/dist/cjs/react/index.cjs.map +1 -1
- package/dist/cjs/store.cjs +319 -306
- package/dist/cjs/store.cjs.map +1 -1
- package/dist/cjs/useCache.cjs +152 -142
- package/dist/cjs/useCache.cjs.map +1 -1
- package/dist/es/cache.mjs +92 -116
- package/dist/es/cache.mjs.map +1 -1
- package/dist/es/immer/index.mjs +3 -21
- package/dist/es/immer/index.mjs.map +1 -1
- package/dist/es/immer/register.mjs +1 -1
- package/dist/es/immerMethods.mjs +24 -0
- package/dist/es/immerMethods.mjs.map +1 -0
- package/dist/es/index.mjs +36 -151
- package/dist/es/index.mjs.map +1 -1
- package/dist/es/react/index.mjs +4 -15
- package/dist/es/react/index.mjs.map +1 -1
- package/dist/es/store.mjs +330 -317
- package/dist/es/store.mjs.map +1 -1
- package/dist/es/useCache.mjs +153 -143
- package/dist/es/useCache.mjs.map +1 -1
- package/dist/types/core/cache.d.ts +9 -18
- package/dist/types/core/commonTypes.d.ts +23 -6
- package/dist/types/core/index.d.ts +1 -2
- package/dist/types/core/store.d.ts +9 -16
- package/dist/types/lib/cacheState.d.ts +1 -0
- package/dist/types/lib/calculatedValue.d.ts +9 -0
- package/dist/types/lib/deferred.d.ts +6 -0
- package/dist/types/lib/disposable.d.ts +3 -0
- package/dist/types/lib/promiseWithState.d.ts +1 -0
- package/dist/types/react/index.d.ts +0 -1
- package/dist/types/react/register.d.ts +2 -2
- package/dist/types/react/scope.d.ts +2 -2
- package/dist/types/sync/sync.d.ts +1 -1
- package/package.json +22 -22
- package/dist/types/core/subscriptionCache.d.ts +0 -62
- package/dist/types/lib/calculationHelper.d.ts +0 -27
- package/dist/types/react/read.d.ts +0 -3
package/dist/cjs/store.cjs
CHANGED
|
@@ -45,55 +45,20 @@ function calcDuration(t) {
|
|
|
45
45
|
return t;
|
|
46
46
|
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;
|
|
47
47
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const internalEqual = (comp) => (a, b) => {
|
|
58
|
-
if (a === b) {
|
|
59
|
-
return true;
|
|
60
|
-
}
|
|
61
|
-
if (a === null || b === null || typeof a !== "object" || typeof b !== "object") {
|
|
62
|
-
return a !== a && b !== b;
|
|
63
|
-
}
|
|
64
|
-
if (a.constructor !== b.constructor) {
|
|
65
|
-
return false;
|
|
66
|
-
}
|
|
67
|
-
if (a.constructor === Object) {
|
|
68
|
-
const entries1 = Object.entries(a);
|
|
69
|
-
const entries2 = Object.entries(b);
|
|
70
|
-
return entries1.length === entries2.length && entries1.every(([key, value]) => comp(value, b[key]));
|
|
71
|
-
}
|
|
72
|
-
if (Array.isArray(a)) {
|
|
73
|
-
return a.length === b.length && a.every((value, i) => comp(value, b[i]));
|
|
74
|
-
}
|
|
75
|
-
if (a instanceof Date) {
|
|
76
|
-
return a.getTime() === b.getTime();
|
|
77
|
-
}
|
|
78
|
-
if (a instanceof RegExp) {
|
|
79
|
-
return a.source === b.source && a.flags === b.flags;
|
|
80
|
-
}
|
|
81
|
-
if (a instanceof Map) {
|
|
82
|
-
return a.size === b.size && [...a.entries()].every(([key, value]) => comp(value, b.get(key)));
|
|
48
|
+
class Deferred extends Promise {
|
|
49
|
+
constructor() {
|
|
50
|
+
const that = {};
|
|
51
|
+
super((resolve, reject) => {
|
|
52
|
+
Object.assign(that, { resolve, reject });
|
|
53
|
+
});
|
|
54
|
+
this.resolve = () => void 0;
|
|
55
|
+
this.reject = () => void 0;
|
|
56
|
+
Object.assign(this, that);
|
|
83
57
|
}
|
|
84
|
-
|
|
85
|
-
return
|
|
58
|
+
static get [Symbol.species]() {
|
|
59
|
+
return Promise;
|
|
86
60
|
}
|
|
87
|
-
|
|
88
|
-
if (a.byteLength !== b.byteLength) {
|
|
89
|
-
return false;
|
|
90
|
-
}
|
|
91
|
-
const a_ = new Int8Array(a.buffer);
|
|
92
|
-
const b_ = new Int8Array(b.buffer);
|
|
93
|
-
return a_.every((value, i) => value === b_[i]);
|
|
94
|
-
}
|
|
95
|
-
return false;
|
|
96
|
-
};
|
|
61
|
+
}
|
|
97
62
|
function queue() {
|
|
98
63
|
const q = [];
|
|
99
64
|
const completionListeners = /* @__PURE__ */ new Set();
|
|
@@ -151,219 +116,214 @@ function queue() {
|
|
|
151
116
|
}
|
|
152
117
|
);
|
|
153
118
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
119
|
+
function defaultEqual(a, b) {
|
|
120
|
+
return a === b;
|
|
121
|
+
}
|
|
122
|
+
function shallowEqual(a, b) {
|
|
123
|
+
return internalEqual(defaultEqual)(a, b);
|
|
124
|
+
}
|
|
125
|
+
function deepEqual(a, b) {
|
|
126
|
+
return internalEqual(deepEqual)(a, b);
|
|
127
|
+
}
|
|
128
|
+
const internalEqual = (comp) => (a, b) => {
|
|
129
|
+
if (a === b) {
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
if (a === null || b === null || typeof a !== "object" || typeof b !== "object") {
|
|
133
|
+
return a !== a && b !== b;
|
|
134
|
+
}
|
|
135
|
+
if (a.constructor !== b.constructor) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
if (a.constructor === Object) {
|
|
139
|
+
const entries1 = Object.entries(a);
|
|
140
|
+
const entries2 = Object.entries(b);
|
|
141
|
+
return entries1.length === entries2.length && entries1.every(([key, value]) => comp(value, b[key]));
|
|
142
|
+
}
|
|
143
|
+
if (Array.isArray(a)) {
|
|
144
|
+
return a.length === b.length && a.every((value, i) => comp(value, b[i]));
|
|
145
|
+
}
|
|
146
|
+
if (a instanceof Date) {
|
|
147
|
+
return a.getTime() === b.getTime();
|
|
148
|
+
}
|
|
149
|
+
if (a instanceof RegExp) {
|
|
150
|
+
return a.source === b.source && a.flags === b.flags;
|
|
151
|
+
}
|
|
152
|
+
if (a instanceof Map) {
|
|
153
|
+
return a.size === b.size && [...a.entries()].every(([key, value]) => comp(value, b.get(key)));
|
|
154
|
+
}
|
|
155
|
+
if (a instanceof Set) {
|
|
156
|
+
return a.size === b.size && [...a.values()].every((value) => b.has(value));
|
|
157
|
+
}
|
|
158
|
+
if (typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView(a)) {
|
|
159
|
+
if (a.byteLength !== b.byteLength) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
const a_ = new Int8Array(a.buffer);
|
|
163
|
+
const b_ = new Int8Array(b.buffer);
|
|
164
|
+
return a_.every((value, i) => value === b_[i]);
|
|
165
|
+
}
|
|
166
|
+
return false;
|
|
167
|
+
};
|
|
168
|
+
class PromiseWithState extends Promise {
|
|
169
|
+
constructor(value, state = { status: "pending" }) {
|
|
170
|
+
super((resolve) => resolve(value));
|
|
171
|
+
this.state = state;
|
|
172
|
+
value.then((value2) => {
|
|
173
|
+
this.state = { status: "value", value: value2 };
|
|
174
|
+
}).catch((error) => {
|
|
175
|
+
this.state = { status: "error", error };
|
|
163
176
|
});
|
|
164
177
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
let isCancled = false;
|
|
173
|
-
const cancelEffect = addEffect(() => {
|
|
174
|
-
isActive = true;
|
|
175
|
-
for (const dep of deps.values()) {
|
|
176
|
-
dep.on();
|
|
177
|
-
}
|
|
178
|
-
return () => {
|
|
179
|
-
isActive = false;
|
|
180
|
-
for (const dep of deps.values()) {
|
|
181
|
-
dep.off();
|
|
182
|
-
}
|
|
183
|
-
if (cancelSubscription) {
|
|
184
|
-
cancelSubscription();
|
|
185
|
-
cancelSubscription = void 0;
|
|
186
|
-
cancel();
|
|
187
|
-
onInvalidate == null ? void 0 : onInvalidate();
|
|
188
|
-
}
|
|
189
|
-
};
|
|
178
|
+
static get [Symbol.species]() {
|
|
179
|
+
return Promise;
|
|
180
|
+
}
|
|
181
|
+
static resolve(value) {
|
|
182
|
+
return new PromiseWithState(Promise.resolve(value), {
|
|
183
|
+
status: "value",
|
|
184
|
+
value
|
|
190
185
|
});
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
186
|
+
}
|
|
187
|
+
static reject(error) {
|
|
188
|
+
return new PromiseWithState(Promise.reject(error), { status: "error", error });
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function calculatedValue(store, notify) {
|
|
192
|
+
let active = false;
|
|
193
|
+
const deps = new Array();
|
|
194
|
+
let value;
|
|
195
|
+
const whenConnected = new Deferred();
|
|
196
|
+
const whenExecuted = new Deferred();
|
|
197
|
+
let cancelConnection;
|
|
198
|
+
const q = queue();
|
|
199
|
+
q(() => whenExecuted);
|
|
200
|
+
const cancelEffect = store.addEffect(() => {
|
|
201
|
+
if (cancelConnection) {
|
|
202
|
+
store.invalidate();
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
active = true;
|
|
206
|
+
for (const dep of deps) {
|
|
207
|
+
dep.on();
|
|
208
|
+
}
|
|
209
|
+
return () => {
|
|
210
|
+
active = false;
|
|
211
|
+
for (const dep of deps) {
|
|
212
|
+
dep.off();
|
|
201
213
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
dep.invalidate();
|
|
214
|
+
cancelConnection == null ? void 0 : cancelConnection();
|
|
215
|
+
if ("state" in store) {
|
|
216
|
+
store.state.set("isConnected", false);
|
|
206
217
|
}
|
|
207
218
|
};
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
return deepEqual(newValue, value);
|
|
215
|
-
};
|
|
216
|
-
const check = () => equals(store.get());
|
|
217
|
-
let sub;
|
|
218
|
-
const dep = {
|
|
219
|
-
on() {
|
|
220
|
-
this.off();
|
|
221
|
-
sub = store.subscribe(
|
|
222
|
-
() => {
|
|
223
|
-
if (sub && !check()) {
|
|
224
|
-
cancel();
|
|
225
|
-
onInvalidate == null ? void 0 : onInvalidate();
|
|
226
|
-
}
|
|
227
|
-
},
|
|
228
|
-
{ runNow: false }
|
|
229
|
-
);
|
|
230
|
-
},
|
|
231
|
-
off() {
|
|
232
|
-
sub == null ? void 0 : sub();
|
|
233
|
-
sub = void 0;
|
|
234
|
-
},
|
|
235
|
-
invalidate() {
|
|
236
|
-
if ("invalidate" in store && store.invalidate instanceof Function) {
|
|
237
|
-
store.invalidate();
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
};
|
|
241
|
-
if (isActive) {
|
|
242
|
-
dep.on();
|
|
243
|
-
}
|
|
244
|
-
checks.push(check);
|
|
245
|
-
deps.set(store, dep);
|
|
246
|
-
return value;
|
|
219
|
+
});
|
|
220
|
+
function use(dep) {
|
|
221
|
+
const value2 = dep.get();
|
|
222
|
+
let cancel;
|
|
223
|
+
const on = () => {
|
|
224
|
+
cancel || (cancel = dep.subscribe(() => store.invalidate(), { runNow: false }));
|
|
247
225
|
};
|
|
248
|
-
const
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
226
|
+
const off = () => {
|
|
227
|
+
cancel == null ? void 0 : cancel();
|
|
228
|
+
cancel = void 0;
|
|
229
|
+
};
|
|
230
|
+
deps.push({ store: dep, value: value2, on, off });
|
|
231
|
+
if (active) {
|
|
232
|
+
on();
|
|
233
|
+
}
|
|
234
|
+
return value2;
|
|
235
|
+
}
|
|
236
|
+
async function connect(connection) {
|
|
237
|
+
if (!active) {
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
const actions = {
|
|
241
|
+
set(value2) {
|
|
242
|
+
q(() => {
|
|
243
|
+
store.set(value2);
|
|
244
|
+
});
|
|
245
|
+
},
|
|
246
|
+
updateValue(update) {
|
|
247
|
+
q(async () => {
|
|
248
|
+
if (update instanceof Function) {
|
|
249
|
+
update = update(await value);
|
|
250
|
+
}
|
|
251
|
+
if (update instanceof Promise) {
|
|
252
|
+
update = await update;
|
|
266
253
|
}
|
|
267
|
-
|
|
254
|
+
value = PromiseWithState.resolve(update);
|
|
255
|
+
notify();
|
|
256
|
+
});
|
|
257
|
+
},
|
|
258
|
+
updateError(error) {
|
|
259
|
+
q(() => {
|
|
260
|
+
store.set(Promise.reject(error));
|
|
261
|
+
});
|
|
262
|
+
},
|
|
263
|
+
updateIsConnected(isConnected) {
|
|
264
|
+
if (isConnected) {
|
|
265
|
+
whenConnected.resolve();
|
|
268
266
|
}
|
|
267
|
+
q(() => {
|
|
268
|
+
if ("state" in store) {
|
|
269
|
+
store.state.set("isConnected", isConnected);
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
},
|
|
273
|
+
close() {
|
|
274
|
+
store.invalidate();
|
|
269
275
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
});
|
|
274
|
-
const updateError = (error) => q(() => {
|
|
275
|
-
if (!isCancled) {
|
|
276
|
-
onError == null ? void 0 : onError(error);
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
const updateConnectionState = (state) => q(() => {
|
|
280
|
-
if (!isCancled) {
|
|
281
|
-
onConnectionState == null ? void 0 : onConnectionState(state);
|
|
282
|
-
}
|
|
283
|
-
});
|
|
284
|
-
let cancelSubscription;
|
|
285
|
-
try {
|
|
286
|
-
cancelSubscription = calculate({ use, updateValue, updateError, updateConnectionState });
|
|
287
|
-
} catch (error) {
|
|
288
|
-
onError == null ? void 0 : onError(error);
|
|
289
|
-
}
|
|
290
|
-
this.current = { cancel, check: checkAll, invalidateDependencies };
|
|
276
|
+
};
|
|
277
|
+
cancelConnection = connection(actions);
|
|
278
|
+
return whenConnected;
|
|
291
279
|
}
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
(
|
|
280
|
+
value = store.getter instanceof Function ? store.getter({ use, connect }) : store.getter;
|
|
281
|
+
if (value instanceof Promise) {
|
|
282
|
+
value.finally(() => whenExecuted.resolve()).catch(() => void 0);
|
|
295
283
|
}
|
|
296
|
-
check() {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
}
|
|
300
|
-
checkOrExecute() {
|
|
301
|
-
if (this.current) {
|
|
302
|
-
this.check();
|
|
303
|
-
} else {
|
|
304
|
-
this.execute();
|
|
284
|
+
function check() {
|
|
285
|
+
if (active) {
|
|
286
|
+
return;
|
|
305
287
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
}
|
|
312
|
-
class Callable extends Function {
|
|
313
|
-
constructor(_call) {
|
|
314
|
-
super("...args", "return this._call(...args)");
|
|
315
|
-
this._call = _call;
|
|
316
|
-
return this.bind(this);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
function debounce(action, options) {
|
|
320
|
-
const wait = typeof options === "object" && "wait" in options ? calcDuration(options.wait) : calcDuration(options);
|
|
321
|
-
const maxWait = typeof options === "object" && "maxWait" in options && options.maxWait !== void 0 ? calcDuration(options.maxWait) : void 0;
|
|
322
|
-
let run;
|
|
323
|
-
let timeout;
|
|
324
|
-
let timeoutStarted;
|
|
325
|
-
function flush() {
|
|
326
|
-
if (timeout !== void 0) {
|
|
327
|
-
clearTimeout(timeout);
|
|
288
|
+
for (const dep of deps) {
|
|
289
|
+
if (!deepEqual(dep.store.get(), dep.value)) {
|
|
290
|
+
store.invalidate();
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
328
293
|
}
|
|
329
|
-
run == null ? void 0 : run();
|
|
330
294
|
}
|
|
331
|
-
function
|
|
332
|
-
|
|
333
|
-
|
|
295
|
+
function stop() {
|
|
296
|
+
cancelEffect();
|
|
297
|
+
cancelConnection == null ? void 0 : cancelConnection();
|
|
298
|
+
if (cancelConnection) {
|
|
299
|
+
whenConnected.reject();
|
|
300
|
+
whenExecuted.reject();
|
|
301
|
+
} else {
|
|
302
|
+
whenConnected.resolve();
|
|
303
|
+
whenExecuted.resolve();
|
|
334
304
|
}
|
|
335
|
-
run = void 0;
|
|
336
|
-
timeout = void 0;
|
|
337
|
-
timeoutStarted = void 0;
|
|
338
305
|
}
|
|
339
|
-
function
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
function debounce2(...args) {
|
|
343
|
-
const now = Date.now();
|
|
344
|
-
timeoutStarted ?? (timeoutStarted = now);
|
|
345
|
-
const deadline = Math.min(
|
|
346
|
-
//
|
|
347
|
-
now + wait,
|
|
348
|
-
timeoutStarted + (maxWait ?? Number.POSITIVE_INFINITY)
|
|
349
|
-
);
|
|
350
|
-
if (timeout !== void 0) {
|
|
351
|
-
clearTimeout(timeout);
|
|
306
|
+
function invalidateDependencies(recursive) {
|
|
307
|
+
for (const dep of deps) {
|
|
308
|
+
dep.store.invalidate(recursive);
|
|
352
309
|
}
|
|
353
|
-
run = () => {
|
|
354
|
-
run = void 0;
|
|
355
|
-
timeout = void 0;
|
|
356
|
-
timeoutStarted = void 0;
|
|
357
|
-
action(...args);
|
|
358
|
-
};
|
|
359
|
-
timeout = setTimeout(run, deadline - now);
|
|
360
310
|
}
|
|
361
|
-
return
|
|
311
|
+
return {
|
|
312
|
+
get value() {
|
|
313
|
+
return value;
|
|
314
|
+
},
|
|
315
|
+
check,
|
|
316
|
+
stop,
|
|
317
|
+
invalidateDependencies
|
|
318
|
+
};
|
|
362
319
|
}
|
|
363
|
-
function
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
320
|
+
function staticValue(value) {
|
|
321
|
+
return {
|
|
322
|
+
value,
|
|
323
|
+
check: () => void 0,
|
|
324
|
+
stop: () => void 0,
|
|
325
|
+
invalidateDependencies: () => void 0
|
|
326
|
+
};
|
|
367
327
|
}
|
|
368
328
|
function flatClone(object) {
|
|
369
329
|
if (object instanceof Map) {
|
|
@@ -469,6 +429,83 @@ function makeSelector(selector) {
|
|
|
469
429
|
}
|
|
470
430
|
return (x) => get(x, selector);
|
|
471
431
|
}
|
|
432
|
+
class Callable extends Function {
|
|
433
|
+
constructor(_call) {
|
|
434
|
+
super("...args", "return this._call(...args)");
|
|
435
|
+
this._call = _call;
|
|
436
|
+
return this.bind(this);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
function debounce(action, options) {
|
|
440
|
+
const wait = typeof options === "object" && "wait" in options ? calcDuration(options.wait) : calcDuration(options);
|
|
441
|
+
const maxWait = typeof options === "object" && "maxWait" in options && options.maxWait !== void 0 ? calcDuration(options.maxWait) : void 0;
|
|
442
|
+
let run;
|
|
443
|
+
let timeout;
|
|
444
|
+
let timeoutStarted;
|
|
445
|
+
function flush() {
|
|
446
|
+
if (timeout !== void 0) {
|
|
447
|
+
clearTimeout(timeout);
|
|
448
|
+
}
|
|
449
|
+
run == null ? void 0 : run();
|
|
450
|
+
}
|
|
451
|
+
function cancel() {
|
|
452
|
+
if (timeout !== void 0) {
|
|
453
|
+
clearTimeout(timeout);
|
|
454
|
+
}
|
|
455
|
+
run = void 0;
|
|
456
|
+
timeout = void 0;
|
|
457
|
+
timeoutStarted = void 0;
|
|
458
|
+
}
|
|
459
|
+
function isScheduled() {
|
|
460
|
+
return timeout !== void 0;
|
|
461
|
+
}
|
|
462
|
+
function debounce2(...args) {
|
|
463
|
+
const now = Date.now();
|
|
464
|
+
timeoutStarted ?? (timeoutStarted = now);
|
|
465
|
+
const deadline = Math.min(
|
|
466
|
+
//
|
|
467
|
+
now + wait,
|
|
468
|
+
timeoutStarted + (maxWait ?? Number.POSITIVE_INFINITY)
|
|
469
|
+
);
|
|
470
|
+
if (timeout !== void 0) {
|
|
471
|
+
clearTimeout(timeout);
|
|
472
|
+
}
|
|
473
|
+
run = () => {
|
|
474
|
+
run = void 0;
|
|
475
|
+
timeout = void 0;
|
|
476
|
+
timeoutStarted = void 0;
|
|
477
|
+
action(...args);
|
|
478
|
+
};
|
|
479
|
+
timeout = setTimeout(run, deadline - now);
|
|
480
|
+
}
|
|
481
|
+
return Object.assign(debounce2, { flush, cancel, isScheduled });
|
|
482
|
+
}
|
|
483
|
+
function forwardError(error) {
|
|
484
|
+
setTimeout(() => {
|
|
485
|
+
throw error;
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
class PromiseCancelError extends Error {
|
|
489
|
+
constructor() {
|
|
490
|
+
super("cancelled");
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
class PromiseWithCancel extends Promise {
|
|
494
|
+
constructor(executor) {
|
|
495
|
+
autobind(PromiseWithCancel);
|
|
496
|
+
const abortController = new AbortController();
|
|
497
|
+
super((resolve, reject) => {
|
|
498
|
+
executor(resolve, reject, abortController.signal);
|
|
499
|
+
abortController.signal.addEventListener("abort", (e) => {
|
|
500
|
+
reject(abortController.signal.reason);
|
|
501
|
+
});
|
|
502
|
+
});
|
|
503
|
+
this.abortController = abortController;
|
|
504
|
+
}
|
|
505
|
+
cancel(reason = new PromiseCancelError()) {
|
|
506
|
+
this.abortController.abort(reason);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
472
509
|
function createArrayAction(prop) {
|
|
473
510
|
return function arrayAction(...args) {
|
|
474
511
|
const newArray = this.get().slice();
|
|
@@ -542,26 +579,11 @@ function throttle(action, duration) {
|
|
|
542
579
|
}, dt);
|
|
543
580
|
};
|
|
544
581
|
}
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
class PromiseWithCancel extends Promise {
|
|
551
|
-
constructor(executor) {
|
|
552
|
-
autobind(PromiseWithCancel);
|
|
553
|
-
const abortController = new AbortController();
|
|
554
|
-
super((resolve, reject) => {
|
|
555
|
-
executor(resolve, reject, abortController.signal);
|
|
556
|
-
abortController.signal.addEventListener("abort", (e) => {
|
|
557
|
-
reject(abortController.signal.reason);
|
|
558
|
-
});
|
|
559
|
-
});
|
|
560
|
-
this.abortController = abortController;
|
|
561
|
-
}
|
|
562
|
-
cancel(reason = new PromiseCancelError()) {
|
|
563
|
-
this.abortController.abort(reason);
|
|
564
|
-
}
|
|
582
|
+
function disposable(dispose) {
|
|
583
|
+
return Object.assign(
|
|
584
|
+
dispose,
|
|
585
|
+
Symbol.dispose ? { [Symbol.dispose]: dispose } : {}
|
|
586
|
+
);
|
|
565
587
|
}
|
|
566
588
|
function noop() {
|
|
567
589
|
return void 0;
|
|
@@ -576,35 +598,18 @@ class Store extends Callable {
|
|
|
576
598
|
this.listeners = /* @__PURE__ */ new Map();
|
|
577
599
|
this.effects = /* @__PURE__ */ new Map();
|
|
578
600
|
this.notifyId = {};
|
|
579
|
-
this.calculationHelper = new CalculationHelper({
|
|
580
|
-
calculate: (helpers) => {
|
|
581
|
-
if (this.getter instanceof Function) {
|
|
582
|
-
const value = this.getter.apply(helpers, [helpers]);
|
|
583
|
-
this._value = { v: value };
|
|
584
|
-
this.notify();
|
|
585
|
-
}
|
|
586
|
-
},
|
|
587
|
-
addEffect: (effect) => this.addEffect(effect, this.options.retain),
|
|
588
|
-
getValue: () => {
|
|
589
|
-
var _a;
|
|
590
|
-
return (_a = this._value) == null ? void 0 : _a.v;
|
|
591
|
-
},
|
|
592
|
-
onInvalidate: () => this.reset()
|
|
593
|
-
});
|
|
594
601
|
autobind(Store);
|
|
595
|
-
if (!(getter instanceof Function)) {
|
|
596
|
-
this._value = { v: getter };
|
|
597
|
-
}
|
|
598
602
|
}
|
|
599
603
|
get() {
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
+
var _a;
|
|
605
|
+
(_a = this.calculatedValue) == null ? void 0 : _a.check();
|
|
606
|
+
if (!this.calculatedValue) {
|
|
607
|
+
this.calculatedValue = calculatedValue(this, this.notify);
|
|
604
608
|
}
|
|
605
|
-
return this.
|
|
609
|
+
return this.calculatedValue.value;
|
|
606
610
|
}
|
|
607
611
|
set(...args) {
|
|
612
|
+
var _a;
|
|
608
613
|
const path = args.length > 1 ? args[0] : [];
|
|
609
614
|
let update = args.length > 1 ? args[1] : args[0];
|
|
610
615
|
if (update instanceof Function) {
|
|
@@ -619,17 +624,20 @@ class Store extends Callable {
|
|
|
619
624
|
this.derivedFrom.updater(update);
|
|
620
625
|
return;
|
|
621
626
|
}
|
|
622
|
-
this.
|
|
627
|
+
(_a = this.calculatedValue) == null ? void 0 : _a.stop();
|
|
628
|
+
this.calculatedValue = staticValue(update);
|
|
623
629
|
this.notify();
|
|
624
630
|
}
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
if (
|
|
628
|
-
this.
|
|
631
|
+
invalidate(recursive) {
|
|
632
|
+
var _a, _b;
|
|
633
|
+
if (recursive) {
|
|
634
|
+
(_a = this.calculatedValue) == null ? void 0 : _a.invalidateDependencies(recursive);
|
|
629
635
|
}
|
|
636
|
+
(_b = this.calculatedValue) == null ? void 0 : _b.stop();
|
|
637
|
+
this.calculatedValue = void 0;
|
|
638
|
+
this.notify();
|
|
630
639
|
}
|
|
631
640
|
subscribe(listener, options) {
|
|
632
|
-
var _a;
|
|
633
641
|
const {
|
|
634
642
|
passive,
|
|
635
643
|
runNow = true,
|
|
@@ -637,22 +645,20 @@ class Store extends Callable {
|
|
|
637
645
|
debounce: debounceOption,
|
|
638
646
|
equals = deepEqual
|
|
639
647
|
} = options ?? {};
|
|
640
|
-
let compareToValue = (_a = this._value) == null ? void 0 : _a.v;
|
|
641
648
|
let previousValue;
|
|
642
|
-
let
|
|
643
|
-
|
|
644
|
-
|
|
649
|
+
let innerListener = () => {
|
|
650
|
+
var _a;
|
|
651
|
+
const value = passive ? this.calculatedValue : { value: this.get() };
|
|
652
|
+
if (!value) {
|
|
645
653
|
return;
|
|
646
654
|
}
|
|
647
|
-
|
|
648
|
-
if (!force && (isInitializing || equals(value, compareToValue))) {
|
|
655
|
+
if (previousValue && equals(value.value, previousValue.value)) {
|
|
649
656
|
return;
|
|
650
657
|
}
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
previousValue = value;
|
|
658
|
+
const _previousValue = previousValue == null ? void 0 : previousValue.value;
|
|
659
|
+
previousValue = this.calculatedValue && { value: (_a = this.calculatedValue) == null ? void 0 : _a.value };
|
|
654
660
|
try {
|
|
655
|
-
listener(value, _previousValue);
|
|
661
|
+
listener(value.value, _previousValue);
|
|
656
662
|
} catch (error) {
|
|
657
663
|
forwardError(error);
|
|
658
664
|
}
|
|
@@ -667,15 +673,16 @@ class Store extends Callable {
|
|
|
667
673
|
this.onSubscribe();
|
|
668
674
|
}
|
|
669
675
|
if (runNow) {
|
|
670
|
-
innerListener(
|
|
676
|
+
innerListener();
|
|
677
|
+
} else {
|
|
678
|
+
previousValue = passive ? this.calculatedValue && { value: this.calculatedValue.value } : { value: this.get() };
|
|
671
679
|
}
|
|
672
|
-
|
|
673
|
-
return () => {
|
|
680
|
+
return disposable(() => {
|
|
674
681
|
this.listeners.delete(innerListener);
|
|
675
682
|
if (!passive) {
|
|
676
683
|
this.onUnsubscribe();
|
|
677
684
|
}
|
|
678
|
-
};
|
|
685
|
+
});
|
|
679
686
|
}
|
|
680
687
|
once(...args) {
|
|
681
688
|
const condition = args[0] instanceof Function ? args[0] : Boolean;
|
|
@@ -756,7 +763,7 @@ class Store extends Callable {
|
|
|
756
763
|
* @returns
|
|
757
764
|
* The effect can return a teardown callback, which will be executed when the last subscription is removed and potentially the ratain time has passed.
|
|
758
765
|
*/
|
|
759
|
-
addEffect(effect, retain) {
|
|
766
|
+
addEffect(effect, retain = this.options.retain) {
|
|
760
767
|
this.effects.set(effect, {
|
|
761
768
|
handle: this.isActive() ? effect() ?? noop : void 0,
|
|
762
769
|
retain: retain !== void 0 ? calcDuration(retain) : void 0
|
|
@@ -830,8 +837,8 @@ class Store extends Callable {
|
|
|
830
837
|
}
|
|
831
838
|
}
|
|
832
839
|
}
|
|
833
|
-
const defaultOptions = {};
|
|
834
840
|
function create(initialState, options) {
|
|
841
|
+
options = { ...createStore.defaultOptions, ...options };
|
|
835
842
|
const store = new Store(initialState, options);
|
|
836
843
|
if (initialState instanceof Function) {
|
|
837
844
|
return store;
|
|
@@ -851,11 +858,17 @@ function create(initialState, options) {
|
|
|
851
858
|
);
|
|
852
859
|
return Object.assign(store, boundMethods);
|
|
853
860
|
}
|
|
854
|
-
const createStore = /* @__PURE__ */ Object.assign(create, {
|
|
861
|
+
const createStore = /* @__PURE__ */ Object.assign(create, {
|
|
862
|
+
defaultOptions: {
|
|
863
|
+
retain: { seconds: 1 }
|
|
864
|
+
}
|
|
865
|
+
});
|
|
866
|
+
exports.PromiseWithState = PromiseWithState;
|
|
855
867
|
exports.Store = Store;
|
|
856
868
|
exports.arrayMethods = arrayMethods;
|
|
857
869
|
exports.autobind = autobind;
|
|
858
870
|
exports.calcDuration = calcDuration;
|
|
871
|
+
exports.calculatedValue = calculatedValue;
|
|
859
872
|
exports.castArrayPath = castArrayPath;
|
|
860
873
|
exports.createStore = createStore;
|
|
861
874
|
exports.debounce = debounce;
|