fractostate 4.2.1 → 4.3.2
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/dist/cjs/index.js +61 -4
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/src/devtools/FractoDevTools.css.js +1 -1
- package/dist/cjs/src/devtools/FractoDevTools.js +51 -33
- package/dist/cjs/src/devtools/FractoDevTools.js.map +1 -1
- package/dist/cjs/src/devtools/MapUtils.js +0 -1
- package/dist/cjs/src/devtools/MapUtils.js.map +1 -1
- package/dist/cjs/src/devtools/MentalMap.js +17 -17
- package/dist/cjs/src/devtools/MentalMap.js.map +1 -1
- package/dist/cjs/src/devtools/PluginManager.css.js +10 -0
- package/dist/cjs/src/devtools/PluginManager.css.js.map +1 -0
- package/dist/cjs/src/devtools/PluginManager.js +46 -0
- package/dist/cjs/src/devtools/PluginManager.js.map +1 -0
- package/dist/cjs/src/plugins/storage/StorageAdapter.js +73 -1
- package/dist/cjs/src/plugins/storage/StorageAdapter.js.map +1 -1
- package/dist/cjs/src/proxy.js +4 -0
- package/dist/cjs/src/proxy.js.map +1 -1
- package/dist/cjs/src/store.js +60 -0
- package/dist/cjs/src/store.js.map +1 -1
- package/dist/esm/index.js +57 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/src/devtools/FractoDevTools.css.js +1 -1
- package/dist/esm/src/devtools/FractoDevTools.js +23 -5
- package/dist/esm/src/devtools/FractoDevTools.js.map +1 -1
- package/dist/esm/src/devtools/MapUtils.js +0 -1
- package/dist/esm/src/devtools/MapUtils.js.map +1 -1
- package/dist/esm/src/devtools/PluginManager.css.js +8 -0
- package/dist/esm/src/devtools/PluginManager.css.js.map +1 -0
- package/dist/esm/src/devtools/PluginManager.js +44 -0
- package/dist/esm/src/devtools/PluginManager.js.map +1 -0
- package/dist/esm/src/plugins/storage/StorageAdapter.js +73 -1
- package/dist/esm/src/plugins/storage/StorageAdapter.js.map +1 -1
- package/dist/esm/src/proxy.js +4 -0
- package/dist/esm/src/proxy.js.map +1 -1
- package/dist/esm/src/store.js +60 -0
- package/dist/esm/src/store.js.map +1 -1
- package/dist/index.d.ts +8 -3
- package/dist/plugins.d.ts +1 -1
- package/dist/types-BGbbVpKy.d.ts +176 -0
- package/dist/types-B_uh22Cx.d.ts +181 -0
- package/dist/types-C9aLynPD.d.ts +176 -0
- package/dist/types-dU51pXGw.d.ts +187 -0
- package/package.json +1 -1
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core types for FractoState
|
|
3
|
+
*/
|
|
4
|
+
type TypeAwareOps<T, BA = any> = [T] extends [null | undefined] ? BaseOps<T, BA> : [T] extends [any[]] ? ArrayOps<T[number], BA> : [T] extends [number] ? NumberOps<BA> : [T] extends [string] ? StringOps<BA> : [T] extends [boolean] ? BooleanOps<BA> : [NonNullable<T>] extends [object] ? ObjectOps<T, BA> : BaseOps<T, BA>;
|
|
5
|
+
type FlowOpsObject<T, BA = any> = {
|
|
6
|
+
readonly self: TypeAwareOps<T, BA>;
|
|
7
|
+
readonly state: T;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Action structure: (...args) => (ops) => Result
|
|
11
|
+
*/
|
|
12
|
+
type FlowAction<T, BA = any> = (...args: any[]) => (ops: FlowOpsObject<T, BA>) => any;
|
|
13
|
+
/**
|
|
14
|
+
* Type-safe action definition.
|
|
15
|
+
* Each action is a curried function: (...args) => (ops) => Result
|
|
16
|
+
*/
|
|
17
|
+
type ActionDef<T, A> = (...args: never[]) => (ops: FlowOpsObject<T, BoundActions<A>>) => unknown;
|
|
18
|
+
/**
|
|
19
|
+
* Transform actions for the component toolbox.
|
|
20
|
+
*/
|
|
21
|
+
type BoundActions<A> = {
|
|
22
|
+
[K in keyof A]: A[K] extends (...args: infer P) => (...inner: never[]) => infer R ? (...args: P) => R : A[K] extends (...args: infer P) => (ops: FlowOpsObject<infer _T, infer _BA>) => infer R ? (...args: P) => R : A[K];
|
|
23
|
+
};
|
|
24
|
+
interface FlowPlugin<T = any> {
|
|
25
|
+
name: string;
|
|
26
|
+
onInit?: (ctx: {
|
|
27
|
+
key: string;
|
|
28
|
+
initial: T;
|
|
29
|
+
store: any;
|
|
30
|
+
}) => void;
|
|
31
|
+
onUpdate?: (ctx: {
|
|
32
|
+
key: string;
|
|
33
|
+
prev: T;
|
|
34
|
+
next: T;
|
|
35
|
+
store: any;
|
|
36
|
+
}) => void;
|
|
37
|
+
onHydrate?: (ctx: {
|
|
38
|
+
key: string;
|
|
39
|
+
store: any;
|
|
40
|
+
}) => T | Promise<T> | undefined;
|
|
41
|
+
}
|
|
42
|
+
interface FlowEffect<T, BA = any> {
|
|
43
|
+
/** Function to run on init and when deps change */
|
|
44
|
+
run: (ops: FlowOpsObject<T, BA>) => void | Promise<void>;
|
|
45
|
+
/** Optional dependencies selector. If omitted, runs only on init */
|
|
46
|
+
deps?: (state: T) => unknown[];
|
|
47
|
+
}
|
|
48
|
+
interface FlowOptions<T = any, A = any> {
|
|
49
|
+
timeTravel?: boolean;
|
|
50
|
+
maxHistory?: number;
|
|
51
|
+
debounce?: number;
|
|
52
|
+
middleware?: Array<(state: T) => T>;
|
|
53
|
+
plugins?: FlowPlugin<T>[];
|
|
54
|
+
effects?: FlowEffect<T, BoundActions<A>>[];
|
|
55
|
+
force?: boolean;
|
|
56
|
+
skipEquality?: boolean;
|
|
57
|
+
isUndoRedo?: boolean;
|
|
58
|
+
bypassClone?: boolean;
|
|
59
|
+
/** The name of the component or module performing the update */
|
|
60
|
+
actor?: string;
|
|
61
|
+
/** Raw actions dictionary for internal binding */
|
|
62
|
+
actions?: A;
|
|
63
|
+
}
|
|
64
|
+
interface UseFlowOptions<T> extends FlowOptions<T> {
|
|
65
|
+
name?: string;
|
|
66
|
+
}
|
|
67
|
+
interface FlowDefinition<T, A = {}> {
|
|
68
|
+
key: string;
|
|
69
|
+
initial: T;
|
|
70
|
+
options?: FlowOptions<T> & {
|
|
71
|
+
actions?: A;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
interface DerivedFlowDefinition<T, R> {
|
|
75
|
+
key: string;
|
|
76
|
+
source: FlowDefinition<T, any>;
|
|
77
|
+
selector: (state: T) => R;
|
|
78
|
+
}
|
|
79
|
+
interface FlowOperations<T, A = {}> {
|
|
80
|
+
/** The operation object for recursive state manipulation via proxies */
|
|
81
|
+
ops: FlowOpsObject<T, BoundActions<A>>;
|
|
82
|
+
/** The actions defined in defineFlow, bound to this specific state instance */
|
|
83
|
+
actions: BoundActions<A>;
|
|
84
|
+
/** Reverts to the previous state in history (if timeTravel is enabled) */
|
|
85
|
+
_undo: () => void;
|
|
86
|
+
/** Reapplies a previously undone state (if timeTravel is enabled) */
|
|
87
|
+
_redo: () => void;
|
|
88
|
+
/** Returns the full history stack for this flow */
|
|
89
|
+
history: T[];
|
|
90
|
+
/** Boolean indicating if an undo operation is currently possible */
|
|
91
|
+
_canUndo: boolean;
|
|
92
|
+
/** Boolean indicating if a redo operation is currently possible */
|
|
93
|
+
_canRedo: boolean;
|
|
94
|
+
/** Forcefully updates the state and records history even if values are identical. Useful for triggering effects or manual snapshots. */
|
|
95
|
+
_set: (val: T | ((prev: T) => T)) => void;
|
|
96
|
+
/** Optimized update: only commits the change if the new value differs from the current one. Saves history space and render cycles. */
|
|
97
|
+
_patch: (val: T | ((prev: T) => T)) => void;
|
|
98
|
+
/** Resets the state back to its initial defined configuration */
|
|
99
|
+
_reset: () => void;
|
|
100
|
+
/** Returns the current flow configuration options */
|
|
101
|
+
cf: FlowOptions<T>;
|
|
102
|
+
/** Boolean indicating if the flow is currently loading data from a hydration plugin (e.g., localStorage) */
|
|
103
|
+
isHydrating: boolean;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Common operations available on every state node.
|
|
107
|
+
* Prefixed with underscores to avoid collisions with your nested data properties.
|
|
108
|
+
*/
|
|
109
|
+
type BaseOps<T, BA = any> = {
|
|
110
|
+
/** Forcefully replace the value at this path, ignoring equality checks. Records history. */
|
|
111
|
+
_set: (value: T) => void;
|
|
112
|
+
/** Smart update: only commits the change if the value is different. */
|
|
113
|
+
_patch: (value: T) => void;
|
|
114
|
+
/** Unwraps the proxy and returns the raw value at this specific tree path. */
|
|
115
|
+
_val: T;
|
|
116
|
+
/** Access all actions defined in this flow */
|
|
117
|
+
__actions__: BA;
|
|
118
|
+
};
|
|
119
|
+
/** Operations specific to numeric values */
|
|
120
|
+
type NumberOps<BA = any> = BaseOps<number, BA> & {
|
|
121
|
+
/** Increment by n (default 1) */
|
|
122
|
+
_increment: (n?: number) => void;
|
|
123
|
+
/** Decrement by n (default 1) */
|
|
124
|
+
_decrement: (n?: number) => void;
|
|
125
|
+
/** Add n to the value */
|
|
126
|
+
_add: (n: number) => void;
|
|
127
|
+
/** Subtract n from the value */
|
|
128
|
+
_subtract: (n: number) => void;
|
|
129
|
+
/** Multiply the value by n */
|
|
130
|
+
_multiply: (n: number) => void;
|
|
131
|
+
/** Divide the value by n */
|
|
132
|
+
_divide: (n: number) => void;
|
|
133
|
+
};
|
|
134
|
+
/** Operations specific to string values */
|
|
135
|
+
type StringOps<BA = any> = BaseOps<string, BA> & {
|
|
136
|
+
/** Append a string at the end */
|
|
137
|
+
_append: (s: string) => void;
|
|
138
|
+
/** Prepend a string at the beginning */
|
|
139
|
+
_prepend: (s: string) => void;
|
|
140
|
+
/** Convert value to uppercase */
|
|
141
|
+
_uppercase: () => void;
|
|
142
|
+
/** Convert value to lowercase */
|
|
143
|
+
_lowercase: () => void;
|
|
144
|
+
/** Trim whitespace from both ends */
|
|
145
|
+
_trim: () => void;
|
|
146
|
+
/** Replace occurrences matching pattern */
|
|
147
|
+
_replace: (pattern: string | RegExp, replacement: string) => void;
|
|
148
|
+
};
|
|
149
|
+
/** Operations specific to array values */
|
|
150
|
+
type ArrayOps<T, BA = any> = BaseOps<T[], BA> & {
|
|
151
|
+
/** Add an item to the end of the array */
|
|
152
|
+
_push: (v: T) => void;
|
|
153
|
+
/** Remove and return the last item */
|
|
154
|
+
_pop: () => void;
|
|
155
|
+
/** Remove and return the first item */
|
|
156
|
+
_shift: () => void;
|
|
157
|
+
/** Add an item to the beginning of the array */
|
|
158
|
+
_unshift: (v: T) => void;
|
|
159
|
+
/** Keep only items matching the condition */
|
|
160
|
+
_filter: (fn: (v: T, i: number) => boolean) => void;
|
|
161
|
+
/** Transform every item in the array */
|
|
162
|
+
_map: (fn: (v: T, i: number) => T) => void;
|
|
163
|
+
/** Remove item at a specific index */
|
|
164
|
+
_removeAt: (i: number) => void;
|
|
165
|
+
/** Insert an item at a specific index */
|
|
166
|
+
_insertAt: (i: number, v: T) => void;
|
|
167
|
+
};
|
|
168
|
+
/** Operations specific to object values */
|
|
169
|
+
type ObjectOps<T, BA = any> = BaseOps<T, BA> & {
|
|
170
|
+
/** Shallow merge the provided object into the current state */
|
|
171
|
+
_merge: (p: Partial<NonNullable<T>>) => void;
|
|
172
|
+
/** Remove a specific key from the object */
|
|
173
|
+
_delete: (k: keyof NonNullable<T>) => void;
|
|
174
|
+
} & {
|
|
175
|
+
[K in keyof NonNullable<T>]-?: TypeAwareOps<NonNullable<T>[K], BA>;
|
|
176
|
+
};
|
|
177
|
+
type BooleanOps<BA = any> = BaseOps<boolean, BA> & {
|
|
178
|
+
_toggle: () => void;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
export type { ActionDef as A, BoundActions as B, DerivedFlowDefinition as D, FlowDefinition as F, NumberOps as N, ObjectOps as O, StringOps as S, TypeAwareOps as T, UseFlowOptions as U, FlowOptions as a, FlowOpsObject as b, FlowOperations as c, ArrayOps as d, BaseOps as e, BooleanOps as f, FlowAction as g, FlowEffect as h, FlowPlugin as i };
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core types for FractoState
|
|
3
|
+
*/
|
|
4
|
+
type TypeAwareOps<T, A = any> = [T] extends [null | undefined] ? BaseOps<T, A> : [T] extends [any[]] ? ArrayOps<T[number], A> : [T] extends [number] ? NumberOps<A> : [T] extends [string] ? StringOps<A> : [T] extends [boolean] ? BooleanOps<A> : [NonNullable<T>] extends [object] ? ObjectOps<T, A> : BaseOps<T, A>;
|
|
5
|
+
type FlowOpsObject<T, A = any> = {
|
|
6
|
+
readonly self: TypeAwareOps<T, A>;
|
|
7
|
+
readonly state: T;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Action structure: (...args) => (ops) => Result
|
|
11
|
+
*/
|
|
12
|
+
type FlowAction<T, A = any> = (...args: any[]) => (ops: FlowOpsObject<T, A>) => any;
|
|
13
|
+
/**
|
|
14
|
+
* Transform actions for the component toolbox.
|
|
15
|
+
*/
|
|
16
|
+
type BoundActions<A> = {
|
|
17
|
+
[K in keyof A]: A[K] extends (...args: infer P) => (...inner: any[]) => infer R ? (...args: P) => R : never;
|
|
18
|
+
};
|
|
19
|
+
interface FlowPlugin<T = any> {
|
|
20
|
+
name: string;
|
|
21
|
+
onInit?: (ctx: {
|
|
22
|
+
key: string;
|
|
23
|
+
initial: T;
|
|
24
|
+
store: any;
|
|
25
|
+
}) => void;
|
|
26
|
+
onUpdate?: (ctx: {
|
|
27
|
+
key: string;
|
|
28
|
+
prev: T;
|
|
29
|
+
next: T;
|
|
30
|
+
store: any;
|
|
31
|
+
}) => void;
|
|
32
|
+
onHydrate?: (ctx: {
|
|
33
|
+
key: string;
|
|
34
|
+
store: any;
|
|
35
|
+
}) => T | Promise<T> | undefined;
|
|
36
|
+
}
|
|
37
|
+
interface FlowEffect<T, A = any> {
|
|
38
|
+
/** Function to run on init and when deps change */
|
|
39
|
+
run: (ops: FlowOpsObject<T, A>) => void | Promise<void>;
|
|
40
|
+
/** Optional dependencies selector. If omitted, runs only on init */
|
|
41
|
+
deps?: (state: T) => any[];
|
|
42
|
+
}
|
|
43
|
+
interface FlowOptions<T = any, A = any> {
|
|
44
|
+
timeTravel?: boolean;
|
|
45
|
+
maxHistory?: number;
|
|
46
|
+
debounce?: number;
|
|
47
|
+
middleware?: Array<(state: T) => T>;
|
|
48
|
+
plugins?: FlowPlugin<T>[];
|
|
49
|
+
effects?: FlowEffect<T, A>[];
|
|
50
|
+
force?: boolean;
|
|
51
|
+
skipEquality?: boolean;
|
|
52
|
+
isUndoRedo?: boolean;
|
|
53
|
+
bypassClone?: boolean;
|
|
54
|
+
/** The name of the component or module performing the update */
|
|
55
|
+
actor?: string;
|
|
56
|
+
/** Raw actions dictionary for internal binding */
|
|
57
|
+
actions?: A;
|
|
58
|
+
}
|
|
59
|
+
interface UseFlowOptions<T> extends FlowOptions<T> {
|
|
60
|
+
name?: string;
|
|
61
|
+
}
|
|
62
|
+
interface FlowDefinition<T, A = {}> {
|
|
63
|
+
key: string;
|
|
64
|
+
initial: T;
|
|
65
|
+
options?: FlowOptions<T> & {
|
|
66
|
+
actions?: A;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
interface DerivedFlowDefinition<T, R> {
|
|
70
|
+
key: string;
|
|
71
|
+
source: FlowDefinition<T, any>;
|
|
72
|
+
selector: (state: T) => R;
|
|
73
|
+
}
|
|
74
|
+
interface FlowOperations<T, A = {}> {
|
|
75
|
+
/** The operation object for recursive state manipulation via proxies */
|
|
76
|
+
ops: FlowOpsObject<T>;
|
|
77
|
+
/** The actions defined in defineFlow, bound to this specific state instance */
|
|
78
|
+
actions: BoundActions<A>;
|
|
79
|
+
/** Reverts to the previous state in history (if timeTravel is enabled) */
|
|
80
|
+
_undo: () => void;
|
|
81
|
+
/** Reapplies a previously undone state (if timeTravel is enabled) */
|
|
82
|
+
_redo: () => void;
|
|
83
|
+
/** Returns the full history stack for this flow */
|
|
84
|
+
history: T[];
|
|
85
|
+
/** Boolean indicating if an undo operation is currently possible */
|
|
86
|
+
_canUndo: boolean;
|
|
87
|
+
/** Boolean indicating if a redo operation is currently possible */
|
|
88
|
+
_canRedo: boolean;
|
|
89
|
+
/** Forcefully updates the state and records history even if values are identical. Useful for triggering effects or manual snapshots. */
|
|
90
|
+
_set: (val: T | ((prev: T) => T)) => void;
|
|
91
|
+
/** Optimized update: only commits the change if the new value differs from the current one. Saves history space and render cycles. */
|
|
92
|
+
_patch: (val: T | ((prev: T) => T)) => void;
|
|
93
|
+
/** Resets the state back to its initial defined configuration */
|
|
94
|
+
_reset: () => void;
|
|
95
|
+
/** Returns the current flow configuration options */
|
|
96
|
+
cf: FlowOptions<T>;
|
|
97
|
+
/** Boolean indicating if the flow is currently loading data from a hydration plugin (e.g., localStorage) */
|
|
98
|
+
isHydrating: boolean;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Common operations available on every state node.
|
|
102
|
+
* Prefixed with underscores to avoid collisions with your nested data properties.
|
|
103
|
+
*/
|
|
104
|
+
type BaseOps<T, A = any> = {
|
|
105
|
+
/** Forcefully replace the value at this path, ignoring equality checks. Records history. */
|
|
106
|
+
_set: (value: T) => void;
|
|
107
|
+
/** Smart update: only commits the change if the value is different. */
|
|
108
|
+
_patch: (value: T) => void;
|
|
109
|
+
/** Unwraps the proxy and returns the raw value at this specific tree path. */
|
|
110
|
+
_val: T;
|
|
111
|
+
/** Access all actions defined in this flow */
|
|
112
|
+
__actions__: BoundActions<A>;
|
|
113
|
+
};
|
|
114
|
+
/** Operations specific to numeric values */
|
|
115
|
+
type NumberOps<A = any> = BaseOps<number, A> & {
|
|
116
|
+
/** Increment by n (default 1) */
|
|
117
|
+
_increment: (n?: number) => void;
|
|
118
|
+
/** Decrement by n (default 1) */
|
|
119
|
+
_decrement: (n?: number) => void;
|
|
120
|
+
/** Add n to the value */
|
|
121
|
+
_add: (n: number) => void;
|
|
122
|
+
/** Subtract n from the value */
|
|
123
|
+
_subtract: (n: number) => void;
|
|
124
|
+
/** Multiply the value by n */
|
|
125
|
+
_multiply: (n: number) => void;
|
|
126
|
+
/** Divide the value by n */
|
|
127
|
+
_divide: (n: number) => void;
|
|
128
|
+
};
|
|
129
|
+
/** Operations specific to string values */
|
|
130
|
+
type StringOps<A = any> = BaseOps<string, A> & {
|
|
131
|
+
/** Append a string at the end */
|
|
132
|
+
_append: (s: string) => void;
|
|
133
|
+
/** Prepend a string at the beginning */
|
|
134
|
+
_prepend: (s: string) => void;
|
|
135
|
+
/** Convert value to uppercase */
|
|
136
|
+
_uppercase: () => void;
|
|
137
|
+
/** Convert value to lowercase */
|
|
138
|
+
_lowercase: () => void;
|
|
139
|
+
/** Trim whitespace from both ends */
|
|
140
|
+
_trim: () => void;
|
|
141
|
+
/** Replace occurrences matching pattern */
|
|
142
|
+
_replace: (pattern: string | RegExp, replacement: string) => void;
|
|
143
|
+
};
|
|
144
|
+
/** Operations specific to array values */
|
|
145
|
+
type ArrayOps<T, A = any> = BaseOps<T[], A> & {
|
|
146
|
+
/** Add an item to the end of the array */
|
|
147
|
+
_push: (v: T) => void;
|
|
148
|
+
/** Remove and return the last item */
|
|
149
|
+
_pop: () => void;
|
|
150
|
+
/** Remove and return the first item */
|
|
151
|
+
_shift: () => void;
|
|
152
|
+
/** Add an item to the beginning of the array */
|
|
153
|
+
_unshift: (v: T) => void;
|
|
154
|
+
/** Keep only items matching the condition */
|
|
155
|
+
_filter: (fn: (v: T, i: number) => boolean) => void;
|
|
156
|
+
/** Transform every item in the array */
|
|
157
|
+
_map: (fn: (v: T, i: number) => T) => void;
|
|
158
|
+
/** Remove item at a specific index */
|
|
159
|
+
_removeAt: (i: number) => void;
|
|
160
|
+
/** Insert an item at a specific index */
|
|
161
|
+
_insertAt: (i: number, v: T) => void;
|
|
162
|
+
};
|
|
163
|
+
/** Operations specific to object values */
|
|
164
|
+
type ObjectOps<T, A = any> = BaseOps<T, A> & {
|
|
165
|
+
/** Shallow merge the provided object into the current state */
|
|
166
|
+
_merge: (p: Partial<NonNullable<T>>) => void;
|
|
167
|
+
/** Remove a specific key from the object */
|
|
168
|
+
_delete: (k: keyof NonNullable<T>) => void;
|
|
169
|
+
} & {
|
|
170
|
+
[K in keyof NonNullable<T>]-?: TypeAwareOps<NonNullable<T>[K], A>;
|
|
171
|
+
};
|
|
172
|
+
type BooleanOps<A = any> = BaseOps<boolean, A> & {
|
|
173
|
+
_toggle: () => void;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
export type { ArrayOps as A, BaseOps as B, DerivedFlowDefinition as D, FlowDefinition as F, NumberOps as N, ObjectOps as O, StringOps as S, TypeAwareOps as T, UseFlowOptions as U, FlowOptions as a, FlowOpsObject as b, FlowOperations as c, BooleanOps as d, BoundActions as e, FlowAction as f, FlowEffect as g, FlowPlugin as h };
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core types for FractoState
|
|
3
|
+
*/
|
|
4
|
+
type TypeAwareOps<T, A = any> = [T] extends [null | undefined] ? BaseOps<T, A> : [T] extends [any[]] ? ArrayOps<T[number], A> : [T] extends [number] ? NumberOps<A> : [T] extends [string] ? StringOps<A> : [T] extends [boolean] ? BooleanOps<A> : [NonNullable<T>] extends [object] ? ObjectOps<T, A> : BaseOps<T, A>;
|
|
5
|
+
type FlowOpsObject<T, A = any> = {
|
|
6
|
+
readonly self: TypeAwareOps<T, A>;
|
|
7
|
+
readonly state: T;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Action structure: (...args) => (ops) => Result
|
|
11
|
+
*/
|
|
12
|
+
type FlowAction<T, A = any> = (...args: any[]) => (ops: FlowOpsObject<T, A>) => any;
|
|
13
|
+
/**
|
|
14
|
+
* Transform actions for the component toolbox.
|
|
15
|
+
*/
|
|
16
|
+
type BoundActions<A> = {
|
|
17
|
+
[K in keyof A]: A[K] extends (...args: infer P) => (...inner: any[]) => infer R ? (...args: P) => R : never;
|
|
18
|
+
};
|
|
19
|
+
interface FlowPlugin<T = any> {
|
|
20
|
+
name: string;
|
|
21
|
+
onInit?: (ctx: {
|
|
22
|
+
key: string;
|
|
23
|
+
initial: T;
|
|
24
|
+
store: any;
|
|
25
|
+
}) => void;
|
|
26
|
+
onUpdate?: (ctx: {
|
|
27
|
+
key: string;
|
|
28
|
+
prev: T;
|
|
29
|
+
next: T;
|
|
30
|
+
store: any;
|
|
31
|
+
}) => void;
|
|
32
|
+
onHydrate?: (ctx: {
|
|
33
|
+
key: string;
|
|
34
|
+
store: any;
|
|
35
|
+
}) => T | Promise<T> | undefined;
|
|
36
|
+
/** Return methods that will be exposed under ops.self._[pluginName] */
|
|
37
|
+
getOps?: (ctx: {
|
|
38
|
+
key: string;
|
|
39
|
+
store: any;
|
|
40
|
+
}) => Record<string, Function>;
|
|
41
|
+
}
|
|
42
|
+
interface FlowEffect<T, A = any> {
|
|
43
|
+
/** Function to run on init and when deps change */
|
|
44
|
+
run: (ops: FlowOpsObject<T, A>) => void | Promise<void>;
|
|
45
|
+
/** Optional dependencies selector. If omitted, runs only on init */
|
|
46
|
+
deps?: (state: T) => any[];
|
|
47
|
+
}
|
|
48
|
+
interface FlowOptions<T = any, A = any> {
|
|
49
|
+
timeTravel?: boolean;
|
|
50
|
+
maxHistory?: number;
|
|
51
|
+
debounce?: number;
|
|
52
|
+
middleware?: Array<(state: T) => T>;
|
|
53
|
+
plugins?: FlowPlugin<T>[];
|
|
54
|
+
effects?: FlowEffect<T, A>[];
|
|
55
|
+
force?: boolean;
|
|
56
|
+
skipEquality?: boolean;
|
|
57
|
+
isUndoRedo?: boolean;
|
|
58
|
+
bypassClone?: boolean;
|
|
59
|
+
/** The name of the component or module performing the update */
|
|
60
|
+
actor?: string;
|
|
61
|
+
/** Raw actions dictionary for internal binding */
|
|
62
|
+
actions?: A;
|
|
63
|
+
}
|
|
64
|
+
interface UseFlowOptions<T> extends FlowOptions<T> {
|
|
65
|
+
name?: string;
|
|
66
|
+
}
|
|
67
|
+
interface FlowDefinition<T, A = {}> {
|
|
68
|
+
key: string;
|
|
69
|
+
initial: T;
|
|
70
|
+
options?: FlowOptions<T> & {
|
|
71
|
+
actions?: A;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
interface DerivedFlowDefinition<T, R> {
|
|
75
|
+
key: string;
|
|
76
|
+
source: FlowDefinition<T, any>;
|
|
77
|
+
selector: (state: T) => R;
|
|
78
|
+
}
|
|
79
|
+
interface FlowOperations<T, A = {}> {
|
|
80
|
+
/** The operation object for recursive state manipulation via proxies */
|
|
81
|
+
ops: FlowOpsObject<T, A>;
|
|
82
|
+
/** The actions defined in defineFlow, bound to this specific state instance */
|
|
83
|
+
actions: BoundActions<A>;
|
|
84
|
+
/** Reverts to the previous state in history (if timeTravel is enabled) */
|
|
85
|
+
_undo: () => void;
|
|
86
|
+
/** Reapplies a previously undone state (if timeTravel is enabled) */
|
|
87
|
+
_redo: () => void;
|
|
88
|
+
/** Returns the full history stack for this flow */
|
|
89
|
+
history: T[];
|
|
90
|
+
/** Boolean indicating if an undo operation is currently possible */
|
|
91
|
+
_canUndo: boolean;
|
|
92
|
+
/** Boolean indicating if a redo operation is currently possible */
|
|
93
|
+
_canRedo: boolean;
|
|
94
|
+
/** Forcefully updates the state and records history even if values are identical. Useful for triggering effects or manual snapshots. */
|
|
95
|
+
_set: (val: T | ((prev: T) => T)) => void;
|
|
96
|
+
/** Optimized update: only commits the change if the new value differs from the current one. Saves history space and render cycles. */
|
|
97
|
+
_patch: (val: T | ((prev: T) => T)) => void;
|
|
98
|
+
/** Resets the state back to its initial defined configuration */
|
|
99
|
+
_reset: () => void;
|
|
100
|
+
/** Returns the current flow configuration options */
|
|
101
|
+
cf: FlowOptions<T>;
|
|
102
|
+
/** Boolean indicating if the flow is currently loading data from a hydration plugin (e.g., localStorage) */
|
|
103
|
+
isHydrating: boolean;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Common operations available on every state node.
|
|
107
|
+
* Prefixed with underscores to avoid collisions with your nested data properties.
|
|
108
|
+
*/
|
|
109
|
+
type BaseOps<T, A = any> = {
|
|
110
|
+
/** Forcefully replace the value at this path, ignoring equality checks. Records history. */
|
|
111
|
+
_set: (value: T) => void;
|
|
112
|
+
/** Smart update: only commits the change if the value is different. */
|
|
113
|
+
_patch: (value: T) => void;
|
|
114
|
+
/** Unwraps the proxy and returns the raw value at this specific tree path. */
|
|
115
|
+
_val: T;
|
|
116
|
+
/** Access all actions defined in this flow */
|
|
117
|
+
__actions__: BoundActions<A>;
|
|
118
|
+
/** Persistence control (provided by persist plugin) */
|
|
119
|
+
_persist?: {
|
|
120
|
+
clear: () => void;
|
|
121
|
+
update: () => void;
|
|
122
|
+
refresh: () => Promise<void>;
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
/** Operations specific to numeric values */
|
|
126
|
+
type NumberOps<A = any> = BaseOps<number, A> & {
|
|
127
|
+
/** Increment by n (default 1) */
|
|
128
|
+
_increment: (n?: number) => void;
|
|
129
|
+
/** Decrement by n (default 1) */
|
|
130
|
+
_decrement: (n?: number) => void;
|
|
131
|
+
/** Add n to the value */
|
|
132
|
+
_add: (n: number) => void;
|
|
133
|
+
/** Subtract n from the value */
|
|
134
|
+
_subtract: (n: number) => void;
|
|
135
|
+
/** Multiply the value by n */
|
|
136
|
+
_multiply: (n: number) => void;
|
|
137
|
+
/** Divide the value by n */
|
|
138
|
+
_divide: (n: number) => void;
|
|
139
|
+
};
|
|
140
|
+
/** Operations specific to string values */
|
|
141
|
+
type StringOps<A = any> = BaseOps<string, A> & {
|
|
142
|
+
/** Append a string at the end */
|
|
143
|
+
_append: (s: string) => void;
|
|
144
|
+
/** Prepend a string at the beginning */
|
|
145
|
+
_prepend: (s: string) => void;
|
|
146
|
+
/** Convert value to uppercase */
|
|
147
|
+
_uppercase: () => void;
|
|
148
|
+
/** Convert value to lowercase */
|
|
149
|
+
_lowercase: () => void;
|
|
150
|
+
/** Trim whitespace from both ends */
|
|
151
|
+
_trim: () => void;
|
|
152
|
+
/** Replace occurrences matching pattern */
|
|
153
|
+
_replace: (pattern: string | RegExp, replacement: string) => void;
|
|
154
|
+
};
|
|
155
|
+
/** Operations specific to array values */
|
|
156
|
+
type ArrayOps<T, A = any> = BaseOps<T[], A> & {
|
|
157
|
+
/** Add an item to the end of the array */
|
|
158
|
+
_push: (v: T) => void;
|
|
159
|
+
/** Remove and return the last item */
|
|
160
|
+
_pop: () => void;
|
|
161
|
+
/** Remove and return the first item */
|
|
162
|
+
_shift: () => void;
|
|
163
|
+
/** Add an item to the beginning of the array */
|
|
164
|
+
_unshift: (v: T) => void;
|
|
165
|
+
/** Keep only items matching the condition */
|
|
166
|
+
_filter: (fn: (v: T, i: number) => boolean) => void;
|
|
167
|
+
/** Transform every item in the array */
|
|
168
|
+
_map: (fn: (v: T, i: number) => T) => void;
|
|
169
|
+
/** Remove item at a specific index */
|
|
170
|
+
_removeAt: (i: number) => void;
|
|
171
|
+
/** Insert an item at a specific index */
|
|
172
|
+
_insertAt: (i: number, v: T) => void;
|
|
173
|
+
};
|
|
174
|
+
/** Operations specific to object values */
|
|
175
|
+
type ObjectOps<T, A = any> = BaseOps<T, A> & {
|
|
176
|
+
/** Shallow merge the provided object into the current state */
|
|
177
|
+
_merge: (p: Partial<NonNullable<T>>) => void;
|
|
178
|
+
/** Remove a specific key from the object */
|
|
179
|
+
_delete: (k: keyof NonNullable<T>) => void;
|
|
180
|
+
} & {
|
|
181
|
+
[K in keyof NonNullable<T>]-?: TypeAwareOps<NonNullable<T>[K], A>;
|
|
182
|
+
};
|
|
183
|
+
type BooleanOps<A = any> = BaseOps<boolean, A> & {
|
|
184
|
+
_toggle: () => void;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
export type { ArrayOps as A, BaseOps as B, DerivedFlowDefinition as D, FlowDefinition as F, NumberOps as N, ObjectOps as O, StringOps as S, TypeAwareOps as T, UseFlowOptions as U, FlowOpsObject as a, FlowOptions as b, FlowOperations as c, BooleanOps as d, BoundActions as e, FlowAction as f, FlowEffect as g, FlowPlugin as h };
|
package/package.json
CHANGED