ccstate 2.1.0 → 3.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/debug/index.js CHANGED
@@ -1,13 +1,3 @@
1
- function nestedAtomToString(atoms) {
2
- return atoms.map(function (atom) {
3
- var _atom$debugLabel;
4
- if (Array.isArray(atom)) {
5
- return nestedAtomToString(atom);
6
- }
7
- return (_atom$debugLabel = atom.debugLabel) !== null && _atom$debugLabel !== void 0 ? _atom$debugLabel : 'anonymous';
8
- });
9
- }
10
-
11
1
  function _arrayLikeToArray(r, a) {
12
2
  (null == a || a > r.length) && (a = r.length);
13
3
  for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
@@ -29,13 +19,6 @@ function _callSuper(t, o, e) {
29
19
  function _classCallCheck(a, n) {
30
20
  if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
31
21
  }
32
- function _construct(t, e, r) {
33
- if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments);
34
- var o = [null];
35
- o.push.apply(o, e);
36
- var p = new (t.bind.apply(t, o))();
37
- return r && _setPrototypeOf(p, r.prototype), p;
38
- }
39
22
  function _defineProperties(e, r) {
40
23
  for (var t = 0; t < r.length; t++) {
41
24
  var o = r[t];
@@ -129,13 +112,6 @@ function _inherits(t, e) {
129
112
  writable: !1
130
113
  }), e && _setPrototypeOf(t, e);
131
114
  }
132
- function _isNativeFunction(t) {
133
- try {
134
- return -1 !== Function.toString.call(t).indexOf("[native code]");
135
- } catch (n) {
136
- return "function" == typeof t;
137
- }
138
- }
139
115
  function _isNativeReflectConstruct() {
140
116
  try {
141
117
  var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
@@ -521,15 +497,6 @@ function _toPropertyKey(t) {
521
497
  var i = _toPrimitive(t, "string");
522
498
  return "symbol" == typeof i ? i : i + "";
523
499
  }
524
- function _typeof(o) {
525
- "@babel/helpers - typeof";
526
-
527
- return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
528
- return typeof o;
529
- } : function (o) {
530
- return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
531
- }, _typeof(o);
532
- }
533
500
  function _unsupportedIterableToArray(r, a) {
534
501
  if (r) {
535
502
  if ("string" == typeof r) return _arrayLikeToArray(r, a);
@@ -537,28 +504,6 @@ function _unsupportedIterableToArray(r, a) {
537
504
  return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
538
505
  }
539
506
  }
540
- function _wrapNativeSuper(t) {
541
- var r = "function" == typeof Map ? new Map() : void 0;
542
- return _wrapNativeSuper = function (t) {
543
- if (null === t || !_isNativeFunction(t)) return t;
544
- if ("function" != typeof t) throw new TypeError("Super expression must either be null or a function");
545
- if (void 0 !== r) {
546
- if (r.has(t)) return r.get(t);
547
- r.set(t, Wrapper);
548
- }
549
- function Wrapper() {
550
- return _construct(t, arguments, _getPrototypeOf(this).constructor);
551
- }
552
- return Wrapper.prototype = Object.create(t.prototype, {
553
- constructor: {
554
- value: Wrapper,
555
- enumerable: !1,
556
- writable: !0,
557
- configurable: !0
558
- }
559
- }), _setPrototypeOf(Wrapper, t);
560
- }, _wrapNativeSuper(t);
561
- }
562
507
 
563
508
  function canReadAsCompute(atom) {
564
509
  return 'read' in atom;
@@ -1158,7 +1103,7 @@ var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1158
1103
  _inherits(DebugStoreImpl, _StoreImpl);
1159
1104
  return _createClass(DebugStoreImpl);
1160
1105
  }(StoreImpl);
1161
- function createDebugStore(interceptor) {
1106
+ function createDebugStoreInternal(interceptor) {
1162
1107
  var atomManager = new AtomManager({
1163
1108
  interceptor: interceptor
1164
1109
  });
@@ -1259,7 +1204,9 @@ var ConsoleInterceptor = /*#__PURE__*/_createClass(function ConsoleInterceptor(w
1259
1204
  });
1260
1205
  this.watches = watches;
1261
1206
  });
1262
- function createConsoleDebugStore(watches, defaultActions) {
1207
+ function createDebugStore() {
1208
+ var watches = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
1209
+ var defaultActions = arguments.length > 1 ? arguments[1] : undefined;
1263
1210
  var parsedWatches = watches.map(function (watch) {
1264
1211
  if (typeof watch === 'string' || watch instanceof RegExp) {
1265
1212
  return {
@@ -1276,168 +1223,7 @@ function createConsoleDebugStore(watches, defaultActions) {
1276
1223
  };
1277
1224
  });
1278
1225
  var interceptor = new ConsoleInterceptor(parsedWatches);
1279
- return createDebugStore(interceptor);
1280
- }
1281
-
1282
- var StoreEvent = /*#__PURE__*/function (_Event) {
1283
- function StoreEvent(type, eventId, targetAtom, state, time, args, result) {
1284
- var _this;
1285
- _classCallCheck(this, StoreEvent);
1286
- _this = _callSuper(this, StoreEvent, [type]);
1287
- _this.eventId = eventId;
1288
- _this.targetAtom = targetAtom;
1289
- _this.state = state;
1290
- _this.time = time;
1291
- _this.args = args;
1292
- _this.result = result;
1293
- return _this;
1294
- }
1295
- _inherits(StoreEvent, _Event);
1296
- return _createClass(StoreEvent);
1297
- }(/*#__PURE__*/_wrapNativeSuper(Event));
1298
-
1299
- var EventInterceptor = /*#__PURE__*/function () {
1300
- function EventInterceptor() {
1301
- var _this = this;
1302
- _classCallCheck(this, EventInterceptor);
1303
- _defineProperty(this, "traceId", 0);
1304
- _defineProperty(this, "events", new EventTarget());
1305
- _defineProperty(this, "get", function (atom$, fn) {
1306
- return _this.wrapWithTrace(fn, function (eventId, time) {
1307
- _this.createEvent('get', eventId, atom$.toString(), time, 'begin', [], undefined);
1308
- }, function (eventId, time, result) {
1309
- _this.createEvent('get', eventId, atom$.toString(), time, 'success', [], result);
1310
- }, function (eventId, time, error) {
1311
- _this.createEvent('get', eventId, atom$.toString(), time, 'error', [], error);
1312
- });
1313
- });
1314
- _defineProperty(this, "computed", function (atom$, fn) {
1315
- return _this.wrapWithTrace(fn, function (eventId, time) {
1316
- _this.createEvent('computed', eventId, atom$.toString(), time, 'begin', [], undefined);
1317
- }, function (eventId, time, result) {
1318
- _this.createEvent('computed', eventId, atom$.toString(), time, 'success', [], result);
1319
- }, function (eventId, time, error) {
1320
- _this.createEvent('computed', eventId, atom$.toString(), time, 'error', [], error);
1321
- });
1322
- });
1323
- _defineProperty(this, "set", function (atom$, fn) {
1324
- for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
1325
- args[_key - 2] = arguments[_key];
1326
- }
1327
- return _this.wrapWithTrace(fn, function (eventId, time) {
1328
- _this.createEvent('set', eventId, atom$.toString(), time, 'begin', args, undefined);
1329
- }, function (eventId, time, result) {
1330
- _this.createEvent('set', eventId, atom$.toString(), time, 'success', args, result);
1331
- }, function (eventId, time, error) {
1332
- _this.createEvent('set', eventId, atom$.toString(), time, 'error', args, error);
1333
- });
1334
- });
1335
- _defineProperty(this, "sub", function (atom$, callback$, fn) {
1336
- var eventId = _this.traceId++;
1337
- _this.createEvent('sub', eventId, atom$.toString(), performance.now(), 'begin', [callback$.toString()], undefined);
1338
- fn();
1339
- _this.createEvent('sub', eventId, atom$.toString(), performance.now(), 'success', [callback$.toString()], undefined);
1340
- });
1341
- _defineProperty(this, "unsub", function (atom$, callback$, fn) {
1342
- var eventId = _this.traceId++;
1343
- _this.createEvent('unsub', eventId, atom$.toString(), performance.now(), 'begin', [callback$.toString()], undefined);
1344
- fn();
1345
- _this.createEvent('unsub', eventId, atom$.toString(), performance.now(), 'success', [callback$.toString()], undefined);
1346
- });
1347
- _defineProperty(this, "mount", function (atom$) {
1348
- var eventId = _this.traceId++;
1349
- _this.createEvent('mount', eventId, atom$.toString(), performance.now(), 'begin', [], undefined);
1350
- });
1351
- _defineProperty(this, "unmount", function (atom$) {
1352
- var eventId = _this.traceId++;
1353
- _this.createEvent('unmount', eventId, atom$.toString(), performance.now(), 'begin', [], undefined);
1354
- });
1355
- _defineProperty(this, "notify", function (callback$, fn) {
1356
- var eventId = _this.traceId++;
1357
- _this.createEvent('notify', eventId, callback$.toString(), performance.now(), 'begin', [], undefined);
1358
- var ret = fn();
1359
- _this.createEvent('notify', eventId, callback$.toString(), performance.now(), 'success', [], ret);
1360
- });
1361
- }
1362
- return _createClass(EventInterceptor, [{
1363
- key: "createEvent",
1364
- value: function createEvent(type, eventId, atom, time, state, args, result) {
1365
- var event = new StoreEvent(type, eventId, atom, state, time, args, result);
1366
- this.events.dispatchEvent(event);
1367
- return event;
1368
- }
1369
- }, {
1370
- key: "wrapWithTrace",
1371
- value: function wrapWithTrace(fn, createBeginEvent, createSuccessEvent, createErrorEvent) {
1372
- var eventId = this.traceId++;
1373
- createBeginEvent(eventId, performance.now());
1374
- try {
1375
- var result = fn();
1376
- createSuccessEvent(eventId, performance.now(), result);
1377
- return result;
1378
- } catch (e) {
1379
- createErrorEvent(eventId, performance.now(), e);
1380
- throw e;
1381
- }
1382
- }
1383
- }, {
1384
- key: "addEventListener",
1385
- value: function addEventListener(type, listener, options) {
1386
- this.events.addEventListener(type, listener, options);
1387
- }
1388
- }, {
1389
- key: "removeEventListener",
1390
- value: function removeEventListener(type, listener, options) {
1391
- this.events.removeEventListener(type, listener, options);
1392
- }
1393
- }]);
1394
- }();
1395
-
1396
- var GLOBAL_CCSTATE_INTERCEPED_KEY = '__CCSTATE_INTERCEPED__';
1397
- function setupDevtoolsInterceptor(targetWindow, signal) {
1398
- var interceptor = new EventInterceptor();
1399
- var watchedAtoms = new Set();
1400
- targetWindow.addEventListener('message', function (_ref) {
1401
- var data = _ref.data;
1402
- if (!data || _typeof(data) !== 'object' || !('source' in data) || data.source !== 'ccstate-devtools') {
1403
- return;
1404
- }
1405
- var payload = data.payload;
1406
- watchedAtoms.add(payload.args[0]);
1407
- }, {
1408
- signal: signal
1409
- });
1410
- function handleStoreEvent(event) {
1411
- var debugLabel = event.targetAtom.substring(event.targetAtom.indexOf(':') + 1);
1412
- if (watchedAtoms.has(debugLabel)) {
1413
- console.group("[CCState] ".concat(event.type, " ").concat(event.targetAtom, " ").concat(event.state));
1414
- console.log('args', event.args);
1415
- console.log('result', event.result);
1416
- console.groupEnd();
1417
- }
1418
- var message = {
1419
- source: 'ccstate-store',
1420
- payload: {
1421
- type: event.type,
1422
- eventId: event.eventId,
1423
- targetAtom: event.targetAtom,
1424
- time: event.time,
1425
- state: event.state
1426
- }
1427
- };
1428
- targetWindow.postMessage(message);
1429
- }
1430
- interceptor.addEventListener('get', handleStoreEvent);
1431
- interceptor.addEventListener('computed', handleStoreEvent);
1432
- interceptor.addEventListener('set', handleStoreEvent);
1433
- interceptor.addEventListener('sub', handleStoreEvent);
1434
- interceptor.addEventListener('unsub', handleStoreEvent);
1435
- interceptor.addEventListener('mount', handleStoreEvent);
1436
- interceptor.addEventListener('unmount', handleStoreEvent);
1437
- interceptor.addEventListener('notify', handleStoreEvent);
1438
- targetWindow[GLOBAL_CCSTATE_INTERCEPED_KEY] = true;
1439
- console.warn('[CCSTATE] Interceptor injected, DO NOT USE THIS IN PRODUCTION');
1440
- return interceptor;
1226
+ return createDebugStoreInternal(interceptor);
1441
1227
  }
1442
1228
 
1443
- export { ConsoleInterceptor, EventInterceptor, GLOBAL_CCSTATE_INTERCEPED_KEY, StoreEvent, createConsoleDebugStore, createDebugStore, nestedAtomToString, setupDevtoolsInterceptor };
1229
+ export { createDebugStore };
package/index.cjs CHANGED
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- var react = require('react');
4
-
5
3
  var globalId = 0;
6
4
  var generateToString = function generateToString(prefix, debugLabel) {
7
5
  var id = globalId++;
@@ -62,13 +60,6 @@ function _callSuper(t, o, e) {
62
60
  function _classCallCheck(a, n) {
63
61
  if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
64
62
  }
65
- function _construct(t, e, r) {
66
- if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments);
67
- var o = [null];
68
- o.push.apply(o, e);
69
- var p = new (t.bind.apply(t, o))();
70
- return r && _setPrototypeOf(p, r.prototype), p;
71
- }
72
63
  function _defineProperties(e, r) {
73
64
  for (var t = 0; t < r.length; t++) {
74
65
  var o = r[t];
@@ -162,13 +153,6 @@ function _inherits(t, e) {
162
153
  writable: !1
163
154
  }), e && _setPrototypeOf(t, e);
164
155
  }
165
- function _isNativeFunction(t) {
166
- try {
167
- return -1 !== Function.toString.call(t).indexOf("[native code]");
168
- } catch (n) {
169
- return "function" == typeof t;
170
- }
171
- }
172
156
  function _isNativeReflectConstruct() {
173
157
  try {
174
158
  var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
@@ -554,15 +538,6 @@ function _toPropertyKey(t) {
554
538
  var i = _toPrimitive(t, "string");
555
539
  return "symbol" == typeof i ? i : i + "";
556
540
  }
557
- function _typeof(o) {
558
- "@babel/helpers - typeof";
559
-
560
- return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
561
- return typeof o;
562
- } : function (o) {
563
- return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
564
- }, _typeof(o);
565
- }
566
541
  function _unsupportedIterableToArray(r, a) {
567
542
  if (r) {
568
543
  if ("string" == typeof r) return _arrayLikeToArray(r, a);
@@ -570,28 +545,6 @@ function _unsupportedIterableToArray(r, a) {
570
545
  return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
571
546
  }
572
547
  }
573
- function _wrapNativeSuper(t) {
574
- var r = "function" == typeof Map ? new Map() : void 0;
575
- return _wrapNativeSuper = function (t) {
576
- if (null === t || !_isNativeFunction(t)) return t;
577
- if ("function" != typeof t) throw new TypeError("Super expression must either be null or a function");
578
- if (void 0 !== r) {
579
- if (r.has(t)) return r.get(t);
580
- r.set(t, Wrapper);
581
- }
582
- function Wrapper() {
583
- return _construct(t, arguments, _getPrototypeOf(this).constructor);
584
- }
585
- return Wrapper.prototype = Object.create(t.prototype, {
586
- constructor: {
587
- value: Wrapper,
588
- enumerable: !1,
589
- writable: !0,
590
- configurable: !0
591
- }
592
- }), _setPrototypeOf(Wrapper, t);
593
- }, _wrapNativeSuper(t);
594
- }
595
548
 
596
549
  function canReadAsCompute(atom) {
597
550
  return 'read' in atom;
@@ -1124,16 +1077,6 @@ function createStore() {
1124
1077
  return new StoreImpl(atomManager, listenerManager);
1125
1078
  }
1126
1079
 
1127
- function nestedAtomToString(atoms) {
1128
- return atoms.map(function (atom) {
1129
- var _atom$debugLabel;
1130
- if (Array.isArray(atom)) {
1131
- return nestedAtomToString(atom);
1132
- }
1133
- return (_atom$debugLabel = atom.debugLabel) !== null && _atom$debugLabel !== void 0 ? _atom$debugLabel : 'anonymous';
1134
- });
1135
- }
1136
-
1137
1080
  var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1138
1081
  function DebugStoreImpl() {
1139
1082
  var _this;
@@ -1206,7 +1149,7 @@ var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1206
1149
  _inherits(DebugStoreImpl, _StoreImpl);
1207
1150
  return _createClass(DebugStoreImpl);
1208
1151
  }(StoreImpl);
1209
- function createDebugStore(interceptor) {
1152
+ function createDebugStoreInternal(interceptor) {
1210
1153
  var atomManager = new AtomManager({
1211
1154
  interceptor: interceptor
1212
1155
  });
@@ -1307,7 +1250,9 @@ var ConsoleInterceptor = /*#__PURE__*/_createClass(function ConsoleInterceptor(w
1307
1250
  });
1308
1251
  this.watches = watches;
1309
1252
  });
1310
- function createConsoleDebugStore(watches, defaultActions) {
1253
+ function createDebugStore() {
1254
+ var watches = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
1255
+ var defaultActions = arguments.length > 1 ? arguments[1] : undefined;
1311
1256
  var parsedWatches = watches.map(function (watch) {
1312
1257
  if (typeof watch === 'string' || watch instanceof RegExp) {
1313
1258
  return {
@@ -1324,300 +1269,11 @@ function createConsoleDebugStore(watches, defaultActions) {
1324
1269
  };
1325
1270
  });
1326
1271
  var interceptor = new ConsoleInterceptor(parsedWatches);
1327
- return createDebugStore(interceptor);
1328
- }
1329
-
1330
- var StoreEvent = /*#__PURE__*/function (_Event) {
1331
- function StoreEvent(type, eventId, targetAtom, state, time, args, result) {
1332
- var _this;
1333
- _classCallCheck(this, StoreEvent);
1334
- _this = _callSuper(this, StoreEvent, [type]);
1335
- _this.eventId = eventId;
1336
- _this.targetAtom = targetAtom;
1337
- _this.state = state;
1338
- _this.time = time;
1339
- _this.args = args;
1340
- _this.result = result;
1341
- return _this;
1342
- }
1343
- _inherits(StoreEvent, _Event);
1344
- return _createClass(StoreEvent);
1345
- }(/*#__PURE__*/_wrapNativeSuper(Event));
1346
-
1347
- var EventInterceptor = /*#__PURE__*/function () {
1348
- function EventInterceptor() {
1349
- var _this = this;
1350
- _classCallCheck(this, EventInterceptor);
1351
- _defineProperty(this, "traceId", 0);
1352
- _defineProperty(this, "events", new EventTarget());
1353
- _defineProperty(this, "get", function (atom$, fn) {
1354
- return _this.wrapWithTrace(fn, function (eventId, time) {
1355
- _this.createEvent('get', eventId, atom$.toString(), time, 'begin', [], undefined);
1356
- }, function (eventId, time, result) {
1357
- _this.createEvent('get', eventId, atom$.toString(), time, 'success', [], result);
1358
- }, function (eventId, time, error) {
1359
- _this.createEvent('get', eventId, atom$.toString(), time, 'error', [], error);
1360
- });
1361
- });
1362
- _defineProperty(this, "computed", function (atom$, fn) {
1363
- return _this.wrapWithTrace(fn, function (eventId, time) {
1364
- _this.createEvent('computed', eventId, atom$.toString(), time, 'begin', [], undefined);
1365
- }, function (eventId, time, result) {
1366
- _this.createEvent('computed', eventId, atom$.toString(), time, 'success', [], result);
1367
- }, function (eventId, time, error) {
1368
- _this.createEvent('computed', eventId, atom$.toString(), time, 'error', [], error);
1369
- });
1370
- });
1371
- _defineProperty(this, "set", function (atom$, fn) {
1372
- for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
1373
- args[_key - 2] = arguments[_key];
1374
- }
1375
- return _this.wrapWithTrace(fn, function (eventId, time) {
1376
- _this.createEvent('set', eventId, atom$.toString(), time, 'begin', args, undefined);
1377
- }, function (eventId, time, result) {
1378
- _this.createEvent('set', eventId, atom$.toString(), time, 'success', args, result);
1379
- }, function (eventId, time, error) {
1380
- _this.createEvent('set', eventId, atom$.toString(), time, 'error', args, error);
1381
- });
1382
- });
1383
- _defineProperty(this, "sub", function (atom$, callback$, fn) {
1384
- var eventId = _this.traceId++;
1385
- _this.createEvent('sub', eventId, atom$.toString(), performance.now(), 'begin', [callback$.toString()], undefined);
1386
- fn();
1387
- _this.createEvent('sub', eventId, atom$.toString(), performance.now(), 'success', [callback$.toString()], undefined);
1388
- });
1389
- _defineProperty(this, "unsub", function (atom$, callback$, fn) {
1390
- var eventId = _this.traceId++;
1391
- _this.createEvent('unsub', eventId, atom$.toString(), performance.now(), 'begin', [callback$.toString()], undefined);
1392
- fn();
1393
- _this.createEvent('unsub', eventId, atom$.toString(), performance.now(), 'success', [callback$.toString()], undefined);
1394
- });
1395
- _defineProperty(this, "mount", function (atom$) {
1396
- var eventId = _this.traceId++;
1397
- _this.createEvent('mount', eventId, atom$.toString(), performance.now(), 'begin', [], undefined);
1398
- });
1399
- _defineProperty(this, "unmount", function (atom$) {
1400
- var eventId = _this.traceId++;
1401
- _this.createEvent('unmount', eventId, atom$.toString(), performance.now(), 'begin', [], undefined);
1402
- });
1403
- _defineProperty(this, "notify", function (callback$, fn) {
1404
- var eventId = _this.traceId++;
1405
- _this.createEvent('notify', eventId, callback$.toString(), performance.now(), 'begin', [], undefined);
1406
- var ret = fn();
1407
- _this.createEvent('notify', eventId, callback$.toString(), performance.now(), 'success', [], ret);
1408
- });
1409
- }
1410
- return _createClass(EventInterceptor, [{
1411
- key: "createEvent",
1412
- value: function createEvent(type, eventId, atom, time, state, args, result) {
1413
- var event = new StoreEvent(type, eventId, atom, state, time, args, result);
1414
- this.events.dispatchEvent(event);
1415
- return event;
1416
- }
1417
- }, {
1418
- key: "wrapWithTrace",
1419
- value: function wrapWithTrace(fn, createBeginEvent, createSuccessEvent, createErrorEvent) {
1420
- var eventId = this.traceId++;
1421
- createBeginEvent(eventId, performance.now());
1422
- try {
1423
- var result = fn();
1424
- createSuccessEvent(eventId, performance.now(), result);
1425
- return result;
1426
- } catch (e) {
1427
- createErrorEvent(eventId, performance.now(), e);
1428
- throw e;
1429
- }
1430
- }
1431
- }, {
1432
- key: "addEventListener",
1433
- value: function addEventListener(type, listener, options) {
1434
- this.events.addEventListener(type, listener, options);
1435
- }
1436
- }, {
1437
- key: "removeEventListener",
1438
- value: function removeEventListener(type, listener, options) {
1439
- this.events.removeEventListener(type, listener, options);
1440
- }
1441
- }]);
1442
- }();
1443
-
1444
- var GLOBAL_CCSTATE_INTERCEPED_KEY = '__CCSTATE_INTERCEPED__';
1445
- function setupDevtoolsInterceptor(targetWindow, signal) {
1446
- var interceptor = new EventInterceptor();
1447
- var watchedAtoms = new Set();
1448
- targetWindow.addEventListener('message', function (_ref) {
1449
- var data = _ref.data;
1450
- if (!data || _typeof(data) !== 'object' || !('source' in data) || data.source !== 'ccstate-devtools') {
1451
- return;
1452
- }
1453
- var payload = data.payload;
1454
- watchedAtoms.add(payload.args[0]);
1455
- }, {
1456
- signal: signal
1457
- });
1458
- function handleStoreEvent(event) {
1459
- var debugLabel = event.targetAtom.substring(event.targetAtom.indexOf(':') + 1);
1460
- if (watchedAtoms.has(debugLabel)) {
1461
- console.group("[CCState] ".concat(event.type, " ").concat(event.targetAtom, " ").concat(event.state));
1462
- console.log('args', event.args);
1463
- console.log('result', event.result);
1464
- console.groupEnd();
1465
- }
1466
- var message = {
1467
- source: 'ccstate-store',
1468
- payload: {
1469
- type: event.type,
1470
- eventId: event.eventId,
1471
- targetAtom: event.targetAtom,
1472
- time: event.time,
1473
- state: event.state
1474
- }
1475
- };
1476
- targetWindow.postMessage(message);
1477
- }
1478
- interceptor.addEventListener('get', handleStoreEvent);
1479
- interceptor.addEventListener('computed', handleStoreEvent);
1480
- interceptor.addEventListener('set', handleStoreEvent);
1481
- interceptor.addEventListener('sub', handleStoreEvent);
1482
- interceptor.addEventListener('unsub', handleStoreEvent);
1483
- interceptor.addEventListener('mount', handleStoreEvent);
1484
- interceptor.addEventListener('unmount', handleStoreEvent);
1485
- interceptor.addEventListener('notify', handleStoreEvent);
1486
- targetWindow[GLOBAL_CCSTATE_INTERCEPED_KEY] = true;
1487
- console.warn('[CCSTATE] Interceptor injected, DO NOT USE THIS IN PRODUCTION');
1488
- return interceptor;
1489
- }
1490
-
1491
- var StoreContext = react.createContext(null);
1492
- var StoreProvider = StoreContext.Provider;
1493
- function useStore() {
1494
- var store = react.useContext(StoreContext);
1495
- if (!store) {
1496
- throw new Error('Store context not found - did you forget to wrap your app with StoreProvider?');
1497
- }
1498
- return store;
1499
- }
1500
-
1501
- function useGet(atom) {
1502
- var store = useStore();
1503
- return react.useSyncExternalStore(function (fn) {
1504
- var ctrl = new AbortController();
1505
- store.sub(atom, command(fn), {
1506
- signal: ctrl.signal
1507
- });
1508
- return function () {
1509
- ctrl.abort();
1510
- };
1511
- }, function () {
1512
- return store.get(atom);
1513
- });
1514
- }
1515
-
1516
- function useSet(atom) {
1517
- var store = useStore();
1518
- if ('write' in atom) {
1519
- return function () {
1520
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
1521
- args[_key] = arguments[_key];
1522
- }
1523
- var ret = store.set.apply(store, [atom].concat(args));
1524
- return ret;
1525
- };
1526
- }
1527
- return function (value) {
1528
- store.set(atom, value);
1529
- };
1530
- }
1531
-
1532
- function useLoadable(atom) {
1533
- var promise = useGet(atom);
1534
- var _useState = react.useState({
1535
- state: 'loading'
1536
- }),
1537
- _useState2 = _slicedToArray(_useState, 2),
1538
- promiseResult = _useState2[0],
1539
- setPromiseResult = _useState2[1];
1540
- react.useEffect(function () {
1541
- var ctrl = new AbortController();
1542
- var signal = ctrl.signal;
1543
- setPromiseResult({
1544
- state: 'loading'
1545
- });
1546
- void promise.then(function (ret) {
1547
- if (signal.aborted) return;
1548
- setPromiseResult({
1549
- state: 'hasData',
1550
- data: ret
1551
- });
1552
- })["catch"](function (error) {
1553
- if (signal.aborted) return;
1554
- setPromiseResult({
1555
- state: 'hasError',
1556
- error: error
1557
- });
1558
- });
1559
- return function () {
1560
- ctrl.abort();
1561
- };
1562
- }, [promise]);
1563
- return promiseResult;
1564
- }
1565
- function useLastLoadable(atom) {
1566
- var promise = useGet(atom);
1567
- var _useState3 = react.useState({
1568
- state: 'loading'
1569
- }),
1570
- _useState4 = _slicedToArray(_useState3, 2),
1571
- promiseResult = _useState4[0],
1572
- setPromiseResult = _useState4[1];
1573
- react.useEffect(function () {
1574
- var ctrl = new AbortController();
1575
- var signal = ctrl.signal;
1576
- void promise.then(function (ret) {
1577
- if (signal.aborted) return;
1578
- setPromiseResult({
1579
- state: 'hasData',
1580
- data: ret
1581
- });
1582
- })["catch"](function (error) {
1583
- if (signal.aborted) return;
1584
- setPromiseResult({
1585
- state: 'hasError',
1586
- error: error
1587
- });
1588
- });
1589
- return function () {
1590
- ctrl.abort();
1591
- };
1592
- }, [promise]);
1593
- return promiseResult;
1594
- }
1595
-
1596
- function useResolved(atom) {
1597
- var loadable = useLoadable(atom);
1598
- return loadable.state === 'hasData' ? loadable.data : undefined;
1599
- }
1600
- function useLastResolved(atom) {
1601
- var loadable = useLastLoadable(atom);
1602
- return loadable.state === 'hasData' ? loadable.data : undefined;
1272
+ return createDebugStoreInternal(interceptor);
1603
1273
  }
1604
1274
 
1605
- exports.ConsoleInterceptor = ConsoleInterceptor;
1606
- exports.EventInterceptor = EventInterceptor;
1607
- exports.GLOBAL_CCSTATE_INTERCEPED_KEY = GLOBAL_CCSTATE_INTERCEPED_KEY;
1608
- exports.StoreEvent = StoreEvent;
1609
- exports.StoreProvider = StoreProvider;
1610
1275
  exports.command = command;
1611
1276
  exports.computed = computed;
1612
- exports.createConsoleDebugStore = createConsoleDebugStore;
1613
1277
  exports.createDebugStore = createDebugStore;
1614
1278
  exports.createStore = createStore;
1615
- exports.nestedAtomToString = nestedAtomToString;
1616
- exports.setupDevtoolsInterceptor = setupDevtoolsInterceptor;
1617
1279
  exports.state = state;
1618
- exports.useGet = useGet;
1619
- exports.useLastLoadable = useLastLoadable;
1620
- exports.useLastResolved = useLastResolved;
1621
- exports.useLoadable = useLoadable;
1622
- exports.useResolved = useResolved;
1623
- exports.useSet = useSet;