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