cross-state 0.37.12 → 0.37.13

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 (61) hide show
  1. package/dist/cjs/diff.cjs +6 -2
  2. package/dist/cjs/diff.cjs.map +1 -1
  3. package/dist/cjs/hash.cjs +5 -2
  4. package/dist/cjs/hash.cjs.map +1 -1
  5. package/dist/cjs/index.cjs +18 -13
  6. package/dist/cjs/index.cjs.map +1 -1
  7. package/dist/cjs/mutative/index.cjs +8 -3
  8. package/dist/cjs/mutative/index.cjs.map +1 -1
  9. package/dist/cjs/mutative/register.cjs +5 -3
  10. package/dist/cjs/mutative/register.cjs.map +1 -1
  11. package/dist/cjs/mutativeMethods.cjs +5 -2
  12. package/dist/cjs/mutativeMethods.cjs.map +1 -1
  13. package/dist/cjs/patches/index.cjs +7 -3
  14. package/dist/cjs/patches/index.cjs.map +1 -1
  15. package/dist/cjs/patches/register.cjs +5 -3
  16. package/dist/cjs/patches/register.cjs.map +1 -1
  17. package/dist/cjs/propAccess.cjs +6 -1
  18. package/dist/cjs/propAccess.cjs.map +1 -1
  19. package/dist/cjs/react/index.cjs +38 -36
  20. package/dist/cjs/react/index.cjs.map +1 -1
  21. package/dist/cjs/react/register.cjs +6 -4
  22. package/dist/cjs/react/register.cjs.map +1 -1
  23. package/dist/cjs/scope.cjs +32 -43
  24. package/dist/cjs/scope.cjs.map +1 -1
  25. package/dist/cjs/store.cjs +53 -46
  26. package/dist/cjs/store.cjs.map +1 -1
  27. package/dist/cjs/storeMethods.cjs +467 -341
  28. package/dist/cjs/storeMethods.cjs.map +1 -1
  29. package/dist/cjs/urlStore.cjs +9 -6
  30. package/dist/cjs/urlStore.cjs.map +1 -1
  31. package/dist/es/diff.mjs +5 -5
  32. package/dist/es/diff.mjs.map +1 -1
  33. package/dist/es/hash.mjs +4 -4
  34. package/dist/es/hash.mjs.map +1 -1
  35. package/dist/es/index.mjs +17 -42
  36. package/dist/es/index.mjs.map +1 -1
  37. package/dist/es/mutative/index.mjs +1 -4
  38. package/dist/es/mutative/index.mjs.map +1 -1
  39. package/dist/es/mutative/register.mjs +3 -2
  40. package/dist/es/mutative/register.mjs.map +1 -1
  41. package/dist/es/mutativeMethods.mjs +4 -4
  42. package/dist/es/mutativeMethods.mjs.map +1 -1
  43. package/dist/es/patches/index.mjs +4 -4
  44. package/dist/es/patches/index.mjs.map +1 -1
  45. package/dist/es/patches/register.mjs +3 -2
  46. package/dist/es/patches/register.mjs.map +1 -1
  47. package/dist/es/propAccess.mjs +5 -12
  48. package/dist/es/propAccess.mjs.map +1 -1
  49. package/dist/es/react/index.mjs +36 -52
  50. package/dist/es/react/index.mjs.map +1 -1
  51. package/dist/es/react/register.mjs +4 -3
  52. package/dist/es/react/register.mjs.map +1 -1
  53. package/dist/es/scope.mjs +31 -52
  54. package/dist/es/scope.mjs.map +1 -1
  55. package/dist/es/store.mjs +52 -62
  56. package/dist/es/store.mjs.map +1 -1
  57. package/dist/es/storeMethods.mjs +455 -341
  58. package/dist/es/storeMethods.mjs.map +1 -1
  59. package/dist/es/urlStore.mjs +5 -7
  60. package/dist/es/urlStore.mjs.map +1 -1
  61. package/package.json +1 -1
package/dist/es/store.mjs CHANGED
@@ -1,13 +1,13 @@
1
- import { d as deepEqual, g as get, s as set, a as isObject } from "./propAccess.mjs";
1
+ import { d as deepEqual, g as get, s as set, a as isObject } from './propAccess.mjs';
2
+
2
3
  const marker = Symbol("autobind");
