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/index.d.cts CHANGED
@@ -1,5 +1,3 @@
1
- import * as react from 'react';
2
-
3
1
  type Updater<T> = (current: T) => T;
4
2
  interface Setter {
5
3
  <T>(state: State<T>, val: T | Updater<T>): void;
@@ -48,35 +46,11 @@ interface SubscribeOptions {
48
46
  }
49
47
  type CallbackFunc<T> = Command<T, []>;
50
48
  type Subscribe = (atoms$: ReadableAtom<unknown>[] | ReadableAtom<unknown>, callback: CallbackFunc<unknown>, options?: SubscribeOptions) => () => void;
51
- type InterceptorGet = <T>(atom$: State<T> | Computed<T>, fn: () => T) => void;
52
- interface InterceptorSet {
53
- <T, Args extends unknown[]>(func$: Command<T, Args>, fn: () => T, ...args: Args): void;
54
- <T>(value$: State<T>, fn: () => void, val: T | Updater<T>): void;
55
- }
56
- type InterceptorSub = <T>(atom$: ReadableAtom<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
57
- type InterceptorUnsub = <T>(atom$: ReadableAtom<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
58
- type InterceptorMount = <T>(atom$: ReadableAtom<T>) => void;
59
- type InterceptorUnmount = <T>(atom$: ReadableAtom<T>) => void;
60
- type InterceptorNotify = <T>(callback$: CallbackFunc<T>, fn: () => T) => void;
61
- type InterceptorComputed = <T>(atom$: Computed<T>, fn: () => T) => void;
62
- interface StoreInterceptor {
63
- get?: InterceptorGet;
64
- set?: InterceptorSet;
65
- sub?: InterceptorSub;
66
- unsub?: InterceptorUnsub;
67
- mount?: InterceptorMount;
68
- unmount?: InterceptorUnmount;
69
- notify?: InterceptorNotify;
70
- computed?: InterceptorComputed;
71
- }
49
+ type StoreEventType = 'set' | 'get' | 'sub' | 'unsub' | 'mount' | 'unmount' | 'notify' | 'computed';
72
50
 
73
51
  declare function createStore(): Store;
74
52
 
75
53
  type NestedAtom = (State<unknown> | Computed<unknown> | Command<unknown, unknown[]> | NestedAtom)[];
76
- type NestedString = (string | NestedString)[];
77
-
78
- declare function nestedAtomToString(atoms: NestedAtom): NestedString;
79
-
80
54
  interface DebugStore extends Store {
81
55
  getReadDependencies: (atom: Computed<unknown>) => NestedAtom;
82
56
  getReadDependents: (atom: State<unknown> | Computed<unknown>) => NestedAtom;
@@ -84,84 +58,10 @@ interface DebugStore extends Store {
84
58
  getSubscribeGraph: () => NestedAtom;
85
59
  }
86
60
 
87
- declare function createDebugStore(interceptor?: StoreInterceptor): DebugStore;
88
-
89
- type StoreEventType = 'set' | 'get' | 'sub' | 'unsub' | 'mount' | 'unmount' | 'notify' | 'computed';
90
-
91
61
  interface AtomWatch {
92
62
  target: State<unknown> | Computed<unknown> | Command<unknown, unknown[]> | string | RegExp;
93
63
  actions?: Set<StoreEventType>;
94
64
  }
95
- declare class ConsoleInterceptor implements StoreInterceptor {
96
- private readonly watches;
97
- constructor(watches: AtomWatch[]);
98
- private shouldLog;
99
- get: <T>(atom$: State<T> | Computed<T>, fn: () => T) => void;
100
- computed: <T>(atom$: Computed<T>, fn: () => T) => void;
101
- set: <T, Args extends unknown[]>(atom$: State<T> | Command<T, Args>, fn: () => T, ...args: Args | [T | Updater<T>]) => void;
102
- sub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
103
- unsub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
104
- mount: <T>(atom$: State<T> | Computed<T>) => void;
105
- unmount: <T>(atom$: State<T> | Computed<T>) => void;
106
- notify: <T>(callback$: CallbackFunc<T>, fn: () => T) => void;
107
- }
108
- declare function createConsoleDebugStore(watches: (AtomWatch | string | RegExp | State<unknown> | Computed<unknown> | Command<unknown, unknown[]>)[], defaultActions?: StoreEventType[]): DebugStore;
109
-
110
- declare class StoreEvent extends Event {
111
- readonly eventId: number;
112
- readonly targetAtom: string;
113
- readonly state: 'begin' | 'success' | 'error';
114
- readonly time: DOMHighResTimeStamp;
115
- readonly args: unknown[];
116
- readonly result: unknown;
117
- constructor(type: StoreEventType, eventId: number, targetAtom: string, state: 'begin' | 'success' | 'error', time: DOMHighResTimeStamp, args: unknown[], result: unknown);
118
- }
119
-
120
- declare class EventInterceptor implements StoreInterceptor {
121
- private traceId;
122
- private events;
123
- private createEvent;
124
- private wrapWithTrace;
125
- addEventListener(type: StoreEventType, listener: (event: StoreEvent) => void, options?: AddEventListenerOptions | boolean): void;
126
- removeEventListener(type: StoreEventType, listener: (event: StoreEvent) => void, options?: EventListenerOptions | boolean): void;
127
- get: <T>(atom$: State<T> | Computed<T>, fn: () => T) => T;
128
- computed: <T>(atom$: Computed<T>, fn: () => T) => T;
129
- set: <T, Args extends unknown[]>(atom$: State<T> | Command<T, Args>, fn: () => T, ...args: Args | [T | Updater<T>]) => T;
130
- sub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
131
- unsub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
132
- mount: <T>(atom$: State<T> | Computed<T>) => void;
133
- unmount: <T>(atom$: State<T> | Computed<T>) => void;
134
- notify: <T>(callback$: CallbackFunc<T>, fn: () => T) => void;
135
- }
136
-
137
- type PackedEventMessage = Pick<StoreEvent, 'type' | 'eventId' | 'targetAtom' | 'time' | 'state'>;
138
- interface DevToolsHookMessage {
139
- source: 'ccstate-store';
140
- payload: PackedEventMessage;
141
- }
142
- declare const GLOBAL_CCSTATE_INTERCEPED_KEY = "__CCSTATE_INTERCEPED__";
143
- declare function setupDevtoolsInterceptor(targetWindow: Window, signal?: AbortSignal): EventInterceptor;
144
-
145
- declare function useGet<T>(atom: State<T> | Computed<T>): T;
146
-
147
- declare function useSet<T>(atom: State<T>): (value: T | Updater<T>) => void;
148
- declare function useSet<T, ARGS extends unknown[]>(atom: Command<T, ARGS>): (...args: ARGS) => T;
149
-
150
- declare function useResolved<T>(atom: State<Promise<T>> | Computed<Promise<T>>): T | undefined;
151
- declare function useLastResolved<T>(atom: State<Promise<T>> | Computed<Promise<T>>): T | undefined;
152
-
153
- type Loadable<T> = {
154
- state: 'loading';
155
- } | {
156
- state: 'hasData';
157
- data: T;
158
- } | {
159
- state: 'hasError';
160
- error: unknown;
161
- };
162
- declare function useLoadable<T>(atom: State<Promise<T>> | Computed<Promise<T>>): Loadable<T>;
163
- declare function useLastLoadable<T>(atom: State<Promise<T>> | Computed<Promise<T>>): Loadable<T>;
164
-
165
- declare const StoreProvider: react.Provider<Store | null>;
65
+ declare function createDebugStore(watches?: (AtomWatch | string | RegExp | State<unknown> | Computed<unknown> | Command<unknown, unknown[]>)[], defaultActions?: StoreEventType[]): DebugStore;
166
66
 
167
- export { type Command, type Computed, ConsoleInterceptor, type DebugStore, type DevToolsHookMessage, EventInterceptor, GLOBAL_CCSTATE_INTERCEPED_KEY, type Getter, type PackedEventMessage, type Read, type Setter, type State, type Store, StoreEvent, type StoreEventType, type StoreInterceptor, StoreProvider, type Subscribe, type Updater, type Write, command, computed, createConsoleDebugStore, createDebugStore, createStore, nestedAtomToString, setupDevtoolsInterceptor, state, useGet, useLastLoadable, useLastResolved, useLoadable, useResolved, useSet };
67
+ export { type Command, type Computed, type DebugStore, type Getter, type Read, type Setter, type State, type Store, type Subscribe, type Updater, type Write, command, computed, createDebugStore, createStore, state };
package/index.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- import * as react from 'react';
2
-
3
1
  type Updater<T> = (current: T) => T;
4
2
  interface Setter {
5
3
  <T>(state: State<T>, val: T | Updater<T>): void;
@@ -48,35 +46,11 @@ interface SubscribeOptions {
48
46
  }
49
47
  type CallbackFunc<T> = Command<T, []>;
50
48
  type Subscribe = (atoms$: ReadableAtom<unknown>[] | ReadableAtom<unknown>, callback: CallbackFunc<unknown>, options?: SubscribeOptions) => () => void;
51
- type InterceptorGet = <T>(atom$: State<T> | Computed<T>, fn: () => T) => void;
52
- interface InterceptorSet {
53
- <T, Args extends unknown[]>(func$: Command<T, Args>, fn: () => T, ...args: Args): void;
54
- <T>(value$: State<T>, fn: () => void, val: T | Updater<T>): void;
55
- }
56
- type InterceptorSub = <T>(atom$: ReadableAtom<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
57
- type InterceptorUnsub = <T>(atom$: ReadableAtom<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
58
- type InterceptorMount = <T>(atom$: ReadableAtom<T>) => void;
59
- type InterceptorUnmount = <T>(atom$: ReadableAtom<T>) => void;
60
- type InterceptorNotify = <T>(callback$: CallbackFunc<T>, fn: () => T) => void;
61
- type InterceptorComputed = <T>(atom$: Computed<T>, fn: () => T) => void;
62
- interface StoreInterceptor {
63
- get?: InterceptorGet;
64
- set?: InterceptorSet;
65
- sub?: InterceptorSub;
66
- unsub?: InterceptorUnsub;
67
- mount?: InterceptorMount;
68
- unmount?: InterceptorUnmount;
69
- notify?: InterceptorNotify;
70
- computed?: InterceptorComputed;
71
- }
49
+ type StoreEventType = 'set' | 'get' | 'sub' | 'unsub' | 'mount' | 'unmount' | 'notify' | 'computed';
72
50
 
73
51
  declare function createStore(): Store;
74
52
 
75
53
  type NestedAtom = (State<unknown> | Computed<unknown> | Command<unknown, unknown[]> | NestedAtom)[];
76
- type NestedString = (string | NestedString)[];
77
-
78
- declare function nestedAtomToString(atoms: NestedAtom): NestedString;
79
-
80
54
  interface DebugStore extends Store {
81
55
  getReadDependencies: (atom: Computed<unknown>) => NestedAtom;
82
56
  getReadDependents: (atom: State<unknown> | Computed<unknown>) => NestedAtom;
@@ -84,84 +58,10 @@ interface DebugStore extends Store {
84
58
  getSubscribeGraph: () => NestedAtom;
85
59
  }
86
60
 
87
- declare function createDebugStore(interceptor?: StoreInterceptor): DebugStore;
88
-
89
- type StoreEventType = 'set' | 'get' | 'sub' | 'unsub' | 'mount' | 'unmount' | 'notify' | 'computed';
90
-
91
61
  interface AtomWatch {
92
62
  target: State<unknown> | Computed<unknown> | Command<unknown, unknown[]> | string | RegExp;
93
63
  actions?: Set<StoreEventType>;
94
64
  }
95
- declare class ConsoleInterceptor implements StoreInterceptor {
96
- private readonly watches;
97
- constructor(watches: AtomWatch[]);
98
- private shouldLog;
99
- get: <T>(atom$: State<T> | Computed<T>, fn: () => T) => void;
100
- computed: <T>(atom$: Computed<T>, fn: () => T) => void;
101
- set: <T, Args extends unknown[]>(atom$: State<T> | Command<T, Args>, fn: () => T, ...args: Args | [T | Updater<T>]) => void;
102
- sub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
103
- unsub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
104
- mount: <T>(atom$: State<T> | Computed<T>) => void;
105
- unmount: <T>(atom$: State<T> | Computed<T>) => void;
106
- notify: <T>(callback$: CallbackFunc<T>, fn: () => T) => void;
107
- }
108
- declare function createConsoleDebugStore(watches: (AtomWatch | string | RegExp | State<unknown> | Computed<unknown> | Command<unknown, unknown[]>)[], defaultActions?: StoreEventType[]): DebugStore;
109
-
110
- declare class StoreEvent extends Event {
111
- readonly eventId: number;
112
- readonly targetAtom: string;
113
- readonly state: 'begin' | 'success' | 'error';
114
- readonly time: DOMHighResTimeStamp;
115
- readonly args: unknown[];
116
- readonly result: unknown;
117
- constructor(type: StoreEventType, eventId: number, targetAtom: string, state: 'begin' | 'success' | 'error', time: DOMHighResTimeStamp, args: unknown[], result: unknown);
118
- }
119
-
120
- declare class EventInterceptor implements StoreInterceptor {
121
- private traceId;
122
- private events;
123
- private createEvent;
124
- private wrapWithTrace;
125
- addEventListener(type: StoreEventType, listener: (event: StoreEvent) => void, options?: AddEventListenerOptions | boolean): void;
126
- removeEventListener(type: StoreEventType, listener: (event: StoreEvent) => void, options?: EventListenerOptions | boolean): void;
127
- get: <T>(atom$: State<T> | Computed<T>, fn: () => T) => T;
128
- computed: <T>(atom$: Computed<T>, fn: () => T) => T;
129
- set: <T, Args extends unknown[]>(atom$: State<T> | Command<T, Args>, fn: () => T, ...args: Args | [T | Updater<T>]) => T;
130
- sub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
131
- unsub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
132
- mount: <T>(atom$: State<T> | Computed<T>) => void;
133
- unmount: <T>(atom$: State<T> | Computed<T>) => void;
134
- notify: <T>(callback$: CallbackFunc<T>, fn: () => T) => void;
135
- }
136
-
137
- type PackedEventMessage = Pick<StoreEvent, 'type' | 'eventId' | 'targetAtom' | 'time' | 'state'>;
138
- interface DevToolsHookMessage {
139
- source: 'ccstate-store';
140
- payload: PackedEventMessage;
141
- }
142
- declare const GLOBAL_CCSTATE_INTERCEPED_KEY = "__CCSTATE_INTERCEPED__";
143
- declare function setupDevtoolsInterceptor(targetWindow: Window, signal?: AbortSignal): EventInterceptor;
144
-
145
- declare function useGet<T>(atom: State<T> | Computed<T>): T;
146
-
147
- declare function useSet<T>(atom: State<T>): (value: T | Updater<T>) => void;
148
- declare function useSet<T, ARGS extends unknown[]>(atom: Command<T, ARGS>): (...args: ARGS) => T;
149
-
150
- declare function useResolved<T>(atom: State<Promise<T>> | Computed<Promise<T>>): T | undefined;
151
- declare function useLastResolved<T>(atom: State<Promise<T>> | Computed<Promise<T>>): T | undefined;
152
-
153
- type Loadable<T> = {
154
- state: 'loading';
155
- } | {
156
- state: 'hasData';
157
- data: T;
158
- } | {
159
- state: 'hasError';
160
- error: unknown;
161
- };
162
- declare function useLoadable<T>(atom: State<Promise<T>> | Computed<Promise<T>>): Loadable<T>;
163
- declare function useLastLoadable<T>(atom: State<Promise<T>> | Computed<Promise<T>>): Loadable<T>;
164
-
165
- declare const StoreProvider: react.Provider<Store | null>;
65
+ declare function createDebugStore(watches?: (AtomWatch | string | RegExp | State<unknown> | Computed<unknown> | Command<unknown, unknown[]>)[], defaultActions?: StoreEventType[]): DebugStore;
166
66
 
167
- export { type Command, type Computed, ConsoleInterceptor, type DebugStore, type DevToolsHookMessage, EventInterceptor, GLOBAL_CCSTATE_INTERCEPED_KEY, type Getter, type PackedEventMessage, type Read, type Setter, type State, type Store, StoreEvent, type StoreEventType, type StoreInterceptor, StoreProvider, type Subscribe, type Updater, type Write, command, computed, createConsoleDebugStore, createDebugStore, createStore, nestedAtomToString, setupDevtoolsInterceptor, state, useGet, useLastLoadable, useLastResolved, useLoadable, useResolved, useSet };
67
+ export { type Command, type Computed, type DebugStore, type Getter, type Read, type Setter, type State, type Store, type Subscribe, type Updater, type Write, command, computed, createDebugStore, createStore, state };
package/index.js CHANGED
@@ -1,5 +1,3 @@
1
- import { createContext, useContext, useSyncExternalStore, useState, useEffect } from 'react';
2
-
3
1
  var globalId = 0;
4
2
  var generateToString = function generateToString(prefix, debugLabel) {
5
3
  var id = globalId++;
@@ -60,13 +58,6 @@ function _callSuper(t, o, e) {
60
58
  function _classCallCheck(a, n) {
61
59
  if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
62
60
  }
63
- function _construct(t, e, r) {
64
- if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments);
65
- var o = [null];
66
- o.push.apply(o, e);
67
- var p = new (t.bind.apply(t, o))();
68
- return r && _setPrototypeOf(p, r.prototype), p;
69
- }
70
61
  function _defineProperties(e, r) {
71
62
  for (var t = 0; t < r.length; t++) {
72
63
  var o = r[t];
@@ -160,13 +151,6 @@ function _inherits(t, e) {
160
151
  writable: !1
161
152
  }), e && _setPrototypeOf(t, e);
162
153
  }
163
- function _isNativeFunction(t) {
164
- try {
165
- return -1 !== Function.toString.call(t).indexOf("[native code]");
166
- } catch (n) {
167
- return "function" == typeof t;
168
- }
169
- }
170
154
  function _isNativeReflectConstruct() {
171
155
  try {
172
156
  var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
@@ -552,15 +536,6 @@ function _toPropertyKey(t) {
552
536
  var i = _toPrimitive(t, "string");
553
537
  return "symbol" == typeof i ? i : i + "";
554
538
  }
555
- function _typeof(o) {
556
- "@babel/helpers - typeof";
557
-
558
- return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
559
- return typeof o;
560
- } : function (o) {
561
- return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
562
- }, _typeof(o);
563
- }
564
539
  function _unsupportedIterableToArray(r, a) {
565
540
  if (r) {
566
541
  if ("string" == typeof r) return _arrayLikeToArray(r, a);
@@ -568,28 +543,6 @@ function _unsupportedIterableToArray(r, a) {
568
543
  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;
569
544
  }
570
545
  }
571
- function _wrapNativeSuper(t) {
572
- var r = "function" == typeof Map ? new Map() : void 0;
573
- return _wrapNativeSuper = function (t) {
574
- if (null === t || !_isNativeFunction(t)) return t;
575
- if ("function" != typeof t) throw new TypeError("Super expression must either be null or a function");
576
- if (void 0 !== r) {
577
- if (r.has(t)) return r.get(t);
578
- r.set(t, Wrapper);
579
- }
580
- function Wrapper() {
581
- return _construct(t, arguments, _getPrototypeOf(this).constructor);
582
- }
583
- return Wrapper.prototype = Object.create(t.prototype, {
584
- constructor: {
585
- value: Wrapper,
586
- enumerable: !1,
587
- writable: !0,
588
- configurable: !0
589
- }
590
- }), _setPrototypeOf(Wrapper, t);
591
- }, _wrapNativeSuper(t);
592
- }
593
546
 
594
547
  function canReadAsCompute(atom) {
595
548
  return 'read' in atom;
@@ -1122,16 +1075,6 @@ function createStore() {
1122
1075
  return new StoreImpl(atomManager, listenerManager);
1123
1076
  }
1124
1077
 
1125
- function nestedAtomToString(atoms) {
1126
- return atoms.map(function (atom) {
1127
- var _atom$debugLabel;
1128
- if (Array.isArray(atom)) {
1129
- return nestedAtomToString(atom);
1130
- }
1131
- return (_atom$debugLabel = atom.debugLabel) !== null && _atom$debugLabel !== void 0 ? _atom$debugLabel : 'anonymous';
1132
- });
1133
- }
1134
-
1135
1078
  var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1136
1079
  function DebugStoreImpl() {
1137
1080
  var _this;
@@ -1204,7 +1147,7 @@ var DebugStoreImpl = /*#__PURE__*/function (_StoreImpl) {
1204
1147
  _inherits(DebugStoreImpl, _StoreImpl);
1205
1148
  return _createClass(DebugStoreImpl);
1206
1149
  }(StoreImpl);
1207
- function createDebugStore(interceptor) {
1150
+ function createDebugStoreInternal(interceptor) {
1208
1151
  var atomManager = new AtomManager({
1209
1152
  interceptor: interceptor
1210
1153
  });
@@ -1305,7 +1248,9 @@ var ConsoleInterceptor = /*#__PURE__*/_createClass(function ConsoleInterceptor(w
1305
1248
  });
1306
1249
  this.watches = watches;
1307
1250
  });
1308
- function createConsoleDebugStore(watches, defaultActions) {
1251
+ function createDebugStore() {
1252
+ var watches = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
1253
+ var defaultActions = arguments.length > 1 ? arguments[1] : undefined;
1309
1254
  var parsedWatches = watches.map(function (watch) {
1310
1255
  if (typeof watch === 'string' || watch instanceof RegExp) {
1311
1256
  return {
@@ -1322,282 +1267,7 @@ function createConsoleDebugStore(watches, defaultActions) {
1322
1267
  };
1323
1268
  });
1324
1269
  var interceptor = new ConsoleInterceptor(parsedWatches);
1325
- return createDebugStore(interceptor);
1326
- }
1327
-
1328
- var StoreEvent = /*#__PURE__*/function (_Event) {
1329
- function StoreEvent(type, eventId, targetAtom, state, time, args, result) {
1330
- var _this;
1331
- _classCallCheck(this, StoreEvent);
1332
- _this = _callSuper(this, StoreEvent, [type]);
1333
- _this.eventId = eventId;
1334
- _this.targetAtom = targetAtom;
1335
- _this.state = state;
1336
- _this.time = time;
1337
- _this.args = args;
1338
- _this.result = result;
1339
- return _this;
1340
- }
1341
- _inherits(StoreEvent, _Event);
1342
- return _createClass(StoreEvent);
1343
- }(/*#__PURE__*/_wrapNativeSuper(Event));
1344
-
1345
- var EventInterceptor = /*#__PURE__*/function () {
1346
- function EventInterceptor() {
1347
- var _this = this;
1348
- _classCallCheck(this, EventInterceptor);
1349
- _defineProperty(this, "traceId", 0);
1350
- _defineProperty(this, "events", new EventTarget());
1351
- _defineProperty(this, "get", function (atom$, fn) {
1352
- return _this.wrapWithTrace(fn, function (eventId, time) {
1353
- _this.createEvent('get', eventId, atom$.toString(), time, 'begin', [], undefined);
1354
- }, function (eventId, time, result) {
1355
- _this.createEvent('get', eventId, atom$.toString(), time, 'success', [], result);
1356
- }, function (eventId, time, error) {
1357
- _this.createEvent('get', eventId, atom$.toString(), time, 'error', [], error);
1358
- });
1359
- });
1360
- _defineProperty(this, "computed", function (atom$, fn) {
1361
- return _this.wrapWithTrace(fn, function (eventId, time) {
1362
- _this.createEvent('computed', eventId, atom$.toString(), time, 'begin', [], undefined);
1363
- }, function (eventId, time, result) {
1364
- _this.createEvent('computed', eventId, atom$.toString(), time, 'success', [], result);
1365
- }, function (eventId, time, error) {
1366
- _this.createEvent('computed', eventId, atom$.toString(), time, 'error', [], error);
1367
- });
1368
- });
1369
- _defineProperty(this, "set", function (atom$, fn) {
1370
- for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
1371
- args[_key - 2] = arguments[_key];
1372
- }
1373
- return _this.wrapWithTrace(fn, function (eventId, time) {
1374
- _this.createEvent('set', eventId, atom$.toString(), time, 'begin', args, undefined);
1375
- }, function (eventId, time, result) {
1376
- _this.createEvent('set', eventId, atom$.toString(), time, 'success', args, result);
1377
- }, function (eventId, time, error) {
1378
- _this.createEvent('set', eventId, atom$.toString(), time, 'error', args, error);
1379
- });
1380
- });
1381
- _defineProperty(this, "sub", function (atom$, callback$, fn) {
1382
- var eventId = _this.traceId++;
1383
- _this.createEvent('sub', eventId, atom$.toString(), performance.now(), 'begin', [callback$.toString()], undefined);
1384
- fn();
1385
- _this.createEvent('sub', eventId, atom$.toString(), performance.now(), 'success', [callback$.toString()], undefined);
1386
- });
1387
- _defineProperty(this, "unsub", function (atom$, callback$, fn) {
1388
- var eventId = _this.traceId++;
1389
- _this.createEvent('unsub', eventId, atom$.toString(), performance.now(), 'begin', [callback$.toString()], undefined);
1390
- fn();
1391
- _this.createEvent('unsub', eventId, atom$.toString(), performance.now(), 'success', [callback$.toString()], undefined);
1392
- });
1393
- _defineProperty(this, "mount", function (atom$) {
1394
- var eventId = _this.traceId++;
1395
- _this.createEvent('mount', eventId, atom$.toString(), performance.now(), 'begin', [], undefined);
1396
- });
1397
- _defineProperty(this, "unmount", function (atom$) {
1398
- var eventId = _this.traceId++;
1399
- _this.createEvent('unmount', eventId, atom$.toString(), performance.now(), 'begin', [], undefined);
1400
- });
1401
- _defineProperty(this, "notify", function (callback$, fn) {
1402
- var eventId = _this.traceId++;
1403
- _this.createEvent('notify', eventId, callback$.toString(), performance.now(), 'begin', [], undefined);
1404
- var ret = fn();
1405
- _this.createEvent('notify', eventId, callback$.toString(), performance.now(), 'success', [], ret);
1406
- });
1407
- }
1408
- return _createClass(EventInterceptor, [{
1409
- key: "createEvent",
1410
- value: function createEvent(type, eventId, atom, time, state, args, result) {
1411
- var event = new StoreEvent(type, eventId, atom, state, time, args, result);
1412
- this.events.dispatchEvent(event);
1413
- return event;
1414
- }
1415
- }, {
1416
- key: "wrapWithTrace",
1417
- value: function wrapWithTrace(fn, createBeginEvent, createSuccessEvent, createErrorEvent) {
1418
- var eventId = this.traceId++;
1419
- createBeginEvent(eventId, performance.now());
1420
- try {
1421
- var result = fn();
1422
- createSuccessEvent(eventId, performance.now(), result);
1423
- return result;
1424
- } catch (e) {
1425
- createErrorEvent(eventId, performance.now(), e);
1426
- throw e;
1427
- }
1428
- }
1429
- }, {
1430
- key: "addEventListener",
1431
- value: function addEventListener(type, listener, options) {
1432
- this.events.addEventListener(type, listener, options);
1433
- }
1434
- }, {
1435
- key: "removeEventListener",
1436
- value: function removeEventListener(type, listener, options) {
1437
- this.events.removeEventListener(type, listener, options);
1438
- }
1439
- }]);
1440
- }();
1441
-
1442
- var GLOBAL_CCSTATE_INTERCEPED_KEY = '__CCSTATE_INTERCEPED__';
1443
- function setupDevtoolsInterceptor(targetWindow, signal) {
1444
- var interceptor = new EventInterceptor();
1445
- var watchedAtoms = new Set();
1446
- targetWindow.addEventListener('message', function (_ref) {
1447
- var data = _ref.data;
1448
- if (!data || _typeof(data) !== 'object' || !('source' in data) || data.source !== 'ccstate-devtools') {
1449
- return;
1450
- }
1451
- var payload = data.payload;
1452
- watchedAtoms.add(payload.args[0]);
1453
- }, {
1454
- signal: signal
1455
- });
1456
- function handleStoreEvent(event) {
1457
- var debugLabel = event.targetAtom.substring(event.targetAtom.indexOf(':') + 1);
1458
- if (watchedAtoms.has(debugLabel)) {
1459
- console.group("[CCState] ".concat(event.type, " ").concat(event.targetAtom, " ").concat(event.state));
1460
- console.log('args', event.args);
1461
- console.log('result', event.result);
1462
- console.groupEnd();
1463
- }
1464
- var message = {
1465
- source: 'ccstate-store',
1466
- payload: {
1467
- type: event.type,
1468
- eventId: event.eventId,
1469
- targetAtom: event.targetAtom,
1470
- time: event.time,
1471
- state: event.state
1472
- }
1473
- };
1474
- targetWindow.postMessage(message);
1475
- }
1476
- interceptor.addEventListener('get', handleStoreEvent);
1477
- interceptor.addEventListener('computed', handleStoreEvent);
1478
- interceptor.addEventListener('set', handleStoreEvent);
1479
- interceptor.addEventListener('sub', handleStoreEvent);
1480
- interceptor.addEventListener('unsub', handleStoreEvent);
1481
- interceptor.addEventListener('mount', handleStoreEvent);
1482
- interceptor.addEventListener('unmount', handleStoreEvent);
1483
- interceptor.addEventListener('notify', handleStoreEvent);
1484
- targetWindow[GLOBAL_CCSTATE_INTERCEPED_KEY] = true;
1485
- console.warn('[CCSTATE] Interceptor injected, DO NOT USE THIS IN PRODUCTION');
1486
- return interceptor;
1487
- }
1488
-
1489
- var StoreContext = createContext(null);
1490
- var StoreProvider = StoreContext.Provider;
1491
- function useStore() {
1492
- var store = useContext(StoreContext);
1493
- if (!store) {
1494
- throw new Error('Store context not found - did you forget to wrap your app with StoreProvider?');
1495
- }
1496
- return store;
1497
- }
1498
-
1499
- function useGet(atom) {
1500
- var store = useStore();
1501
- return useSyncExternalStore(function (fn) {
1502
- var ctrl = new AbortController();
1503
- store.sub(atom, command(fn), {
1504
- signal: ctrl.signal
1505
- });
1506
- return function () {
1507
- ctrl.abort();
1508
- };
1509
- }, function () {
1510
- return store.get(atom);
1511
- });
1512
- }
1513
-
1514
- function useSet(atom) {
1515
- var store = useStore();
1516
- if ('write' in atom) {
1517
- return function () {
1518
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
1519
- args[_key] = arguments[_key];
1520
- }
1521
- var ret = store.set.apply(store, [atom].concat(args));
1522
- return ret;
1523
- };
1524
- }
1525
- return function (value) {
1526
- store.set(atom, value);
1527
- };
1528
- }
1529
-
1530
- function useLoadable(atom) {
1531
- var promise = useGet(atom);
1532
- var _useState = useState({
1533
- state: 'loading'
1534
- }),
1535
- _useState2 = _slicedToArray(_useState, 2),
1536
- promiseResult = _useState2[0],
1537
- setPromiseResult = _useState2[1];
1538
- useEffect(function () {
1539
- var ctrl = new AbortController();
1540
- var signal = ctrl.signal;
1541
- setPromiseResult({
1542
- state: 'loading'
1543
- });
1544
- void promise.then(function (ret) {
1545
- if (signal.aborted) return;
1546
- setPromiseResult({
1547
- state: 'hasData',
1548
- data: ret
1549
- });
1550
- })["catch"](function (error) {
1551
- if (signal.aborted) return;
1552
- setPromiseResult({
1553
- state: 'hasError',
1554
- error: error
1555
- });
1556
- });
1557
- return function () {
1558
- ctrl.abort();
1559
- };
1560
- }, [promise]);
1561
- return promiseResult;
1562
- }
1563
- function useLastLoadable(atom) {
1564
- var promise = useGet(atom);
1565
- var _useState3 = useState({
1566
- state: 'loading'
1567
- }),
1568
- _useState4 = _slicedToArray(_useState3, 2),
1569
- promiseResult = _useState4[0],
1570
- setPromiseResult = _useState4[1];
1571
- useEffect(function () {
1572
- var ctrl = new AbortController();
1573
- var signal = ctrl.signal;
1574
- void promise.then(function (ret) {
1575
- if (signal.aborted) return;
1576
- setPromiseResult({
1577
- state: 'hasData',
1578
- data: ret
1579
- });
1580
- })["catch"](function (error) {
1581
- if (signal.aborted) return;
1582
- setPromiseResult({
1583
- state: 'hasError',
1584
- error: error
1585
- });
1586
- });
1587
- return function () {
1588
- ctrl.abort();
1589
- };
1590
- }, [promise]);
1591
- return promiseResult;
1592
- }
1593
-
1594
- function useResolved(atom) {
1595
- var loadable = useLoadable(atom);
1596
- return loadable.state === 'hasData' ? loadable.data : undefined;
1597
- }
1598
- function useLastResolved(atom) {
1599
- var loadable = useLastLoadable(atom);
1600
- return loadable.state === 'hasData' ? loadable.data : undefined;
1270
+ return createDebugStoreInternal(interceptor);
1601
1271
  }
1602
1272
 
1603
- export { ConsoleInterceptor, EventInterceptor, GLOBAL_CCSTATE_INTERCEPED_KEY, StoreEvent, StoreProvider, command, computed, createConsoleDebugStore, createDebugStore, createStore, nestedAtomToString, setupDevtoolsInterceptor, state, useGet, useLastLoadable, useLastResolved, useLoadable, useResolved, useSet };
1273
+ export { command, computed, createDebugStore, createStore, state };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ccstate",
3
- "version": "2.1.0",
3
+ "version": "3.0.0",
4
4
  "description": "CCState Core",
5
5
  "private": false,
6
6
  "repository": {
@@ -20,17 +20,5 @@
20
20
  "import": "./*/index.js",
21
21
  "require": "./*/index.cjs"
22
22
  }
23
- },
24
- "peerDependencies": {
25
- "@types/react": ">=17.0.0",
26
- "react": ">=17.0.0"
27
- },
28
- "peerDependenciesMeta": {
29
- "@types/react": {
30
- "optional": true
31
- },
32
- "react": {
33
- "optional": true
34
- }
35
23
  }
36
24
  }