ccstate 4.12.0 → 5.0.0

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/core/index.cjs CHANGED
@@ -55,14 +55,8 @@ function _arrayWithHoles(r) {
55
55
  function _classCallCheck(a, n) {
56
56
  if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
57
57
  }
58
- function _defineProperties(e, r) {
59
- for (var t = 0; t < r.length; t++) {
60
- var o = r[t];
61
- o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o);
62
- }
63
- }
64
58
  function _createClass(e, r, t) {
65
- return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", {
59
+ return Object.defineProperty(e, "prototype", {
66
60
  writable: !1
67
61
  }), e;
68
62
  }
@@ -159,11 +153,11 @@ function _toPrimitive(t, r) {
159
153
  if ("object" != typeof t || !t) return t;
160
154
  var e = t[Symbol.toPrimitive];
161
155
  if (void 0 !== e) {
162
- var i = e.call(t, r);
156
+ var i = e.call(t, r || "default");
163
157
  if ("object" != typeof i) return i;
164
158
  throw new TypeError("@@toPrimitive must return a primitive value.");
165
159
  }
166
- return (String )(t);
160
+ return ("string" === r ? String : Number)(t);
167
161
  }
168
162
  function _toPropertyKey(t) {
169
163
  var i = _toPrimitive(t, "string");
@@ -261,63 +255,6 @@ function withGeValInterceptor(fn, signal, interceptor) {
261
255
  }
262
256
  return result.data;
263
257
  }
264
- function withSubInterceptor(fn, signal, callback$, interceptor) {
265
- if (!interceptor) {
266
- fn();
267
- return;
268
- }
269
- var result = {
270
- called: false
271
- };
272
- interceptor(signal, callback$, function () {
273
- result = {
274
- called: true,
275
- data: undefined
276
- };
277
- fn();
278
- });
279
- if (!result.called) {
280
- throw new Error('interceptor must call fn sync');
281
- }
282
- }
283
- function withUnsubInterceptor(fn, signal, callback$, interceptor) {
284
- if (!interceptor) {
285
- fn();
286
- return;
287
- }
288
- var result = {
289
- called: false
290
- };
291
- interceptor(signal, callback$, function () {
292
- result = {
293
- called: true,
294
- data: undefined
295
- };
296
- fn();
297
- });
298
- if (!result.called) {
299
- throw new Error('interceptor must call fn sync');
300
- }
301
- }
302
- function withNotifyInterceptor(fn, callback$, interceptor) {
303
- if (!interceptor) {
304
- return fn();
305
- }
306
- var result = {
307
- called: false
308
- };
309
- interceptor(callback$, function () {
310
- result = {
311
- called: true,
312
- data: fn()
313
- };
314
- return result.data;
315
- });
316
- if (!result.called) {
317
- throw new Error('interceptor must call fn sync');
318
- }
319
- return result.data;
320
- }
321
258
 
322
259
  function canReadAsCompute(atom) {
323
260
  return 'read' in atom;
@@ -515,30 +452,18 @@ function pushDirtyMarkers(signalState, context, mutation) {
515
452
  }
516
453
  }
517
454
  function pullEvaluate(readComputed, signalState, context, mutation) {
518
- var _signalState$mounted$2, _signalState$mounted2, _signalState$mounted$3, _signalState$mounted3, _signalState$mounted$4, _signalState$mounted4;
455
+ var _signalState$mounted$2, _signalState$mounted2, _signalState$mounted$3, _signalState$mounted3;
519
456
  var queue = Array.from((_signalState$mounted$2 = (_signalState$mounted2 = signalState.mounted) === null || _signalState$mounted2 === void 0 ? void 0 : _signalState$mounted2.readDepts) !== null && _signalState$mounted$2 !== void 0 ? _signalState$mounted$2 : []);
520
- var _iterator3 = _createForOfIteratorHelper((_signalState$mounted$4 = (_signalState$mounted4 = signalState.mounted) === null || _signalState$mounted4 === void 0 ? void 0 : _signalState$mounted4.listeners) !== null && _signalState$mounted$4 !== void 0 ? _signalState$mounted$4 : []),
521
- _step3;
522
- try {
523
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
524
- var _listener = _step3.value;
525
- mutation.pendingListeners.add(_listener);
526
- }
527
- } catch (err) {
528
- _iterator3.e(err);
529
- } finally {
530
- _iterator3.f();
531
- }
532
457
  var oldValues = new Map();
533
458
  var oldErrors = new Map();
