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.
package/index.d.cts ADDED
@@ -0,0 +1,166 @@
1
+ import * as react from 'react';
2
+
3
+ type Updater<T> = (current: T) => T;
4
+ interface Setter {
5
+ <T>(state: State<T>, val: T | Updater<T>): void;
6
+ <T, Args extends unknown[]>(command: Command<T, Args>, ...args: Args): T;
7
+ }
8
+ type Getter = <T>(readable: ReadableAtom<T>) => T;
9
+ interface GetterOptions {
10
+ signal: AbortSignal;
11
+ }
12
+ type Read<T> = (get: Getter, options: GetterOptions) => T;
13
+ type Write<T, Args extends unknown[]> = (visitor: {
14
+ get: Getter;
15
+ set: Setter;
16
+ }, ...args: Args) => T;
17
+ interface State<T> {
18
+ init: T;
19
+ debugLabel?: string;
20
+ toString: () => string;
21
+ }
22
+ interface Computed<T> {
23
+ read: Read<T>;
24
+ debugLabel?: string;
25
+ toString: () => string;
26
+ }
27
+ interface Command<T, Args extends unknown[]> {
28
+ write: Write<T, Args>;
29
+ debugLabel?: string;
30
+ toString: () => string;
31
+ }
32
+ type ReadableAtom<T> = State<T> | Computed<T>;
33
+
34
+ interface Options {
35
+ debugLabel?: string;
36
+ }
37
+ declare function state<T>(init: T, options?: Options): State<T>;
38
+ declare function computed<T>(read: Read<T>, options?: Options): Computed<T>;
39
+ declare function command<T, Args extends unknown[]>(write: Write<T, Args>, options?: Options): Command<T, Args>;
40
+
41
+ interface Store {
42
+ get: Getter;
43
+ set: Setter;
44
+ sub: Subscribe;
45
+ }
46
+ interface SubscribeOptions {
47
+ signal?: AbortSignal;
48
+ }
49
+ type CallbackFunc<T> = Command<T, []>;
50
+ 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
+ }
72
+
73
+ declare function createStore(): Store;
74
+
75
+ 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
+ interface DebugStore extends Store {
81
+ getReadDependencies: (atom: Computed<unknown>) => NestedAtom;
82
+ getReadDependents: (atom: State<unknown> | Computed<unknown>) => NestedAtom;
83
+ isMounted: (atom: State<unknown> | Computed<unknown>) => boolean;
84
+ getSubscribeGraph: () => NestedAtom;
85
+ }
86
+
87
+ declare function createDebugStore(interceptor?: StoreInterceptor): DebugStore;
88
+
89
+ type StoreEventType = 'set' | 'get' | 'sub' | 'unsub' | 'mount' | 'unmount' | 'notify' | 'computed';
90
+
91
+ interface AtomWatch {
92
+ target: State<unknown> | Computed<unknown> | Command<unknown, unknown[]> | string | RegExp;
93
+ actions?: Set<StoreEventType>;
94
+ }
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
+
109
+ declare class StoreEvent extends Event {
110
+ readonly eventId: number;
111
+ readonly targetAtom: string;
112
+ readonly state: 'begin' | 'success' | 'error';
113
+ readonly time: DOMHighResTimeStamp;
114
+ readonly args: unknown[];
115
+ readonly result: unknown;
116
+ constructor(type: StoreEventType, eventId: number, targetAtom: string, state: 'begin' | 'success' | 'error', time: DOMHighResTimeStamp, args: unknown[], result: unknown);
117
+ }
118
+
119
+ declare class EventInterceptor implements StoreInterceptor {
120
+ private traceId;
121
+ private events;
122
+ private createEvent;
123
+ private wrapWithTrace;
124
+ addEventListener(type: StoreEventType, listener: (event: StoreEvent) => void, options?: AddEventListenerOptions | boolean): void;
125
+ removeEventListener(type: StoreEventType, listener: (event: StoreEvent) => void, options?: EventListenerOptions | boolean): void;
126
+ get: <T>(atom$: State<T> | Computed<T>, fn: () => T) => T;
127
+ computed: <T>(atom$: Computed<T>, fn: () => T) => T;
128
+ set: <T, Args extends unknown[]>(atom$: State<T> | Command<T, Args>, fn: () => T, ...args: Args | [T | Updater<T>]) => T;
129
+ sub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
130
+ unsub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
131
+ mount: <T>(atom$: State<T> | Computed<T>) => void;
132
+ unmount: <T>(atom$: State<T> | Computed<T>) => void;
133
+ notify: <T>(callback$: CallbackFunc<T>, fn: () => T) => void;
134
+ }
135
+
136
+ type PackedEventMessage = Pick<StoreEvent, 'type' | 'eventId' | 'targetAtom' | 'time' | 'state'>;
137
+ interface DevToolsHookMessage {
138
+ source: 'ccstate-store';
139
+ payload: PackedEventMessage;
140
+ }
141
+ declare const GLOBAL_CCSTATE_INTERCEPED_KEY = "__CCSTATE_INTERCEPED__";
142
+ declare function setupDevtoolsInterceptor(targetWindow: Window, signal?: AbortSignal): EventInterceptor;
143
+
144
+ declare function useGet<T>(atom: State<T> | Computed<T>): T;
145
+
146
+ declare function useSet<T>(atom: State<T>): (value: T | Updater<T>) => void;
147
+ declare function useSet<T, ARGS extends unknown[]>(atom: Command<T, ARGS>): (...args: ARGS) => T;
148
+
149
+ declare function useResolved<T>(atom: State<Promise<T>> | Computed<Promise<T>>): T | undefined;
150
+ declare function useLastResolved<T>(atom: State<Promise<T>> | Computed<Promise<T>>): T | undefined;
151
+
152
+ type Loadable<T> = {
153
+ state: 'loading';
154
+ } | {
155
+ state: 'hasData';
156
+ data: T;
157
+ } | {
158
+ state: 'hasError';
159
+ error: unknown;
160
+ };
161
+ declare function useLoadable<T>(atom: State<Promise<T>> | Computed<Promise<T>>): Loadable<T>;
162
+ declare function useLastLoadable<T>(atom: State<Promise<T>> | Computed<Promise<T>>): Loadable<T>;
163
+
164
+ declare const StoreProvider: react.Provider<Store | null>;
165
+
166
+ 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, createDebugStore, createStore, nestedAtomToString, setupDevtoolsInterceptor, state, useGet, useLastLoadable, useLastResolved, useLoadable, useResolved, useSet };
package/index.d.ts ADDED
@@ -0,0 +1,166 @@
1
+ import * as react from 'react';
2
+
3
+ type Updater<T> = (current: T) => T;
4
+ interface Setter {
5
+ <T>(state: State<T>, val: T | Updater<T>): void;
6
+ <T, Args extends unknown[]>(command: Command<T, Args>, ...args: Args): T;
7
+ }
8
+ type Getter = <T>(readable: ReadableAtom<T>) => T;
9
+ interface GetterOptions {
10
+ signal: AbortSignal;
11
+ }
12
+ type Read<T> = (get: Getter, options: GetterOptions) => T;
13
+ type Write<T, Args extends unknown[]> = (visitor: {
14
+ get: Getter;
15
+ set: Setter;
16
+ }, ...args: Args) => T;
17
+ interface State<T> {
18
+ init: T;
19
+ debugLabel?: string;
20
+ toString: () => string;
21
+ }
22
+ interface Computed<T> {
23
+ read: Read<T>;
24
+ debugLabel?: string;
25
+ toString: () => string;
26
+ }
27
+ interface Command<T, Args extends unknown[]> {
28
+ write: Write<T, Args>;
29
+ debugLabel?: string;
30
+ toString: () => string;
31
+ }
32
+ type ReadableAtom<T> = State<T> | Computed<T>;
33
+
34
+ interface Options {
35
+ debugLabel?: string;
36
+ }
37
+ declare function state<T>(init: T, options?: Options): State<T>;
38
+ declare function computed<T>(read: Read<T>, options?: Options): Computed<T>;
39
+ declare function command<T, Args extends unknown[]>(write: Write<T, Args>, options?: Options): Command<T, Args>;
40
+
41
+ interface Store {
42
+ get: Getter;
43
+ set: Setter;
44
+ sub: Subscribe;
45
+ }
46
+ interface SubscribeOptions {
47
+ signal?: AbortSignal;
48
+ }
49
+ type CallbackFunc<T> = Command<T, []>;
50
+ 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
+ }
72
+
73
+ declare function createStore(): Store;
74
+
75
+ 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
+ interface DebugStore extends Store {
81
+ getReadDependencies: (atom: Computed<unknown>) => NestedAtom;
82
+ getReadDependents: (atom: State<unknown> | Computed<unknown>) => NestedAtom;
83
+ isMounted: (atom: State<unknown> | Computed<unknown>) => boolean;
84
+ getSubscribeGraph: () => NestedAtom;
85
+ }
86
+
87
+ declare function createDebugStore(interceptor?: StoreInterceptor): DebugStore;
88
+
89
+ type StoreEventType = 'set' | 'get' | 'sub' | 'unsub' | 'mount' | 'unmount' | 'notify' | 'computed';
90
+
91
+ interface AtomWatch {
92
+ target: State<unknown> | Computed<unknown> | Command<unknown, unknown[]> | string | RegExp;
93
+ actions?: Set<StoreEventType>;
94
+ }
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
+
109
+ declare class StoreEvent extends Event {
110
+ readonly eventId: number;
111
+ readonly targetAtom: string;
112
+ readonly state: 'begin' | 'success' | 'error';
113
+ readonly time: DOMHighResTimeStamp;
114
+ readonly args: unknown[];
115
+ readonly result: unknown;
116
+ constructor(type: StoreEventType, eventId: number, targetAtom: string, state: 'begin' | 'success' | 'error', time: DOMHighResTimeStamp, args: unknown[], result: unknown);
117
+ }
118
+
119
+ declare class EventInterceptor implements StoreInterceptor {
120
+ private traceId;
121
+ private events;
122
+ private createEvent;
123
+ private wrapWithTrace;
124
+ addEventListener(type: StoreEventType, listener: (event: StoreEvent) => void, options?: AddEventListenerOptions | boolean): void;
125
+ removeEventListener(type: StoreEventType, listener: (event: StoreEvent) => void, options?: EventListenerOptions | boolean): void;
126
+ get: <T>(atom$: State<T> | Computed<T>, fn: () => T) => T;
127
+ computed: <T>(atom$: Computed<T>, fn: () => T) => T;
128
+ set: <T, Args extends unknown[]>(atom$: State<T> | Command<T, Args>, fn: () => T, ...args: Args | [T | Updater<T>]) => T;
129
+ sub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
130
+ unsub: <T>(atom$: State<T> | Computed<T>, callback$: CallbackFunc<T>, fn: () => void) => void;
131
+ mount: <T>(atom$: State<T> | Computed<T>) => void;
132
+ unmount: <T>(atom$: State<T> | Computed<T>) => void;
133
+ notify: <T>(callback$: CallbackFunc<T>, fn: () => T) => void;
134
+ }
135
+
136
+ type PackedEventMessage = Pick<StoreEvent, 'type' | 'eventId' | 'targetAtom' | 'time' | 'state'>;
137
+ interface DevToolsHookMessage {
138
+ source: 'ccstate-store';
139
+ payload: PackedEventMessage;
140
+ }
141
+ declare const GLOBAL_CCSTATE_INTERCEPED_KEY = "__CCSTATE_INTERCEPED__";
142
+ declare function setupDevtoolsInterceptor(targetWindow: Window, signal?: AbortSignal): EventInterceptor;
143
+
144
+ declare function useGet<T>(atom: State<T> | Computed<T>): T;
145
+
146
+ declare function useSet<T>(atom: State<T>): (value: T | Updater<T>) => void;
147
+ declare function useSet<T, ARGS extends unknown[]>(atom: Command<T, ARGS>): (...args: ARGS) => T;
148
+
149
+ declare function useResolved<T>(atom: State<Promise<T>> | Computed<Promise<T>>): T | undefined;
150
+ declare function useLastResolved<T>(atom: State<Promise<T>> | Computed<Promise<T>>): T | undefined;
151
+
152
+ type Loadable<T> = {
153
+ state: 'loading';
154
+ } | {
155
+ state: 'hasData';
156
+ data: T;
157
+ } | {
158
+ state: 'hasError';
159
+ error: unknown;
160
+ };
161
+ declare function useLoadable<T>(atom: State<Promise<T>> | Computed<Promise<T>>): Loadable<T>;
162
+ declare function useLastLoadable<T>(atom: State<Promise<T>> | Computed<Promise<T>>): Loadable<T>;
163
+
164
+ declare const StoreProvider: react.Provider<Store | null>;
165
+
166
+ 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, createDebugStore, createStore, nestedAtomToString, setupDevtoolsInterceptor, state, useGet, useLastLoadable, useLastResolved, useLoadable, useResolved, useSet };