ccstate 2.2.0 → 4.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,12 +1204,15 @@ 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;
1210
+ var parsedDefaultActions = defaultActions ? new Set(defaultActions) : undefined;
1263
1211
  var parsedWatches = watches.map(function (watch) {
1264
1212
  if (typeof watch === 'string' || watch instanceof RegExp) {
1265
1213
  return {
1266
1214
  target: watch,
1267
- actions: defaultActions ? new Set(defaultActions) : undefined
1215
+ actions: parsedDefaultActions
1268
1216
  };
1269
1217
  }
1270
1218
  if ('target' in watch) {
@@ -1272,172 +1220,11 @@ function createConsoleDebugStore(watches, defaultActions) {
1272
1220
  }
1273
1221
  return {
1274
1222
  target: watch,
1275
- actions: defaultActions ? new Set(defaultActions) : undefined
1223
+ actions: parsedDefaultActions
1276
1224
  };
1277
1225
  });
1278
1226
  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;
1227
+ return createDebugStoreInternal(interceptor);
1441
1228
  }
1442
1229
 
1443
- export { ConsoleInterceptor, EventInterceptor, GLOBAL_CCSTATE_INTERCEPED_KEY, StoreEvent, createConsoleDebugStore, createDebugStore, nestedAtomToString, setupDevtoolsInterceptor };
1230
+ 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;
@@ -1123,15 +1076,12 @@ function createStore() {
1123
1076
  var listenerManager = new ListenerManager();
1124
1077
  return new StoreImpl(atomManager, listenerManager);
1125
1078
  }
1126
-
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
- });
1079
+ var defaultStore = undefined;
1080
+ function getDefaultStore() {
1081
+ if (!defaultStore) {
1082
+ defaultStore = createStore();
1083
+ }
1084
+ return defaultStore;
1135
1085
  }
1136
1086
 
1137
1087
  var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
@@ -1206,7 +1156,7 @@ var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1206
1156
  _inherits(DebugStoreImpl, _StoreImpl);
1207
1157
  return _createClass(DebugStoreImpl);
1208
1158
  }(StoreImpl);
1209
- function createDebugStore(interceptor) {
1159
+ function createDebugStoreInternal(interceptor) {
1210
1160
  var atomManager = new AtomManager({
1211
1161
  interceptor: interceptor
1212
1162
  });
@@ -1307,12 +1257,15 @@ var ConsoleInterceptor = /*#__PURE__*/_createClass(function ConsoleInterceptor(w
1307
1257
  });
1308
1258
  this.watches = watches;
1309
1259
  });
1310
- function createConsoleDebugStore(watches, defaultActions) {
1260
+ function createDebugStore() {
1261
+ var watches = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
1262
+ var defaultActions = arguments.length > 1 ? arguments[1] : undefined;
1263
+ var parsedDefaultActions = defaultActions ? new Set(defaultActions) : undefined;
1311
1264
  var parsedWatches = watches.map(function (watch) {
1312
1265
  if (typeof watch === 'string' || watch instanceof RegExp) {
1313
1266
  return {
1314
1267
  target: watch,
1315
- actions: defaultActions ? new Set(defaultActions) : undefined
1268
+ actions: parsedDefaultActions
1316
1269
  };
1317
1270
  }
1318
1271
  if ('target' in watch) {
@@ -1320,282 +1273,16 @@ function createConsoleDebugStore(watches, defaultActions) {
1320
1273
  }
1321
1274
  return {
1322
1275
  target: watch,
1323
- actions: defaultActions ? new Set(defaultActions) : undefined
1276
+ actions: parsedDefaultActions
1324
1277
  };
1325
1278
  });
1326
1279
  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 useLoadableInternal(atom, keepLastResolved) {
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
- if (!keepLastResolved) {
1544
- setPromiseResult({
1545
- state: 'loading'
1546
- });
1547
- }
1548
- void promise.then(function (ret) {
1549
- if (signal.aborted) return;
1550
- setPromiseResult({
1551
- state: 'hasData',
1552
- data: ret
1553
- });
1554
- })["catch"](function (error) {
1555
- if (signal.aborted) return;
1556
- setPromiseResult({
1557
- state: 'hasError',
1558
- error: error
1559
- });
1560
- });
1561
- return function () {
1562
- ctrl.abort();
1563
- };
1564
- }, [promise]);
1565
- return promiseResult;
1566
- }
1567
- function useLoadable(atom) {
1568
- return useLoadableInternal(atom, false);
1569
- }
1570
- function useLastLoadable(atom) {
1571
- return useLoadableInternal(atom, true);
1572
- }
1573
-
1574
- function useResolved(atom) {
1575
- var loadable = useLoadable(atom);
1576
- return loadable.state === 'hasData' ? loadable.data : undefined;
1577
- }
1578
- function useLastResolved(atom) {
1579
- var loadable = useLastLoadable(atom);
1580
- return loadable.state === 'hasData' ? loadable.data : undefined;
1280
+ return createDebugStoreInternal(interceptor);
1581
1281
  }
1582
1282
 
1583
- exports.ConsoleInterceptor = ConsoleInterceptor;
1584
- exports.EventInterceptor = EventInterceptor;
1585
- exports.GLOBAL_CCSTATE_INTERCEPED_KEY = GLOBAL_CCSTATE_INTERCEPED_KEY;
1586
- exports.StoreEvent = StoreEvent;
1587
- exports.StoreProvider = StoreProvider;
1588
1283
  exports.command = command;
1589
1284
  exports.computed = computed;
1590
- exports.createConsoleDebugStore = createConsoleDebugStore;
1591
1285
  exports.createDebugStore = createDebugStore;
1592
1286
  exports.createStore = createStore;
1593
- exports.nestedAtomToString = nestedAtomToString;
1594
- exports.setupDevtoolsInterceptor = setupDevtoolsInterceptor;
1287
+ exports.getDefaultStore = getDefaultStore;
1595
1288
  exports.state = state;
1596
- exports.useGet = useGet;
1597
- exports.useLastLoadable = useLastLoadable;
1598
- exports.useLastResolved = useLastResolved;
1599
- exports.useLoadable = useLoadable;
1600
- exports.useResolved = useResolved;
1601
- exports.useSet = useSet;