534
459
  while (queue.length > 0) {
535
460
  var nextQueue = [];
536
- var _iterator4 = _createForOfIteratorHelper(queue),
537
- _step4;
461
+ var _iterator3 = _createForOfIteratorHelper(queue),
462
+ _step3;
538
463
  try {
539
- for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
464
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
540
465
  var _context$stateMap$get;
541
- var computed$ = _step4.value;
466
+ var computed$ = _step3.value;
542
467
  var oldState = context.stateMap.get(computed$);
543
468
  oldValues.set(computed$, oldState === null || oldState === void 0 ? void 0 : oldState.val);
544
469
  oldErrors.set(computed$, oldState === null || oldState === void 0 ? void 0 : oldState.error);
@@ -551,42 +476,28 @@ function pullEvaluate(readComputed, signalState, context, mutation) {
551
476
  }
552
477
  }
553
478
  } catch (err) {
554
- _iterator4.e(err);
479
+ _iterator3.e(err);
555
480
  } finally {
556
- _iterator4.f();
481
+ _iterator3.f();
557
482
  }
558
483
  queue = nextQueue;
559
484
  }
560
485
  queue = Array.from((_signalState$mounted$3 = (_signalState$mounted3 = signalState.mounted) === null || _signalState$mounted3 === void 0 ? void 0 : _signalState$mounted3.readDepts) !== null && _signalState$mounted$3 !== void 0 ? _signalState$mounted$3 : []);
561
486
  while (queue.length > 0) {
562
487
  var _nextQueue = [];
563
- var _iterator5 = _createForOfIteratorHelper(queue),
564
- _step5;
488
+ var _iterator4 = _createForOfIteratorHelper(queue),
489
+ _step4;
565
490
  try {
566
- for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
567
- var _computedState$mounte, _computedState$mounte2;
568
- var _computed$ = _step5.value;
491
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
492
+ var _computedState$mounte;
493
+ var _computed$ = _step4.value;
569
494
  var computedState = readComputed(_computed$, context, mutation);
570
495
  var isSameWithOldValue = !computedState.error && oldValues.has(_computed$) && oldValues.get(_computed$) === computedState.val;
571
496
  var isSameError = computedState.error && Boolean(oldErrors.get(_computed$));
572
497
  if (isSameWithOldValue || isSameError) {
573
498
  continue;
574
499
  }
575
- if ((_computedState$mounte = computedState.mounted) !== null && _computedState$mounte !== void 0 && _computedState$mounte.listeners) {
576
- var _iterator6 = _createForOfIteratorHelper(computedState.mounted.listeners),
577
- _step6;
578
- try {
579
- for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
580
- var listener = _step6.value;
581
- mutation.pendingListeners.add(listener);
582
- }
583
- } catch (err) {
584
- _iterator6.e(err);
585
- } finally {
586
- _iterator6.f();
587
- }
588
- }
589
- var _readDepts = (_computedState$mounte2 = computedState.mounted) === null || _computedState$mounte2 === void 0 ? void 0 : _computedState$mounte2.readDepts;
500
+ var _readDepts = (_computedState$mounte = computedState.mounted) === null || _computedState$mounte === void 0 ? void 0 : _computedState$mounte.readDepts;
590
501
  if (_readDepts) {
591
502
  for (var _i2 = 0, _Array$from2 = Array.from(_readDepts); _i2 < _Array$from2.length; _i2++) {
592
503
  var _dep = _Array$from2[_i2];
@@ -595,9 +506,9 @@ function pullEvaluate(readComputed, signalState, context, mutation) {
595
506
  }
596
507
  }
597
508
  } catch (err) {
598
- _iterator5.e(err);
509
+ _iterator4.e(err);
599
510
  } finally {
600
- _iterator5.f();
511
+ _iterator4.f();
601
512
  }
602
513
  queue = _nextQueue;
603
514
  }
