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/README.md CHANGED
@@ -7,7 +7,6 @@
7
7
  ![NPM Version](https://img.shields.io/npm/v/ccstate)
8
8
  ![npm package minimized gzipped size](https://img.shields.io/bundlejs/size/ccstate)
9
9
  [![CI](https://github.com/e7h4n/ccstate/actions/workflows/ci.yaml/badge.svg)](https://github.com/e7h4n/ccstate/actions/workflows/ci.yaml)
10
- [![CodSpeed Badge](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json)](https://codspeed.io/e7h4n/ccstate)
11
10
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
12
11
 
13
12
  CCState is a semantic, strict, and flexible state management library suitable for medium to large single-page applications with complex state management needs.
@@ -19,7 +18,7 @@ The name of CCState comes from three basic data types: computed, command, and st
19
18
  - ðŸ’Ŋ Simple & Intuitive: Crystal-clear API design with just 3 data types and 2 operations
20
19
  - ✅ Rock-solid Reliability: Comprehensive test coverage reaching 100% branch coverage
21
20
  - ðŸŠķ Ultra-lightweight: Zero dependencies, only 500 lines of core code
22
- - ðŸ’Ą Framework Agnostic: Seamlessly works with [React](docs/react.md), [Vue](docs/vue.md), or any UI framework
21
+ - ðŸ’Ą Framework Agnostic: Seamlessly works with [React](docs/react.md), [Vue](docs/vue.md), [Solid.js](docs/solid.md), [Vanilla](docs/vanilla.md), or any UI framework
23
22
  - 🚀 Blazing Fast: Optimized performance from day one, 2x-7x faster than Jotai across scenarios
24
23
 
25
24
  ## Getting Started
@@ -62,7 +61,7 @@ Use `useGet` and `useSet` hooks in React to get/set data, and use `useResolved`
62
61
 
63
62
  ```jsx
64
63
  // App.js
65
- import { useGet, useSet, useResolved } from 'ccstate';
64
+ import { useGet, useSet, useResolved } from 'ccstate-react';
66
65
  import { userId$, user$ } from './data';
67
66
 
68
67
  export default function App() {
@@ -85,29 +84,15 @@ export default function App() {
85
84
  </div>
86
85
  );
87
86
  }
88
- ```
89
-
90
- Use `createStore` and `StoreProvider` to provide a CCState store to React, all states and computations will only affect this isolated store.
91
87
 
92
- ```tsx
93
88
  // main.jsx
94
- import { createStore, StoreProvider } from 'ccstate';
95
- import { StrictMode } from 'react';
96
89
  import { createRoot } from 'react-dom/client';
97
-
98
90
  import App from './App';
99
91
 
100
92
  const rootElement = document.getElementById('root');
101
93
  const root = createRoot(rootElement);
102
94
 
103
- const store = createStore();
104
- root.render(
105
- <StrictMode>
106
- <StoreProvider value={store}>
107
- <App />
108
- </StoreProvider>
109
- </StrictMode>,
110
- );
95
+ root.render(<App />);
111
96
  ```
112
97
 
113
98
  That's it! [Click here to see the full example](https://codesandbox.io/p/sandbox/cr3xg6).
@@ -290,6 +275,14 @@ That's it! Next, you can learn how to use CCState in React.
290
275
 
291
276
  [Using in Vue](docs/vue.md)
292
277
 
278
+ ## Using in Solid.js
279
+
280
+ [Using in Solid.js](docs/solid.md)
281
+
282
+ ## Using in Vanilla
283
+
284
+ [Using in Vanilla](docs/vanilla.md)
285
+
293
286
  ### Testing & Debugging
294
287
 
295
288
  Testing Value/Computed should be as simple as testing a Map.
@@ -314,12 +307,12 @@ Here are some tips to help you better debug during testing.
314
307
  Use `ConsoleInterceptor` to log most store behaviors to the console during testing:
315
308
 
316
309
  ```typescript
317
- import { createConsoleDebugStore, state, computed, command } from 'ccstate';
310
+ import { createDebugStore, state, computed, command } from 'ccstate';
318
311
 
319
312
  const base$ = state(1, { debugLabel: 'base$' });
320
313
  const derived$ = computed((get) => get(base$) * 2);
321
314
 
322
- const store = createConsoleDebugStore([base$, 'derived'], ['set', 'sub']); // log sub & set actions
315
+ const store = createDebugStore([base$, 'derived'], ['set', 'sub']); // log sub & set actions
323
316
  store.set(base$, 1); // console: SET [V0:base$] 1
324
317
  store.sub(
325
318
  derived$,
@@ -728,7 +721,7 @@ Click show to make double enter the display state, and you can see the `set` `sh
728
721
  ret: – 14
729
722
  ```
730
723
 
731
- The abbreviation `CPT` represents `Computed` evaluation, not just a simple read operation. You can also try modifying the parameters of `createConsoleDebugStore` in the code to include `get` in the logs, and you'll find that not every `get` triggers a `Computed` evaluation.
724
+ The abbreviation `CPT` represents `Computed` evaluation, not just a simple read operation. You can also try modifying the parameters of `createDebugStore` in the code to include `get` in the logs, and you'll find that not every `get` triggers a `Computed` evaluation.
732
725
 
733
726
  Click increment to see the `set` trigger the `Computed` evaluation.
734
727
 
@@ -823,10 +816,9 @@ So, I think the only way to implement `Computed`'s effect-less is to separate th
823
816
 
824
817
  Here are some new ideas:
825
818
 
826
- - Integration with svelte / solid.js
827
- - Enhance devtools
819
+ - Integration with svelte
820
+ - Enhance debug ability
828
821
  - Support viewing current subscription graph and related atom values
829
- - Enable logging and breakpoints for specific atoms in devtools
830
822
  - Performance improvements
831
823
  - Mount atomState directly on atoms when there's only one store in the application to reduce WeakMap lookup overhead
832
824
  - Support static declaration of upstream dependencies for Computed to improve performance by disabling runtime dependency analysis
package/core/index.cjs CHANGED
@@ -1015,8 +1015,16 @@ function createStore() {
1015
1015
  var listenerManager = new ListenerManager();
1016
1016
  return new StoreImpl(atomManager, listenerManager);
1017
1017
  }
1018
+ var defaultStore = undefined;
1019
+ function getDefaultStore() {
1020
+ if (!defaultStore) {
1021
+ defaultStore = createStore();
1022
+ }
1023
+ return defaultStore;
1024
+ }
1018
1025
 
1019
1026
  exports.command = command;
1020
1027
  exports.computed = computed;
1021
1028
  exports.createStore = createStore;
1029
+ exports.getDefaultStore = getDefaultStore;
1022
1030
  exports.state = state;
package/core/index.d.cts CHANGED
@@ -48,5 +48,6 @@ type CallbackFunc<T> = Command<T, []>;
48
48
  type Subscribe = (atoms$: ReadableAtom<unknown>[] | ReadableAtom<unknown>, callback: CallbackFunc<unknown>, options?: SubscribeOptions) => () => void;
49
49
 
50
50
  declare function createStore(): Store;
51
+ declare function getDefaultStore(): Store;
51
52
 
52
- export { type Command, type Computed, type Getter, type Read, type Setter, type State, type Store, type Subscribe, type Updater, type Write, command, computed, createStore, state };
53
+ export { type Command, type Computed, type Getter, type Read, type Setter, type State, type Store, type Subscribe, type Updater, type Write, command, computed, createStore, getDefaultStore, state };
package/core/index.d.ts CHANGED
@@ -48,5 +48,6 @@ type CallbackFunc<T> = Command<T, []>;
48
48
  type Subscribe = (atoms$: ReadableAtom<unknown>[] | ReadableAtom<unknown>, callback: CallbackFunc<unknown>, options?: SubscribeOptions) => () => void;
49
49
 
50
50
  declare function createStore(): Store;
51
+ declare function getDefaultStore(): Store;
51
52
 
52
- export { type Command, type Computed, type Getter, type Read, type Setter, type State, type Store, type Subscribe, type Updater, type Write, command, computed, createStore, state };
53
+ export { type Command, type Computed, type Getter, type Read, type Setter, type State, type Store, type Subscribe, type Updater, type Write, command, computed, createStore, getDefaultStore, state };
package/core/index.js CHANGED
@@ -1013,5 +1013,12 @@ function createStore() {
1013
1013
  var listenerManager = new ListenerManager();
1014
1014
  return new StoreImpl(atomManager, listenerManager);
1015
1015
  }
1016
+ var defaultStore = undefined;
1017
+ function getDefaultStore() {
1018
+ if (!defaultStore) {
1019
+ defaultStore = createStore();
1020
+ }
1021
+ return defaultStore;
1022
+ }
1016
1023
 
1017
- export { command, computed, createStore, state };
1024
+ export { command, computed, createStore, getDefaultStore, state };
package/debug/index.cjs CHANGED
@@ -1,15 +1,5 @@
1
1
  'use strict';
2
2
 
3
- function nestedAtomToString(atoms) {
4
- return atoms.map(function (atom) {
5
- var _atom$debugLabel;
6
- if (Array.isArray(atom)) {
7
- return nestedAtomToString(atom);
8
- }
9
- return (_atom$debugLabel = atom.debugLabel) !== null && _atom$debugLabel !== void 0 ? _atom$debugLabel : 'anonymous';
10
- });
11
- }
12
-
13
3
  function _arrayLikeToArray(r, a) {
14
4
  (null == a || a > r.length) && (a = r.length);
15
5
  for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
@@ -31,13 +21,6 @@ function _callSuper(t, o, e) {
31
21
  function _classCallCheck(a, n) {
32
22
  if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
33
23
  }
34
- function _construct(t, e, r) {
35
- if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments);
36
- var o = [null];
37
- o.push.apply(o, e);
38
- var p = new (t.bind.apply(t, o))();
39
- return r && _setPrototypeOf(p, r.prototype), p;
40
- }
41
24
  function _defineProperties(e, r) {
42
25
  for (var t = 0; t < r.length; t++) {
43
26
  var o = r[t];
@@ -131,13 +114,6 @@ function _inherits(t, e) {
131
114
  writable: !1
132
115
  }), e && _setPrototypeOf(t, e);
133
116
  }
134
- function _isNativeFunction(t) {
135
- try {
136
- return -1 !== Function.toString.call(t).indexOf("[native code]");
137
- } catch (n) {
138
- return "function" == typeof t;
139
- }
140
- }
141
117
  function _isNativeReflectConstruct() {
142
118
  try {
143
119
  var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
@@ -523,15 +499,6 @@ function _toPropertyKey(t) {
523
499
  var i = _toPrimitive(t, "string");
524
500
  return "symbol" == typeof i ? i : i + "";
525
501
  }
526
- function _typeof(o) {
527
- "@babel/helpers - typeof";
528
-
529
- return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
530
- return typeof o;
531
- } : function (o) {
532
- return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
533
- }, _typeof(o);
534
- }
535
502
  function _unsupportedIterableToArray(r, a) {
536
503
  if (r) {
537
504
  if ("string" == typeof r) return _arrayLikeToArray(r, a);
@@ -539,28 +506,6 @@ function _unsupportedIterableToArray(r, a) {
539
506
  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;
540
507
  }
541
508
  }
542
- function _wrapNativeSuper(t) {
543
- var r = "function" == typeof Map ? new Map() : void 0;
544
- return _wrapNativeSuper = function (t) {
545
- if (null === t || !_isNativeFunction(t)) return t;
546
- if ("function" != typeof t) throw new TypeError("Super expression must either be null or a function");
547
- if (void 0 !== r) {
548
- if (r.has(t)) return r.get(t);
549
- r.set(t, Wrapper);
550
- }
551
- function Wrapper() {
552
- return _construct(t, arguments, _getPrototypeOf(this).constructor);
553
- }
554
- return Wrapper.prototype = Object.create(t.prototype, {
555
- constructor: {
556
- value: Wrapper,
557
- enumerable: !1,
558
- writable: !0,
559
- configurable: !0
560
- }
561
- }), _setPrototypeOf(Wrapper, t);
562
- }, _wrapNativeSuper(t);
563
- }
564
509
 
565
510
  function canReadAsCompute(atom) {
566
511
  return 'read' in atom;
@@ -1160,7 +1105,7 @@ var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1160
1105
  _inherits(DebugStoreImpl, _StoreImpl);
1161
1106
  return _createClass(DebugStoreImpl);
1162
1107
  }(StoreImpl);
1163
- function createDebugStore(interceptor) {
1108
+ function createDebugStoreInternal(interceptor) {
1164
1109
  var atomManager = new AtomManager({
1165
1110
  interceptor: interceptor
1166
1111
  });
@@ -1261,12 +1206,15 @@ var ConsoleInterceptor = /*#__PURE__*/_createClass(function ConsoleInterceptor(w
1261
1206
  });
1262
1207
  this.watches = watches;
1263
1208
  });
1264
- function createConsoleDebugStore(watches, defaultActions) {
1209
+ function createDebugStore() {
1210
+ var watches = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
1211
+ var defaultActions = arguments.length > 1 ? arguments[1] : undefined;
1212
+ var parsedDefaultActions = defaultActions ? new Set(defaultActions) : undefined;
1265
1213
  var parsedWatches = watches.map(function (watch) {
1266
1214
  if (typeof watch === 'string' || watch instanceof RegExp) {
1267
1215
  return {
1268
1216
  target: watch,
1269
- actions: defaultActions ? new Set(defaultActions) : undefined
1217
+ actions: parsedDefaultActions
1270
1218
  };
1271
1219
  }
1272
1220
  if ('target' in watch) {
@@ -1274,179 +1222,11 @@ function createConsoleDebugStore(watches, defaultActions) {
1274
1222
  }
1275
1223
  return {
1276
1224
  target: watch,
1277
- actions: defaultActions ? new Set(defaultActions) : undefined
1225
+ actions: parsedDefaultActions
1278
1226
  };
1279
1227
  });
1280
1228
  var interceptor = new ConsoleInterceptor(parsedWatches);
1281
- return createDebugStore(interceptor);
1282
- }
1283
-
1284
- var StoreEvent = /*#__PURE__*/function (_Event) {
1285
- function StoreEvent(type, eventId, targetAtom, state, time, args, result) {
1286
- var _this;
1287
- _classCallCheck(this, StoreEvent);
1288
- _this = _callSuper(this, StoreEvent, [type]);
1289
- _this.eventId = eventId;
1290
- _this.targetAtom = targetAtom;
1291
- _this.state = state;
1292
- _this.time = time;
1293
- _this.args = args;
1294
- _this.result = result;
1295
- return _this;
1296
- }
1297
- _inherits(StoreEvent, _Event);
1298
- return _createClass(StoreEvent);
1299
- }(/*#__PURE__*/_wrapNativeSuper(Event));
1300
-
1301
- var EventInterceptor = /*#__PURE__*/function () {
1302
- function EventInterceptor() {
1303
- var _this = this;
1304
- _classCallCheck(this, EventInterceptor);
1305
- _defineProperty(this, "traceId", 0);
1306
- _defineProperty(this, "events", new EventTarget());
1307
- _defineProperty(this, "get", function (atom$, fn) {
1308
- return _this.wrapWithTrace(fn, function (eventId, time) {
1309
- _this.createEvent('get', eventId, atom$.toString(), time, 'begin', [], undefined);
1310
- }, function (eventId, time, result) {
1311
- _this.createEvent('get', eventId, atom$.toString(), time, 'success', [], result);
1312
- }, function (eventId, time, error) {
1313
- _this.createEvent('get', eventId, atom$.toString(), time, 'error', [], error);
1314
- });
1315
- });
1316
- _defineProperty(this, "computed", function (atom$, fn) {
1317
- return _this.wrapWithTrace(fn, function (eventId, time) {
1318
- _this.createEvent('computed', eventId, atom$.toString(), time, 'begin', [], undefined);
1319
- }, function (eventId, time, result) {
1320
- _this.createEvent('computed', eventId, atom$.toString(), time, 'success', [], result);
1321
- }, function (eventId, time, error) {
1322
- _this.createEvent('computed', eventId, atom$.toString(), time, 'error', [], error);
1323
- });
1324
- });
1325
- _defineProperty(this, "set", function (atom$, fn) {
1326
- for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
1327
- args[_key - 2] = arguments[_key];
1328
- }
1329
- return _this.wrapWithTrace(fn, function (eventId, time) {
1330
- _this.createEvent('set', eventId, atom$.toString(), time, 'begin', args, undefined);
1331
- }, function (eventId, time, result) {
1332
- _this.createEvent('set', eventId, atom$.toString(), time, 'success', args, result);
1333
- }, function (eventId, time, error) {
1334
- _this.createEvent('set', eventId, atom$.toString(), time, 'error', args, error);
1335
- });
1336
- });
1337
- _defineProperty(this, "sub", function (atom$, callback$, fn) {
1338
- var eventId = _this.traceId++;
1339
- _this.createEvent('sub', eventId, atom$.toString(), performance.now(), 'begin', [callback$.toString()], undefined);
1340
- fn();
1341
- _this.createEvent('sub', eventId, atom$.toString(), performance.now(), 'success', [callback$.toString()], undefined);
1342
- });
1343
- _defineProperty(this, "unsub", function (atom$, callback$, fn) {
1344
- var eventId = _this.traceId++;
1345
- _this.createEvent('unsub', eventId, atom$.toString(), performance.now(), 'begin', [callback$.toString()], undefined);
1346
- fn();
1347
- _this.createEvent('unsub', eventId, atom$.toString(), performance.now(), 'success', [callback$.toString()], undefined);
1348
- });
1349
- _defineProperty(this, "mount", function (atom$) {
1350
- var eventId = _this.traceId++;
1351
- _this.createEvent('mount', eventId, atom$.toString(), performance.now(), 'begin', [], undefined);
1352
- });
1353
- _defineProperty(this, "unmount", function (atom$) {
1354
- var eventId = _this.traceId++;
1355
- _this.createEvent('unmount', eventId, atom$.toString(), performance.now(), 'begin', [], undefined);
1356
- });
1357
- _defineProperty(this, "notify", function (callback$, fn) {
1358
- var eventId = _this.traceId++;
1359
- _this.createEvent('notify', eventId, callback$.toString(), performance.now(), 'begin', [], undefined);
1360
- var ret = fn();
1361
- _this.createEvent('notify', eventId, callback$.toString(), performance.now(), 'success', [], ret);
1362
- });
1363
- }
1364
- return _createClass(EventInterceptor, [{
1365
- key: "createEvent",
1366
- value: function createEvent(type, eventId, atom, time, state, args, result) {
1367
- var event = new StoreEvent(type, eventId, atom, state, time, args, result);
1368
- this.events.dispatchEvent(event);
1369
- return event;
1370
- }
1371
- }, {
1372
- key: "wrapWithTrace",
1373
- value: function wrapWithTrace(fn, createBeginEvent, createSuccessEvent, createErrorEvent) {
1374
- var eventId = this.traceId++;
1375
- createBeginEvent(eventId, performance.now());
1376
- try {
1377
- var result = fn();
1378
- createSuccessEvent(eventId, performance.now(), result);
1379
- return result;
1380
- } catch (e) {
1381
- createErrorEvent(eventId, performance.now(), e);
1382
- throw e;
1383
- }
1384
- }
1385
- }, {
1386
- key: "addEventListener",
1387
- value: function addEventListener(type, listener, options) {
1388
- this.events.addEventListener(type, listener, options);
1389
- }
1390
- }, {
1391
- key: "removeEventListener",
1392
- value: function removeEventListener(type, listener, options) {
1393
- this.events.removeEventListener(type, listener, options);
1394
- }
1395
- }]);
1396
- }();
1397
-
1398
- var GLOBAL_CCSTATE_INTERCEPED_KEY = '__CCSTATE_INTERCEPED__';
1399
- function setupDevtoolsInterceptor(targetWindow, signal) {
1400
- var interceptor = new EventInterceptor();
1401
- var watchedAtoms = new Set();
1402
- targetWindow.addEventListener('message', function (_ref) {
1403
- var data = _ref.data;
1404
- if (!data || _typeof(data) !== 'object' || !('source' in data) || data.source !== 'ccstate-devtools') {
1405
- return;
1406
- }
1407
- var payload = data.payload;
1408
- watchedAtoms.add(payload.args[0]);
1409
- }, {
1410
- signal: signal
1411
- });
1412
- function handleStoreEvent(event) {
1413
- var debugLabel = event.targetAtom.substring(event.targetAtom.indexOf(':') + 1);
1414
- if (watchedAtoms.has(debugLabel)) {
1415
- console.group("[CCState] ".concat(event.type, " ").concat(event.targetAtom, " ").concat(event.state));
1416
- console.log('args', event.args);
1417
- console.log('result', event.result);
1418
- console.groupEnd();
1419
- }
1420
- var message = {
1421
- source: 'ccstate-store',
1422
- payload: {
1423
- type: event.type,
1424
- eventId: event.eventId,
1425
- targetAtom: event.targetAtom,
1426
- time: event.time,
1427
- state: event.state
1428
- }
1429
- };
1430
- targetWindow.postMessage(message);
1431
- }
1432
- interceptor.addEventListener('get', handleStoreEvent);
1433
- interceptor.addEventListener('computed', handleStoreEvent);
1434
- interceptor.addEventListener('set', handleStoreEvent);
1435
- interceptor.addEventListener('sub', handleStoreEvent);
1436
- interceptor.addEventListener('unsub', handleStoreEvent);
1437
- interceptor.addEventListener('mount', handleStoreEvent);
1438
- interceptor.addEventListener('unmount', handleStoreEvent);
1439
- interceptor.addEventListener('notify', handleStoreEvent);
1440
- targetWindow[GLOBAL_CCSTATE_INTERCEPED_KEY] = true;
1441
- console.warn('[CCSTATE] Interceptor injected, DO NOT USE THIS IN PRODUCTION');
1442
- return interceptor;
1229
+ return createDebugStoreInternal(interceptor);
1443
1230
  }
1444
1231
 
1445
- exports.ConsoleInterceptor = ConsoleInterceptor;
1446
- exports.EventInterceptor = EventInterceptor;
1447
- exports.GLOBAL_CCSTATE_INTERCEPED_KEY = GLOBAL_CCSTATE_INTERCEPED_KEY;
1448
- exports.StoreEvent = StoreEvent;
1449
- exports.createConsoleDebugStore = createConsoleDebugStore;
1450
1232
  exports.createDebugStore = createDebugStore;
1451
- exports.nestedAtomToString = nestedAtomToString;
1452
- exports.setupDevtoolsInterceptor = setupDevtoolsInterceptor;
package/debug/index.d.cts CHANGED
@@ -29,11 +29,6 @@ interface Command<T, Args extends unknown[]> {
29
29
  }
30
30
  type ReadableAtom<T> = State<T> | Computed<T>;
31
31
 
32
- type NestedAtom = (State<unknown> | Computed<unknown> | Command<unknown, unknown[]> | NestedAtom)[];
33
- type NestedString = (string | NestedString)[];
34
-
35
- declare function nestedAtomToString(atoms: NestedAtom): NestedString;
36
-
37
32
  interface Store {
38
33
  get: Getter;
39
34
  set: Setter;
@@ -44,28 +39,9 @@ interface SubscribeOptions {
44
39
  }
45
40
  type CallbackFunc<T> = Command<T, []>;
46
41
  type Subscribe = (atoms$: ReadableAtom<unknown>[] | ReadableAtom<unknown>, callback: CallbackFunc<unknown>, options?: SubscribeOptions) => () => void;
47
- type InterceptorGet = <T>(atom$: State<T> | Computed<T>, fn: () => T) => void;
48
- interface InterceptorSet {
49
- <T, Args extends unknown[]>(func$: Command<T, Args>, fn: () => T, ...args: Args): void;
50
- <T>(value$: State<T>, fn: () => void, val: T | Updater<T>): void;
51
- }
52
- type InterceptorSub = <T>(atom$: ReadableAtom<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
53
- type InterceptorUnsub = <T>(atom$: ReadableAtom<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
54
- type InterceptorMount = <T>(atom$: ReadableAtom<T>) => void;
55
- type InterceptorUnmount = <T>(atom$: ReadableAtom<T>) => void;
56
- type InterceptorNotify = <T>(callback$: CallbackFunc<T>, fn: () => T) => void;
57
- type InterceptorComputed = <T>(atom$: Computed<T>, fn: () => T) => void;
58
- interface StoreInterceptor {
59
- get?: InterceptorGet;
60
- set?: InterceptorSet;
61
- sub?: InterceptorSub;
62
- unsub?: InterceptorUnsub;
63
- mount?: InterceptorMount;
64
- unmount?: InterceptorUnmount;
65
- notify?: InterceptorNotify;
66
- computed?: InterceptorComputed;
67
- }
42
+ type StoreEventType = 'set' | 'get' | 'sub' | 'unsub' | 'mount' | 'unmount' | 'notify' | 'computed';
68
43
 
44
+ type NestedAtom = (State<unknown> | Computed<unknown> | Command<unknown, unknown[]> | NestedAtom)[];
69
45
  interface DebugStore extends Store {
70
46
  getReadDependencies: (atom: Computed<unknown>) => NestedAtom;
71
47
  getReadDependents: (atom: State<unknown> | Computed<unknown>) => NestedAtom;
@@ -73,62 +49,10 @@ interface DebugStore extends Store {
73
49
  getSubscribeGraph: () => NestedAtom;
74
50
  }
75
51
 
76
- declare function createDebugStore(interceptor?: StoreInterceptor): DebugStore;
77
-
78
- type StoreEventType = 'set' | 'get' | 'sub' | 'unsub' | 'mount' | 'unmount' | 'notify' | 'computed';
79
-
80
52
  interface AtomWatch {
81
53
  target: State<unknown> | Computed<unknown> | Command<unknown, unknown[]> | string | RegExp;
82
54
  actions?: Set<StoreEventType>;
83
55
  }
84
- declare class ConsoleInterceptor implements StoreInterceptor {
85
- private readonly watches;
86
- constructor(watches: AtomWatch[]);
87
- private shouldLog;
88
- get: <T>(atom$: State<T> | Computed<T>, fn: () => T) => void;
89
- computed: <T>(atom$: Computed<T>, fn: () => T) => void;
90
- set: <T, Args extends unknown[]>(atom$: State<T> | Command<T, Args>, fn: () => T, ...args: Args | [T | Updater<T>]) => void;
91
- sub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
92
- unsub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
93
- mount: <T>(atom$: State<T> | Computed<T>) => void;
94
- unmount: <T>(atom$: State<T> | Computed<T>) => void;
95
- notify: <T>(callback$: CallbackFunc<T>, fn: () => T) => void;
96
- }
97
- declare function createConsoleDebugStore(watches: (AtomWatch | string | RegExp | State<unknown> | Computed<unknown> | Command<unknown, unknown[]>)[], defaultActions?: StoreEventType[]): DebugStore;
98
-
99
- declare class StoreEvent extends Event {
100
- readonly eventId: number;
101
- readonly targetAtom: string;
102
- readonly state: 'begin' | 'success' | 'error';
103
- readonly time: DOMHighResTimeStamp;
104
- readonly args: unknown[];
105
- readonly result: unknown;
106
- constructor(type: StoreEventType, eventId: number, targetAtom: string, state: 'begin' | 'success' | 'error', time: DOMHighResTimeStamp, args: unknown[], result: unknown);
107
- }
108
-
109
- declare class EventInterceptor implements StoreInterceptor {
110
- private traceId;
111
- private events;
112
- private createEvent;
113
- private wrapWithTrace;
114
- addEventListener(type: StoreEventType, listener: (event: StoreEvent) => void, options?: AddEventListenerOptions | boolean): void;
115
- removeEventListener(type: StoreEventType, listener: (event: StoreEvent) => void, options?: EventListenerOptions | boolean): void;
116
- get: <T>(atom$: State<T> | Computed<T>, fn: () => T) => T;
117
- computed: <T>(atom$: Computed<T>, fn: () => T) => T;
118
- set: <T, Args extends unknown[]>(atom$: State<T> | Command<T, Args>, fn: () => T, ...args: Args | [T | Updater<T>]) => T;
119
- sub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
120
- unsub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
121
- mount: <T>(atom$: State<T> | Computed<T>) => void;
122
- unmount: <T>(atom$: State<T> | Computed<T>) => void;
123
- notify: <T>(callback$: CallbackFunc<T>, fn: () => T) => void;
124
- }
125
-
126
- type PackedEventMessage = Pick<StoreEvent, 'type' | 'eventId' | 'targetAtom' | 'time' | 'state'>;
127
- interface DevToolsHookMessage {
128
- source: 'ccstate-store';
129
- payload: PackedEventMessage;
130
- }
131
- declare const GLOBAL_CCSTATE_INTERCEPED_KEY = "__CCSTATE_INTERCEPED__";
132
- declare function setupDevtoolsInterceptor(targetWindow: Window, signal?: AbortSignal): EventInterceptor;
56
+ declare function createDebugStore(watches?: (AtomWatch | string | RegExp | State<unknown> | Computed<unknown> | Command<unknown, unknown[]>)[], defaultActions?: StoreEventType[]): DebugStore;
133
57
 
134
- export { ConsoleInterceptor, type DebugStore, type DevToolsHookMessage, EventInterceptor, GLOBAL_CCSTATE_INTERCEPED_KEY, type PackedEventMessage, StoreEvent, type StoreEventType, type StoreInterceptor, createConsoleDebugStore, createDebugStore, nestedAtomToString, setupDevtoolsInterceptor };
58
+ export { type DebugStore, createDebugStore };
package/debug/index.d.ts CHANGED
@@ -29,11 +29,6 @@ interface Command<T, Args extends unknown[]> {
29
29
  }
30
30
  type ReadableAtom<T> = State<T> | Computed<T>;
31
31
 
32
- type NestedAtom = (State<unknown> | Computed<unknown> | Command<unknown, unknown[]> | NestedAtom)[];
33
- type NestedString = (string | NestedString)[];
34
-
35
- declare function nestedAtomToString(atoms: NestedAtom): NestedString;
36
-
37
32
  interface Store {
38
33
  get: Getter;
39
34
  set: Setter;
@@ -44,28 +39,9 @@ interface SubscribeOptions {
44
39
  }
45
40
  type CallbackFunc<T> = Command<T, []>;
46
41
  type Subscribe = (atoms$: ReadableAtom<unknown>[] | ReadableAtom<unknown>, callback: CallbackFunc<unknown>, options?: SubscribeOptions) => () => void;
47
- type InterceptorGet = <T>(atom$: State<T> | Computed<T>, fn: () => T) => void;
48
- interface InterceptorSet {
49
- <T, Args extends unknown[]>(func$: Command<T, Args>, fn: () => T, ...args: Args): void;
50
- <T>(value$: State<T>, fn: () => void, val: T | Updater<T>): void;
51
- }
52
- type InterceptorSub = <T>(atom$: ReadableAtom<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
53
- type InterceptorUnsub = <T>(atom$: ReadableAtom<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
54
- type InterceptorMount = <T>(atom$: ReadableAtom<T>) => void;
55
- type InterceptorUnmount = <T>(atom$: ReadableAtom<T>) => void;
56
- type InterceptorNotify = <T>(callback$: CallbackFunc<T>, fn: () => T) => void;
57
- type InterceptorComputed = <T>(atom$: Computed<T>, fn: () => T) => void;
58
- interface StoreInterceptor {
59
- get?: InterceptorGet;
60
- set?: InterceptorSet;
61
- sub?: InterceptorSub;
62
- unsub?: InterceptorUnsub;
63
- mount?: InterceptorMount;
64
- unmount?: InterceptorUnmount;
65
- notify?: InterceptorNotify;
66
- computed?: InterceptorComputed;
67
- }
42
+ type StoreEventType = 'set' | 'get' | 'sub' | 'unsub' | 'mount' | 'unmount' | 'notify' | 'computed';
68
43
 
44
+ type NestedAtom = (State<unknown> | Computed<unknown> | Command<unknown, unknown[]> | NestedAtom)[];
69
45
  interface DebugStore extends Store {
70
46
  getReadDependencies: (atom: Computed<unknown>) => NestedAtom;
71
47
  getReadDependents: (atom: State<unknown> | Computed<unknown>) => NestedAtom;
@@ -73,62 +49,10 @@ interface DebugStore extends Store {
73
49
  getSubscribeGraph: () => NestedAtom;
74
50
  }
75
51
 
76
- declare function createDebugStore(interceptor?: StoreInterceptor): DebugStore;
77
-
78
- type StoreEventType = 'set' | 'get' | 'sub' | 'unsub' | 'mount' | 'unmount' | 'notify' | 'computed';
79
-
80
52
  interface AtomWatch {
81
53
  target: State<unknown> | Computed<unknown> | Command<unknown, unknown[]> | string | RegExp;
82
54
  actions?: Set<StoreEventType>;
83
55
  }
84
- declare class ConsoleInterceptor implements StoreInterceptor {
85
- private readonly watches;
86
- constructor(watches: AtomWatch[]);
87
- private shouldLog;
88
- get: <T>(atom$: State<T> | Computed<T>, fn: () => T) => void;
89
- computed: <T>(atom$: Computed<T>, fn: () => T) => void;
90
- set: <T, Args extends unknown[]>(atom$: State<T> | Command<T, Args>, fn: () => T, ...args: Args | [T | Updater<T>]) => void;
91
- sub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
92
- unsub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
93
- mount: <T>(atom$: State<T> | Computed<T>) => void;
94
- unmount: <T>(atom$: State<T> | Computed<T>) => void;
95
- notify: <T>(callback$: CallbackFunc<T>, fn: () => T) => void;
96
- }
97
- declare function createConsoleDebugStore(watches: (AtomWatch | string | RegExp | State<unknown> | Computed<unknown> | Command<unknown, unknown[]>)[], defaultActions?: StoreEventType[]): DebugStore;
98
-
99
- declare class StoreEvent extends Event {
100
- readonly eventId: number;
101
- readonly targetAtom: string;
102
- readonly state: 'begin' | 'success' | 'error';
103
- readonly time: DOMHighResTimeStamp;
104
- readonly args: unknown[];
105
- readonly result: unknown;
106
- constructor(type: StoreEventType, eventId: number, targetAtom: string, state: 'begin' | 'success' | 'error', time: DOMHighResTimeStamp, args: unknown[], result: unknown);
107
- }
108
-
109
- declare class EventInterceptor implements StoreInterceptor {
110
- private traceId;
111
- private events;
112
- private createEvent;
113
- private wrapWithTrace;
114
- addEventListener(type: StoreEventType, listener: (event: StoreEvent) => void, options?: AddEventListenerOptions | boolean): void;
115
- removeEventListener(type: StoreEventType, listener: (event: StoreEvent) => void, options?: EventListenerOptions | boolean): void;
116
- get: <T>(atom$: State<T> | Computed<T>, fn: () => T) => T;
117
- computed: <T>(atom$: Computed<T>, fn: () => T) => T;
118
- set: <T, Args extends unknown[]>(atom$: State<T> | Command<T, Args>, fn: () => T, ...args: Args | [T | Updater<T>]) => T;
119
- sub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
120
- unsub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
121
- mount: <T>(atom$: State<T> | Computed<T>) => void;
122
- unmount: <T>(atom$: State<T> | Computed<T>) => void;
123
- notify: <T>(callback$: CallbackFunc<T>, fn: () => T) => void;
124
- }
125
-
126
- type PackedEventMessage = Pick<StoreEvent, 'type' | 'eventId' | 'targetAtom' | 'time' | 'state'>;
127
- interface DevToolsHookMessage {
128
- source: 'ccstate-store';
129
- payload: PackedEventMessage;
130
- }
131
- declare const GLOBAL_CCSTATE_INTERCEPED_KEY = "__CCSTATE_INTERCEPED__";
132
- declare function setupDevtoolsInterceptor(targetWindow: Window, signal?: AbortSignal): EventInterceptor;
56
+ declare function createDebugStore(watches?: (AtomWatch | string | RegExp | State<unknown> | Computed<unknown> | Command<unknown, unknown[]>)[], defaultActions?: StoreEventType[]): DebugStore;
133
57
 
134
- export { ConsoleInterceptor, type DebugStore, type DevToolsHookMessage, EventInterceptor, GLOBAL_CCSTATE_INTERCEPED_KEY, type PackedEventMessage, StoreEvent, type StoreEventType, type StoreInterceptor, createConsoleDebugStore, createDebugStore, nestedAtomToString, setupDevtoolsInterceptor };
58
+ export { type DebugStore, createDebugStore };