3
4
  const autobind = (_class) => {
4
- var _a;
5
5
  for (const key of Reflect.ownKeys(_class.prototype)) {
6
6
  if (key === "constructor") {
7
7
  continue;
8
8
  }
9
9
  const descriptor = Reflect.getOwnPropertyDescriptor(_class.prototype, key);
10
- let method = ((_a = descriptor == null ? void 0 : descriptor.get) == null ? void 0 : _a.call(descriptor)) ?? (descriptor == null ? void 0 : descriptor.value);
10
+ let method = descriptor?.get?.() ?? descriptor?.value;
11
11
  let isBinding = false;
12
12
  if (typeof method !== "function" || method[marker]) {
13
13
  continue;
@@ -37,11 +37,13 @@ const autobind = (_class) => {
37
37
  }
38
38
  return _class;
39
39
  };
40
+
40
41
  function calcDuration(t) {
41
42
  if (typeof t === "number")
42
43
  return t;
43
44
  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;
44
45
  }
46
+
45
47
  class Deferred extends Promise {
46
48
  constructor() {
47
49
  const that = {};
@@ -56,9 +58,11 @@ class Deferred extends Promise {
56
58
  return Promise;
57
59
  }
58
60
  }
61
+
59
62
  function isPromise(value) {
60
63
  return typeof value === "object" && value !== null && "then" in value && typeof value.then === "function";
61
64
  }
65
+
62
66
  function queue() {
63
67
  const q = [];
64
68
  const completionListeners = /* @__PURE__ */ new Set();
@@ -116,6 +120,7 @@ function queue() {
116
120
  }
117
121
  );
118
122
  }
123
+
119
124
  class PromiseWithState extends Promise {
120
125
  constructor(value, state = { status: "pending" }) {
121
126
  super((resolve) => resolve(value));
@@ -140,6 +145,7 @@ class PromiseWithState extends Promise {
140
145
  return new PromiseWithState(Promise.reject(error), { status: "error", error });
141
146
  }
142
147
  }
148
+
143
149
  function calculatedValue(store, notify) {
144
150
  let active = false;
145
151
  const deps = new Array();
@@ -160,14 +166,13 @@ function calculatedValue(store, notify) {
160
166
  dep.on();
161
167
  }
162
168
  return () => {
163
- var _a;
164
169
  active = false;
165
170
  for (const dep of deps) {
166
171
  dep.off();
167
172
  }
168
173
  if (connection) {
169
174
  connection.active = false;
170
- (_a = connection.cancel) == null ? void 0 : _a.call(connection);
175
+ connection.cancel?.();
171
176
  q.clear();
172
177
  if ("state" in store) {
173
178
  store.state.set((state) => ({
@@ -183,10 +188,10 @@ function calculatedValue(store, notify) {
183
188
  const value2 = dep.get();
184
189
  let cancel;
185
190
  const on = () => {
186
- cancel || (cancel = dep.subscribe(() => store.invalidate(), { runNow: false }));
191
+ cancel ||= dep.subscribe(() => store.invalidate(), { runNow: false });
187
192
  };
188
193
  const off = () => {
189
- cancel == null ? void 0 : cancel();
194
+ cancel?.();
190
195
  cancel = void 0;
191
196
  };
192
197
  deps.push({ store: dep, value: value2, on, off });
@@ -202,20 +207,20 @@ function calculatedValue(store, notify) {
202
207
  }
203
208
  const actions = {
204
209
  set(_value) {
205
- (connection == null ? void 0 : connection.active) && q(() => {
210
+ connection?.active && q(() => {
206
211
  value = _value;
207
212
  notify();
208
213
  });
209
214
  },
210
215
  updateValue(update) {
211
- (connection == null ? void 0 : connection.active) && q(async () => {
216
+ connection?.active && q(async () => {
212
217
  if (update instanceof Function) {
213
218
  update = update(await value);
214
219
  }
215
220
  if (isPromise(update)) {
216
221
  update = await update;
217
222
  }
218
- if (!(connection == null ? void 0 : connection.active)) {
223
+ if (!connection?.active) {
219
224
  return;
220
225
  }
221
226
  value = PromiseWithState.resolve(update);
@@ -223,13 +228,13 @@ function calculatedValue(store, notify) {
223
228
  });
224
229
  },
225
230
  updateError(error) {
226
- (connection == null ? void 0 : connection.active) && q(() => {
231
+ connection?.active && q(() => {
227
232
  value = PromiseWithState.reject(error);
228
233
  notify();
229
234
  });
230
235
  },
231
236
  updateIsConnected(isConnected) {
232
- if (!(connection == null ? void 0 : connection.active)) {
237
+ if (!connection?.active) {
233
238
  return;
234
239
  }
235
240
  if (isConnected) {
@@ -242,13 +247,13 @@ function calculatedValue(store, notify) {
242
247
  });
243
248
  },
244
249
  close() {
245
- (connection == null ? void 0 : connection.active) && store.invalidate();
250
+ connection?.active && store.invalidate();
246
251
  }
247
252
  };
248
253
  connection = { active: true };
249
254
  let _cancel = createConnection(actions);
250
255
  connection.cancel = () => {
251
- _cancel == null ? void 0 : _cancel();
256
+ _cancel?.();
252
257
  _cancel = void 0;
253
258
  };
254
259
  if (!connection.active) {
@@ -274,14 +279,13 @@ function calculatedValue(store, notify) {
274
279
  }
275
280
  }
276
281
  function stop() {
277
- var _a;
278
282
  cancelEffect();
279
283
  whenExecuted.resolve();
280
284
  whenConnected.resolve();
281
285
  ac.abort();
282
286
  if (connection) {
283
287
  connection.active = false;
284
- (_a = connection.cancel) == null ? void 0 : _a.call(connection);
288
+ connection.cancel?.();
285
289
  q.clear();
286
290
  }
287
291
  }
@@ -307,6 +311,7 @@ function staticValue(value) {
307
311
  invalidateDependencies: () => void 0
308
312
  };
309
313
  }
314
+
310
315
  function makeSelector(selector) {
311
316
  if (!selector) {
312
317
  return (x) => x;
@@ -316,6 +321,7 @@ function makeSelector(selector) {
316
321
  }
317
322
  return (x) => get(x, selector);
318
323
  }
324
+
319
325
  class Callable extends Function {
320
326
  constructor(_call) {
321
327
  super("...args", "return this._call(...args)");
@@ -323,6 +329,7 @@ class Callable extends Function {
323
329
  return this.bind(this);
324
330
  }
325
331
  }
332
+
326
333
  function debounce(action, options) {
327
334
  const wait = typeof options === "object" && "wait" in options ? calcDuration(options.wait) : calcDuration(options);
328
335
  const maxWait = typeof options === "object" && "maxWait" in options && options.maxWait !== void 0 ? calcDuration(options.maxWait) : void 0;
@@ -333,7 +340,7 @@ function debounce(action, options) {
333
340
  if (timeout !== void 0) {
334
341
  clearTimeout(timeout);
335
342
  }
336
- run == null ? void 0 : run();
343
+ run?.();
337
344
  }
338
345
  function cancel() {
339
346
  if (timeout !== void 0) {
@@ -348,7 +355,7 @@ function debounce(action, options) {
348
355
  }
349
356
  function debounce2(...args) {
350
357
  const now = Date.now();
351
- timeoutStarted ?? (timeoutStarted = now);
358
+ timeoutStarted ??= now;
352
359
  const deadline = Math.min(
353
360
  //
354
361
  now + wait,
@@ -367,24 +374,28 @@ function debounce(action, options) {
367
374
  }
368
375
  return Object.assign(debounce2, { flush, cancel, isScheduled });
369
376
  }
377
+
370
378
  function disposable(dispose) {
371
379
  return Object.assign(
372
380
  dispose,
373
381
  Symbol.dispose ? { [Symbol.dispose]: dispose } : {}
374
382
  );
375
383
  }
384
+
376
385
  function forwardError(error) {
377
386
  setTimeout(() => {
378
387
  throw error;
379
388
  });
380
389
  }
390
+
381
391
  class PromiseCancelError extends Error {
382
392
  constructor() {
383
393
  super("cancelled");
384
394
  }
385
395
  }
386
- const _PromiseWithCancel = class _PromiseWithCancel extends Promise {
396
+ class PromiseWithCancel extends Promise {
387
397
  constructor(executor) {
398
+ autobind(PromiseWithCancel);
388
399
  const abortController = new AbortController();
389
400
  super((resolve, reject) => {
390
401
  executor(resolve, reject, abortController.signal);
@@ -397,9 +408,8 @@ const _PromiseWithCancel = class _PromiseWithCancel extends Promise {
397
408
  cancel(reason = new PromiseCancelError()) {
398
409
  this.abortController.abort(reason);
399
410
  }
400
- };
401
- /* @__PURE__ */ autobind(_PromiseWithCancel);
402
- let PromiseWithCancel = _PromiseWithCancel;
411
+ }
412
+
403
413
  function createArrayAction(prop) {
404
414
  return function arrayAction(...args) {
405
415
  const newArray = this.get().slice();
@@ -453,6 +463,7 @@ const setMethods = {
453
463
  this.set(/* @__PURE__ */ new Set());
454
464
  }
455
465
  };
466
+
456
467
  function throttle(action, duration) {
457
468
  const ms = calcDuration(duration);
458
469
  let t = 0;
@@ -473,10 +484,11 @@ function throttle(action, duration) {
473
484
  }, dt);
474
485
  };
475
486
  }
487
+
476
488
  function noop() {
477
489
  return void 0;
478
490
  }
479
- const _Store = class _Store extends Callable {
491
+ class Store extends Callable {
480
492
  constructor(getter, options = {}, derivedFrom, _call = () => void 0) {
481
493
  super(_call);
482
494
  this.getter = getter;
@@ -486,20 +498,19 @@ const _Store = class _Store extends Callable {
486
498
  this.listeners = /* @__PURE__ */ new Map();
487
499
  this.effects = /* @__PURE__ */ new Map();
488
500
  this.notifyId = {};
501
+ autobind(Store);
489
502
  if (typeof getter !== "function") {
490
503
  this.calculatedValue = this.defaultValue = staticValue(getter);
491
504
  }
492
505
  }
493
506
  get() {
494
- var _a;
495
- (_a = this.calculatedValue) == null ? void 0 : _a.check();
507
+ this.calculatedValue?.check();
496
508
  if (!this.calculatedValue) {
497
509
  this.calculatedValue = calculatedValue(this, this.notify);
498
510
  }
499
511
  return this.calculatedValue.value;
500
512
  }
501
513
  set(...args) {
502
- var _a;
503
514
  const path = args.length > 1 ? args[0] : [];
504
515
  let update = args.length > 1 ? args[1] : args[0];
505
516
  if (update instanceof Function) {
@@ -514,16 +525,15 @@ const _Store = class _Store extends Callable {
514
525
  this.derivedFrom.updater(update);
515
526
  return;
516
527
  }
517
- (_a = this.calculatedValue) == null ? void 0 : _a.stop();
528
+ this.calculatedValue?.stop();
518
529
  this.calculatedValue = staticValue(update);
519
530
  this.notify();
520
531
  }
521
532
  invalidate(recursive) {
522
- var _a, _b;
523
533
  if (recursive) {
524
- (_a = this.calculatedValue) == null ? void 0 : _a.invalidateDependencies(recursive);
534
+ this.calculatedValue?.invalidateDependencies(recursive);
525
535
  }
526
- (_b = this.calculatedValue) == null ? void 0 : _b.stop();
536
+ this.calculatedValue?.stop();
527
537
  this.calculatedValue = this.defaultValue;
528
538
  this.notify();
529
539
  }
@@ -538,7 +548,6 @@ const _Store = class _Store extends Callable {
538
548
  let isSetup = false;
539
549
  let previousValue;
540
550
  let innerListener = () => {
541
- var _a;
542
551
  if (!isSetup) {
543
552
  return;
544
553
  }
@@ -549,8 +558,8 @@ const _Store = class _Store extends Callable {
549
558
  if (previousValue && equals(value.value, previousValue.value)) {
550
559
  return;
551
560
  }
552
- const _previousValue = previousValue == null ? void 0 : previousValue.value;
553
- previousValue = this.calculatedValue && { value: (_a = this.calculatedValue) == null ? void 0 : _a.value };
561
+ const _previousValue = previousValue?.value;
562
+ previousValue = this.calculatedValue && { value: this.calculatedValue?.value };
554
563
  try {
555
564
  listener(value.value, _previousValue);
556
565
  } catch (error) {
@@ -583,7 +592,6 @@ const _Store = class _Store extends Callable {
583
592
  const condition = args[0] instanceof Function ? args[0] : Boolean;
584
593
  const options = args[0] instanceof Function ? args[1] : args[0];
585
594
  return new PromiseWithCancel((resolve, reject, signal) => {
586
- var _a;
587
595
  let stopped = false;
588
596
  let timer;
589
597
  const cancel = this.subscribe(
@@ -604,12 +612,11 @@ const _Store = class _Store extends Callable {
604
612
  return;
605
613
  }
606
614
  signal.addEventListener("abort", cancel);
607
- (_a = options == null ? void 0 : options.signal) == null ? void 0 : _a.addEventListener("abort", () => {
608
- var _a2;
615
+ options?.signal?.addEventListener("abort", () => {
609
616
  cancel();
610
- reject(((_a2 = options.signal) == null ? void 0 : _a2.reason) ?? new Error("cancelled"));
617
+ reject(options.signal?.reason ?? new Error("cancelled"));
611
618
  });
612
- if ((options == null ? void 0 : options.timeout) !== void 0) {
619
+ if (options?.timeout !== void 0) {
613
620
  timer = setTimeout(() => {
614
621
  cancel();
615
622
  reject(new Error("timeout"));
@@ -640,7 +647,7 @@ const _Store = class _Store extends Callable {
640
647
  }
641
648
  }
642
649
  };
643
- return new _Store(
650
+ return new Store(
644
651
  ({ use }) => {
645
652
  return selector(use(this));
646
653
  },
@@ -665,7 +672,7 @@ const _Store = class _Store extends Callable {
665
672
  });
666
673
  return () => {
667
674
  const { handle, timeout } = this.effects.get(effect) ?? {};
668
- handle == null ? void 0 : handle();
675
+ handle?.();
669
676
  if (timeout !== void 0) {
670
677
  clearTimeout(timeout);
671
678
  }
@@ -695,7 +702,7 @@ const _Store = class _Store extends Callable {
695
702
  return;
696
703
  for (const [effect, { handle, retain, timeout }] of this.effects.entries()) {
697
704
  if (!retain) {
698
- handle == null ? void 0 : handle();
705
+ handle?.();
699
706
  if (timeout !== void 0) {
700
707
  clearTimeout(timeout);
701
708
  }
@@ -731,16 +738,14 @@ const _Store = class _Store extends Callable {
731
738
  break;
732
739
  }
733
740
  }
734
- };
735
- /* @__PURE__ */ autobind(_Store);
736
- let Store = _Store;
741
+ }
737
742
  function create(initialState, options) {
738
743
  options = { ...createStore.defaultOptions, ...options };
739
744
  const store = new Store(initialState, options);
740
745
  if (initialState instanceof Function) {
741
746
  return store;
742
747
  }
743
- let methods = options == null ? void 0 : options.methods;
748
+ let methods = options?.methods;
744
749
  if (initialState instanceof Map) {
745
750
  methods = { ...mapMethods, ...methods };
746
751
  } else if (initialState instanceof Set) {
@@ -760,21 +765,6 @@ const createStore = /* @__PURE__ */ Object.assign(create, {
760
765
  equals: deepEqual
761
766
  }
762
767
  });
763
- export {
764
- PromiseWithState as P,
765
- Store as S,
766
- autobind as a,
767
- calculatedValue as b,
768
- createStore as c,
769
- calcDuration as d,
770
- arrayMethods as e,
771
- mapMethods as f,
772
- debounce as g,
773
- isPromise as i,
774
- makeSelector as m,
775
- queue as q,
776
- recordMethods as r,
777
- setMethods as s,
778
- throttle as t
779
- };
768
+
769
+ export { PromiseWithState as P, Store as S, autobind as a, calculatedValue as b, createStore as c, calcDuration as d, arrayMethods as e, mapMethods as f, debounce as g, isPromise as i, makeSelector as m, queue as q, recordMethods as r, setMethods as s, throttle as t };
780
770
  //# sourceMappingURL=store.mjs.map