ccstate 2.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.
@@ -0,0 +1,133 @@
1
+ type Updater<T> = (current: T) => T;
2
+ interface Setter {
3
+ <T>(state: State<T>, val: T | Updater<T>): void;
4
+ <T, Args extends unknown[]>(command: Command<T, Args>, ...args: Args): T;
5
+ }
6
+ type Getter = <T>(readable: ReadableAtom<T>) => T;
7
+ interface GetterOptions {
8
+ signal: AbortSignal;
9
+ }
10
+ type Read<T> = (get: Getter, options: GetterOptions) => T;
11
+ type Write<T, Args extends unknown[]> = (visitor: {
12
+ get: Getter;
13
+ set: Setter;
14
+ }, ...args: Args) => T;
15
+ interface State<T> {
16
+ init: T;
17
+ debugLabel?: string;
18
+ toString: () => string;
19
+ }
20
+ interface Computed<T> {
21
+ read: Read<T>;
22
+ debugLabel?: string;
23
+ toString: () => string;
24
+ }
25
+ interface Command<T, Args extends unknown[]> {
26
+ write: Write<T, Args>;
27
+ debugLabel?: string;
28
+ toString: () => string;
29
+ }
30
+ type ReadableAtom<T> = State<T> | Computed<T>;
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
+ interface Store {
38
+ get: Getter;
39
+ set: Setter;
40
+ sub: Subscribe;
41
+ }
42
+ interface SubscribeOptions {
43
+ signal?: AbortSignal;
44
+ }
45
+ type CallbackFunc<T> = Command<T, []>;
46
+ 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
+ }
68
+
69
+ interface DebugStore extends Store {
70
+ getReadDependencies: (atom: Computed<unknown>) => NestedAtom;
71
+ getReadDependents: (atom: State<unknown> | Computed<unknown>) => NestedAtom;
72
+ isMounted: (atom: State<unknown> | Computed<unknown>) => boolean;
73
+ getSubscribeGraph: () => NestedAtom;
74
+ }
75
+
76
+ declare function createDebugStore(interceptor?: StoreInterceptor): DebugStore;
77
+
78
+ type StoreEventType = 'set' | 'get' | 'sub' | 'unsub' | 'mount' | 'unmount' | 'notify' | 'computed';
79
+
80
+ interface AtomWatch {
81
+ target: State<unknown> | Computed<unknown> | Command<unknown, unknown[]> | string | RegExp;
82
+ actions?: Set<StoreEventType>;
83
+ }
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
+
98
+ declare class StoreEvent extends Event {
99
+ readonly eventId: number;
100
+ readonly targetAtom: string;
101
+ readonly state: 'begin' | 'success' | 'error';
102
+ readonly time: DOMHighResTimeStamp;
103
+ readonly args: unknown[];
104
+ readonly result: unknown;
105
+ constructor(type: StoreEventType, eventId: number, targetAtom: string, state: 'begin' | 'success' | 'error', time: DOMHighResTimeStamp, args: unknown[], result: unknown);
106
+ }
107
+
108
+ declare class EventInterceptor implements StoreInterceptor {
109
+ private traceId;
110
+ private events;
111
+ private createEvent;
112
+ private wrapWithTrace;
113
+ addEventListener(type: StoreEventType, listener: (event: StoreEvent) => void, options?: AddEventListenerOptions | boolean): void;
114
+ removeEventListener(type: StoreEventType, listener: (event: StoreEvent) => void, options?: EventListenerOptions | boolean): void;
115
+ get: <T>(atom$: State<T> | Computed<T>, fn: () => T) => T;
116
+ computed: <T>(atom$: Computed<T>, fn: () => T) => T;
117
+ set: <T, Args extends unknown[]>(atom$: State<T> | Command<T, Args>, fn: () => T, ...args: Args | [T | Updater<T>]) => T;
118
+ sub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
119
+ unsub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
120
+ mount: <T>(atom$: State<T> | Computed<T>) => void;
121
+ unmount: <T>(atom$: State<T> | Computed<T>) => void;
122
+ notify: <T>(callback$: CallbackFunc<T>, fn: () => T) => void;
123
+ }
124
+
125
+ type PackedEventMessage = Pick<StoreEvent, 'type' | 'eventId' | 'targetAtom' | 'time' | 'state'>;
126
+ interface DevToolsHookMessage {
127
+ source: 'ccstate-store';
128
+ payload: PackedEventMessage;
129
+ }
130
+ declare const GLOBAL_CCSTATE_INTERCEPED_KEY = "__CCSTATE_INTERCEPED__";
131
+ declare function setupDevtoolsInterceptor(targetWindow: Window, signal?: AbortSignal): EventInterceptor;
132
+
133
+ export { ConsoleInterceptor, type DebugStore, type DevToolsHookMessage, EventInterceptor, GLOBAL_CCSTATE_INTERCEPED_KEY, type PackedEventMessage, StoreEvent, type StoreEventType, type StoreInterceptor, createDebugStore, nestedAtomToString, setupDevtoolsInterceptor };
@@ -0,0 +1,133 @@
1
+ type Updater<T> = (current: T) => T;
2
+ interface Setter {
3
+ <T>(state: State<T>, val: T | Updater<T>): void;
4
+ <T, Args extends unknown[]>(command: Command<T, Args>, ...args: Args): T;
5
+ }
6
+ type Getter = <T>(readable: ReadableAtom<T>) => T;
7
+ interface GetterOptions {
8
+ signal: AbortSignal;
9
+ }
10
+ type Read<T> = (get: Getter, options: GetterOptions) => T;
11
+ type Write<T, Args extends unknown[]> = (visitor: {
12
+ get: Getter;
13
+ set: Setter;
14
+ }, ...args: Args) => T;
15
+ interface State<T> {
16
+ init: T;
17
+ debugLabel?: string;
18
+ toString: () => string;
19
+ }
20
+ interface Computed<T> {
21
+ read: Read<T>;
22
+ debugLabel?: string;
23
+ toString: () => string;
24
+ }
25
+ interface Command<T, Args extends unknown[]> {
26
+ write: Write<T, Args>;
27
+ debugLabel?: string;
28
+ toString: () => string;
29
+ }
30
+ type ReadableAtom<T> = State<T> | Computed<T>;
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
+ interface Store {
38
+ get: Getter;
39
+ set: Setter;
40
+ sub: Subscribe;
41
+ }
42
+ interface SubscribeOptions {
43
+ signal?: AbortSignal;
44
+ }
45
+ type CallbackFunc<T> = Command<T, []>;
46
+ 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
+ }
68
+
69
+ interface DebugStore extends Store {
70
+ getReadDependencies: (atom: Computed<unknown>) => NestedAtom;
71
+ getReadDependents: (atom: State<unknown> | Computed<unknown>) => NestedAtom;
72
+ isMounted: (atom: State<unknown> | Computed<unknown>) => boolean;
73
+ getSubscribeGraph: () => NestedAtom;
74
+ }
75
+
76
+ declare function createDebugStore(interceptor?: StoreInterceptor): DebugStore;
77
+
78
+ type StoreEventType = 'set' | 'get' | 'sub' | 'unsub' | 'mount' | 'unmount' | 'notify' | 'computed';
79
+
80
+ interface AtomWatch {
81
+ target: State<unknown> | Computed<unknown> | Command<unknown, unknown[]> | string | RegExp;
82
+ actions?: Set<StoreEventType>;
83
+ }
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
+
98
+ declare class StoreEvent extends Event {
99
+ readonly eventId: number;
100
+ readonly targetAtom: string;
101
+ readonly state: 'begin' | 'success' | 'error';
102
+ readonly time: DOMHighResTimeStamp;
103
+ readonly args: unknown[];
104
+ readonly result: unknown;
105
+ constructor(type: StoreEventType, eventId: number, targetAtom: string, state: 'begin' | 'success' | 'error', time: DOMHighResTimeStamp, args: unknown[], result: unknown);
106
+ }
107
+
108
+ declare class EventInterceptor implements StoreInterceptor {
109
+ private traceId;
110
+ private events;
111
+ private createEvent;
112
+ private wrapWithTrace;
113
+ addEventListener(type: StoreEventType, listener: (event: StoreEvent) => void, options?: AddEventListenerOptions | boolean): void;
114
+ removeEventListener(type: StoreEventType, listener: (event: StoreEvent) => void, options?: EventListenerOptions | boolean): void;
115
+ get: <T>(atom$: State<T> | Computed<T>, fn: () => T) => T;
116
+ computed: <T>(atom$: Computed<T>, fn: () => T) => T;
117
+ set: <T, Args extends unknown[]>(atom$: State<T> | Command<T, Args>, fn: () => T, ...args: Args | [T | Updater<T>]) => T;
118
+ sub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
119
+ unsub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
120
+ mount: <T>(atom$: State<T> | Computed<T>) => void;
121
+ unmount: <T>(atom$: State<T> | Computed<T>) => void;
122
+ notify: <T>(callback$: CallbackFunc<T>, fn: () => T) => void;
123
+ }
124
+
125
+ type PackedEventMessage = Pick<StoreEvent, 'type' | 'eventId' | 'targetAtom' | 'time' | 'state'>;
126
+ interface DevToolsHookMessage {
127
+ source: 'ccstate-store';
128
+ payload: PackedEventMessage;
129
+ }
130
+ declare const GLOBAL_CCSTATE_INTERCEPED_KEY = "__CCSTATE_INTERCEPED__";
131
+ declare function setupDevtoolsInterceptor(targetWindow: Window, signal?: AbortSignal): EventInterceptor;
132
+
133
+ export { ConsoleInterceptor, type DebugStore, type DevToolsHookMessage, EventInterceptor, GLOBAL_CCSTATE_INTERCEPED_KEY, type PackedEventMessage, StoreEvent, type StoreEventType, type StoreInterceptor, createDebugStore, nestedAtomToString, setupDevtoolsInterceptor };