@@ -661,7 +572,6 @@ function set(readComputed, writable$, context, mutation) {
661
572
  function createMutation(context, _get, _set) {
662
573
  var mutation = {
663
574
  potentialDirtyIds: new Set(),
664
- pendingListeners: new Set(),
665
575
  visitor: {
666
576
  get: function get(signal$) {
667
577
  return _get(signal$, context, mutation);
@@ -708,11 +618,10 @@ function mountComputedDependencies(readSignal, computed$, computedState, context
708
618
  }
709
619
  }
710
620
  function initMount(readSignal, signal$, context, mutation) {
711
- var _context$interceptor, _context$interceptor$, _signalState$mounted;
621
+ var _context$interceptor, _context$interceptor$;
712
622
  (_context$interceptor = context.interceptor) === null || _context$interceptor === void 0 || (_context$interceptor$ = _context$interceptor.mount) === null || _context$interceptor$ === void 0 || _context$interceptor$.call(_context$interceptor, signal$);
713
623
  var signalState = readSignal(signal$, context, mutation);
714
- signalState.mounted = (_signalState$mounted = signalState.mounted) !== null && _signalState$mounted !== void 0 ? _signalState$mounted : {
715
- listeners: new Set(),
624
+ signalState.mounted = {
716
625
  readDepts: new Set()
717
626
  };
718
627
  if (isComputedState(signalState)) {
@@ -738,52 +647,11 @@ function doUnmount(signal$, signalState, context, mutation) {
738
647
  }
739
648
  function unmount(signal$, context, mutation) {
740
649
  var signalState = context.stateMap.get(signal$);
741
- if (!(signalState !== null && signalState !== void 0 && signalState.mounted) || signalState.mounted.listeners.size || signalState.mounted.readDepts.size) {
650
+ if (!(signalState !== null && signalState !== void 0 && signalState.mounted) || signalState.mounted.readDepts.size) {
742
651
  return;
743
652
  }
744
653
  doUnmount(signal$, signalState, context);
745
654
  }
746
- function subSingleSignal(readSignal, signal$, callback$, context, signal) {
747
- var _context$interceptor5;
748
- withSubInterceptor(function () {
749
- var mounted = mount$1(readSignal, signal$, context);
750
- mounted.listeners.add(callback$);
751
- var unsub = function unsub() {
752
- var _context$interceptor4;
753
- withUnsubInterceptor(function () {
754
- mounted.listeners["delete"](callback$);
755
- if (mounted.readDepts.size === 0 && mounted.listeners.size === 0) {
756
- unmount(signal$, context);
757
- }
758
- }, signal$, callback$, (_context$interceptor4 = context.interceptor) === null || _context$interceptor4 === void 0 ? void 0 : _context$interceptor4.unsub);
759
- };
760
- signal.addEventListener('abort', unsub, {
761
- once: true
762
- });
763
- }, signal$, callback$, (_context$interceptor5 = context.interceptor) === null || _context$interceptor5 === void 0 ? void 0 : _context$interceptor5.sub);
764
- }
765
- function notify(context, mutation) {
766
- var pendingListeners = mutation.pendingListeners;
767
- mutation.pendingListeners = new Set();
768
- var _iterator = _createForOfIteratorHelper(pendingListeners),
769
- _step;
770
- try {
771
- var _loop = function _loop() {
772
- var _context$interceptor6;
773
- var listener = _step.value;
774
- withNotifyInterceptor(function () {
775
- return listener.write(mutation.visitor);
776
- }, listener, (_context$interceptor6 = context.interceptor) === null || _context$interceptor6 === void 0 ? void 0 : _context$interceptor6.notify);
777
- };
778
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
779
- _loop();
780
- }
781
- } catch (err) {
782
- _iterator.e(err);
783
- } finally {
784
- _iterator.f();
785
- }
786
- }
787
655
 
788
656
  var _readComputed = function readComputed(computed$, context, mutation) {
789
657
  var _context$interceptor;
@@ -804,26 +672,7 @@ function readSignal(signal$, context, mutation) {
804
672
  function mount(signal$, context, mutation) {
805
673
  return mount$1(readSignal, signal$, context, mutation);
806
674
  }
807
- function _sub(signals$, callback$, context, options) {
808
- if (Array.isArray(signals$) && signals$.length === 0) {
809
- return function () {
810
- return void 0;
811
- };
812
- }
813
- var controller = new AbortController();
814
- var signal = options !== null && options !== void 0 && options.signal ? AbortSignal.any([controller.signal, options.signal]) : controller.signal;
815
- if (!Array.isArray(signals$)) {
816
- subSingleSignal(readSignal, signals$, callback$, context, signal);
817
- } else {
818
- signals$.forEach(function (atom) {
819
- subSingleSignal(readSignal, atom, callback$, context, signal);
820
- });
821
- }
822
- return function () {
823
- controller.abort();
824
- };
825
- }
826
- var get = function get(signal, context, mutation) {
675
+ var storeGet = function storeGet(signal, context, mutation) {
827
676
  var _context$interceptor2;
828
677
  return withGetInterceptor(function () {
829
678
  var signalState = readSignal(signal, context, mutation);
@@ -833,64 +682,69 @@ var get = function get(signal, context, mutation) {
833
682
  return signalState.val;
834
683
  }, signal, (_context$interceptor2 = context.interceptor) === null || _context$interceptor2 === void 0 ? void 0 : _context$interceptor2.get);
835
684
  };
836
- var _set = function set$1(atom, context) {
685
+ var _storeSet = function storeSet(atom, context) {
837
686
  var _context$interceptor3;
838
687
  for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
839
688
  args[_key - 2] = arguments[_key];
840
689
  }
841
690
  return withSetInterceptor.apply(void 0, [function () {
842
- var mutation = createMutation(context, get, _set);
843
- var ret;
844
- try {
845
- ret = set.apply(void 0, [_readComputed, atom, context, mutation].concat(args));
846
- } finally {
847
- notify(context, mutation);
848
- }
849
- return ret;
691
+ var mutation = createMutation(context, storeGet, _storeSet);
692
+ return set.apply(void 0, [_readComputed, atom, context, mutation].concat(args));
850
693
  }, atom, (_context$interceptor3 = context.interceptor) === null || _context$interceptor3 === void 0 ? void 0 : _context$interceptor3.set].concat(args));
851
694
  };
852
- var StoreImpl = /*#__PURE__*/function () {
853
- function StoreImpl(options) {
854
- var _this = this,
855
- _this$options;
856
- _classCallCheck(this, StoreImpl);
857
- _defineProperty(this, "stateMap", new WeakMap());
858
- _defineProperty(this, "get", function (atom) {
859
- return get(atom, _this.context);
860
- });
861
- _defineProperty(this, "set", function (atom) {
862
- for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
863
- args[_key2 - 1] = arguments[_key2];
695
+ var storeWatch = function storeWatch(watchFn, context, options) {
696
+ var _options$signal;
697
+ var computed$ = computed(function (get, _ref) {
698
+ var signal = _ref.signal;
699
+ var childSignal;
700
+ var obOptions = {
701
+ get signal() {
702
+ if (!childSignal) {
703
+ childSignal = options !== null && options !== void 0 && options.signal ? AbortSignal.any([options.signal, signal]) : signal;
704
+ }
705
+ return childSignal;
864
706
  }
865
- return _set.apply(void 0, [atom, _this.context].concat(args));
866
- });
867
- this.options = options;
868
- this.context = {
869
- stateMap: this.stateMap,
870
- interceptor: (_this$options = this.options) === null || _this$options === void 0 ? void 0 : _this$options.interceptor
871
707
  };
872
- }
873
- return _createClass(StoreImpl, [{
874
- key: "sub",
875
- value: function sub(targets$, cb$, options) {
876
- return _sub(targets$, cb$, this.context, options);
708
+ watchFn(get, obOptions);
709
+ }, {
710
+ debugLabel: options === null || options === void 0 ? void 0 : options.debugLabel
711
+ });
712
+ mount$1(readSignal, computed$, context);
713
+ options === null || options === void 0 || (_options$signal = options.signal) === null || _options$signal === void 0 || _options$signal.addEventListener('abort', function () {
714
+ unmount(computed$, context);
715
+ }, {
716
+ once: true
717
+ });
718
+ };
719
+ var StoreImpl = /*#__PURE__*/_createClass(function StoreImpl(_options) {
720
+ var _this = this,
721
+ _this$options;
722
+ _classCallCheck(this, StoreImpl);
723
+ _defineProperty(this, "stateMap", new WeakMap());
724
+ _defineProperty(this, "get", function (atom) {
725
+ return storeGet(atom, _this.context);
726
+ });
727
+ _defineProperty(this, "set", function (atom) {
728
+ for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
729
+ args[_key2 - 1] = arguments[_key2];
877
730
  }
878
- }]);
879
- }();
731
+ return _storeSet.apply(void 0, [atom, _this.context].concat(args));
732
+ });
733
+ _defineProperty(this, "watch", function (watchFn, options) {
734
+ storeWatch(watchFn, _this.context, options);
735
+ });
736
+ this.options = _options;
737
+ this.context = {
738
+ stateMap: this.stateMap,
739
+ interceptor: (_this$options = this.options) === null || _this$options === void 0 ? void 0 : _this$options.interceptor
740
+ };
741
+ });
880
742
  function createStore() {
881
743
  return new StoreImpl();
882
744
  }
883
- var defaultStore = undefined;
884
- function getDefaultStore() {
885
- if (!defaultStore) {
886
- defaultStore = createStore();
887
- }
888
- return defaultStore;
889
- }
890
745
 
891
746
  exports.command = command;
892
747
  exports.computed = computed;
893
748
  exports.createStore = createStore;
894
- exports.getDefaultStore = getDefaultStore;
895
749
  exports.state = state;
896
750
  //# sourceMappingURL=index.cjs.map