cross-state 0.6.10 → 0.7.1

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