@voidhash/mimic-react 0.0.1-alpha.10 → 0.0.1-alpha.111
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/.turbo/turbo-build.log +13 -13
- package/dist/zustand-commander/index.cjs +11 -21
- package/dist/zustand-commander/index.d.cts +7 -19
- package/dist/zustand-commander/index.d.cts.map +1 -1
- package/dist/zustand-commander/index.d.mts +7 -19
- package/dist/zustand-commander/index.d.mts.map +1 -1
- package/dist/zustand-commander/index.mjs +11 -21
- package/dist/zustand-commander/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/zustand-commander/commander.ts +10 -82
- package/src/zustand-commander/index.ts +5 -8
- package/src/zustand-commander/types.ts +14 -36
- package/tests/zustand-commander/commander.test.ts +15 -28
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @voidhash/mimic-react@0.0.1-alpha.
|
|
2
|
+
> @voidhash/mimic-react@0.0.1-alpha.111 build /home/runner/work/mimic/mimic/packages/mimic-react
|
|
3
3
|
> tsdown
|
|
4
4
|
|
|
5
5
|
[34mℹ[39m tsdown [2mv0.18.2[22m powered by rolldown [2mv1.0.0-beta.55[22m
|
|
@@ -8,28 +8,28 @@
|
|
|
8
8
|
[34mℹ[39m target: [34mes2017[39m
|
|
9
9
|
[34mℹ[39m tsconfig: [34mtsconfig.json[39m
|
|
10
10
|
[34mℹ[39m Build start
|
|
11
|
-
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[1mzustand-commander/index.cjs[22m [2m10.
|
|
11
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[1mzustand-commander/index.cjs[22m [2m10.02 kB[22m [2m│ gzip: 2.67 kB[22m
|
|
12
12
|
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[1mzustand/index.cjs[22m [2m 3.02 kB[22m [2m│ gzip: 1.09 kB[22m
|
|
13
13
|
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[1mindex.cjs[22m [2m 0.00 kB[22m [2m│ gzip: 0.02 kB[22m
|
|
14
14
|
[34mℹ[39m [33m[CJS][39m [2mdist/[22mobjectSpread2-CIP_6jda.cjs [2m 2.23 kB[22m [2m│ gzip: 0.80 kB[22m
|
|
15
|
-
[34mℹ[39m [33m[CJS][39m 4 files, total: 15.
|
|
16
|
-
[34mℹ[39m [33m[CJS][39m [2mdist/[22mzustand-commander/index.d.cts.map [2m 5.
|
|
15
|
+
[34mℹ[39m [33m[CJS][39m 4 files, total: 15.27 kB
|
|
16
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22mzustand-commander/index.d.cts.map [2m 5.29 kB[22m [2m│ gzip: 2.15 kB[22m
|
|
17
17
|
[34mℹ[39m [33m[CJS][39m [2mdist/[22mzustand/index.d.cts.map [2m 1.86 kB[22m [2m│ gzip: 0.82 kB[22m
|
|
18
|
-
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32m[1mzustand-commander/index.d.cts[22m[39m [
|
|
18
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32m[1mzustand-commander/index.d.cts[22m[39m [2m10.95 kB[22m [2m│ gzip: 3.00 kB[22m
|
|
19
19
|
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32m[1mzustand/index.d.cts[22m[39m [2m 5.01 kB[22m [2m│ gzip: 1.46 kB[22m
|
|
20
20
|
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32m[1mindex.d.cts[22m[39m [2m 0.01 kB[22m [2m│ gzip: 0.03 kB[22m
|
|
21
|
-
[34mℹ[39m [33m[CJS][39m 5 files, total:
|
|
22
|
-
[32m✔[39m Build complete in [
|
|
23
|
-
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[1mzustand-commander/index.mjs[22m [2m 9.
|
|
21
|
+
[34mℹ[39m [33m[CJS][39m 5 files, total: 23.13 kB
|
|
22
|
+
[32m✔[39m Build complete in [32m4109ms[39m
|
|
23
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[1mzustand-commander/index.mjs[22m [2m 9.40 kB[22m [2m│ gzip: 2.63 kB[22m
|
|
24
24
|
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[1mzustand/index.mjs[22m [2m 2.96 kB[22m [2m│ gzip: 1.11 kB[22m
|
|
25
25
|
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[1mindex.mjs[22m [2m 0.01 kB[22m [2m│ gzip: 0.03 kB[22m
|
|
26
|
-
[34mℹ[39m [34m[ESM][39m [2mdist/[22mzustand-commander/index.mjs.map [
|
|
26
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22mzustand-commander/index.mjs.map [2m30.61 kB[22m [2m│ gzip: 7.03 kB[22m
|
|
27
27
|
[34mℹ[39m [34m[ESM][39m [2mdist/[22mzustand/index.mjs.map [2m 6.71 kB[22m [2m│ gzip: 2.09 kB[22m
|
|
28
|
-
[34mℹ[39m [34m[ESM][39m [2mdist/[22mzustand-commander/index.d.mts.map [2m 5.
|
|
28
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22mzustand-commander/index.d.mts.map [2m 5.29 kB[22m [2m│ gzip: 2.15 kB[22m
|
|
29
29
|
[34mℹ[39m [34m[ESM][39m [2mdist/[22mobjectSpread2-CxTyNSYl.mjs [2m 2.13 kB[22m [2m│ gzip: 0.77 kB[22m
|
|
30
30
|
[34mℹ[39m [34m[ESM][39m [2mdist/[22mzustand/index.d.mts.map [2m 1.86 kB[22m [2m│ gzip: 0.82 kB[22m
|
|
31
|
-
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[32m[1mzustand-commander/index.d.mts[22m[39m [
|
|
31
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[32m[1mzustand-commander/index.d.mts[22m[39m [2m10.95 kB[22m [2m│ gzip: 3.00 kB[22m
|
|
32
32
|
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[32m[1mzustand/index.d.mts[22m[39m [2m 5.01 kB[22m [2m│ gzip: 1.46 kB[22m
|
|
33
33
|
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[32m[1mindex.d.mts[22m[39m [2m 0.01 kB[22m [2m│ gzip: 0.03 kB[22m
|
|
34
|
-
[34mℹ[39m [34m[ESM][39m 11 files, total:
|
|
35
|
-
[32m✔[39m Build complete in [
|
|
34
|
+
[34mℹ[39m [34m[ESM][39m 11 files, total: 74.95 kB
|
|
35
|
+
[32m✔[39m Build complete in [32m4114ms[39m
|
|
@@ -92,34 +92,24 @@ function createCommander(options = {}) {
|
|
|
92
92
|
};
|
|
93
93
|
};
|
|
94
94
|
};
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
paramsSchema: paramsSchemaOrFn
|
|
100
|
-
};
|
|
101
|
-
if (typeof paramsSchemaOrFn !== "function") throw new Error("Commander: action requires a function");
|
|
95
|
+
/**
|
|
96
|
+
* Create a regular command (no undo support).
|
|
97
|
+
*/
|
|
98
|
+
function action(fn) {
|
|
102
99
|
return {
|
|
103
100
|
[COMMAND_SYMBOL]: true,
|
|
104
|
-
fn
|
|
105
|
-
paramsSchema: null
|
|
101
|
+
fn
|
|
106
102
|
};
|
|
107
103
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
fn: fnOrRevert,
|
|
113
|
-
paramsSchema: paramsSchemaOrFn,
|
|
114
|
-
revert: maybeRevert
|
|
115
|
-
};
|
|
116
|
-
if (typeof paramsSchemaOrFn !== "function") throw new Error("Commander: undoableAction requires a function");
|
|
104
|
+
/**
|
|
105
|
+
* Create an undoable command with undo/redo support.
|
|
106
|
+
*/
|
|
107
|
+
function undoableAction(fn, revert) {
|
|
117
108
|
return {
|
|
118
109
|
[COMMAND_SYMBOL]: true,
|
|
119
110
|
[UNDOABLE_COMMAND_SYMBOL]: true,
|
|
120
|
-
fn
|
|
121
|
-
|
|
122
|
-
revert: fnOrRevert
|
|
111
|
+
fn,
|
|
112
|
+
revert
|
|
123
113
|
};
|
|
124
114
|
}
|
|
125
115
|
/**
|
|
@@ -1,16 +1,7 @@
|
|
|
1
1
|
import { StoreApi, UseBoundStore } from "zustand";
|
|
2
|
-
import { Schema } from "effect";
|
|
3
2
|
|
|
4
3
|
//#region src/zustand-commander/types.d.ts
|
|
5
4
|
|
|
6
|
-
/**
|
|
7
|
-
* Any Effect Schema type (used for type constraints).
|
|
8
|
-
*/
|
|
9
|
-
type AnyEffectSchema = Schema.Schema<any, any, any>;
|
|
10
|
-
/**
|
|
11
|
-
* Infer the Type from an Effect Schema.
|
|
12
|
-
*/
|
|
13
|
-
type InferSchemaType<T> = T extends Schema.Schema<infer A, any, any> ? A : never;
|
|
14
5
|
/**
|
|
15
6
|
* Symbol used to identify Command objects at runtime.
|
|
16
7
|
*/
|
|
@@ -57,7 +48,6 @@ type RevertFn<TStore, TParams, TReturn> = (ctx: CommandContext<TStore>, params:
|
|
|
57
48
|
interface Command<TStore, TParams, TReturn> {
|
|
58
49
|
readonly [COMMAND_SYMBOL]: true;
|
|
59
50
|
readonly fn: CommandFn<TStore, TParams, TReturn>;
|
|
60
|
-
readonly paramsSchema: AnyEffectSchema | null;
|
|
61
51
|
}
|
|
62
52
|
/**
|
|
63
53
|
* An undoable command that supports undo/redo.
|
|
@@ -127,11 +117,10 @@ interface Commander<TStore> {
|
|
|
127
117
|
* Create a regular command (no undo support).
|
|
128
118
|
*
|
|
129
119
|
* @example
|
|
130
|
-
* // With params
|
|
131
|
-
* const addItem = commander.action(
|
|
132
|
-
* Schema.Struct({ name: Schema.String }),
|
|
120
|
+
* // With params
|
|
121
|
+
* const addItem = commander.action<{ name: string }>(
|
|
133
122
|
* (ctx, params) => {
|
|
134
|
-
* // modify state
|
|
123
|
+
* // modify state using params.name
|
|
135
124
|
* }
|
|
136
125
|
* );
|
|
137
126
|
*
|
|
@@ -141,7 +130,7 @@ interface Commander<TStore> {
|
|
|
141
130
|
* });
|
|
142
131
|
*/
|
|
143
132
|
readonly action: {
|
|
144
|
-
<
|
|
133
|
+
<TParams, TReturn = void>(fn: CommandFn<TStore, TParams, TReturn>): Command<TStore, TParams, TReturn>;
|
|
145
134
|
<TReturn = void>(fn: CommandFn<TStore, void, TReturn>): Command<TStore, void, TReturn>;
|
|
146
135
|
};
|
|
147
136
|
/**
|
|
@@ -149,8 +138,7 @@ interface Commander<TStore> {
|
|
|
149
138
|
* The revert function is called when undoing the command.
|
|
150
139
|
*
|
|
151
140
|
* @example
|
|
152
|
-
* const moveItem = commander.undoableAction(
|
|
153
|
-
* Schema.Struct({ id: Schema.String, toIndex: Schema.Number }),
|
|
141
|
+
* const moveItem = commander.undoableAction<{ id: string; toIndex: number }, { fromIndex: number }>(
|
|
154
142
|
* (ctx, params) => {
|
|
155
143
|
* const fromIndex = // get current index
|
|
156
144
|
* // perform move
|
|
@@ -163,7 +151,7 @@ interface Commander<TStore> {
|
|
|
163
151
|
* );
|
|
164
152
|
*/
|
|
165
153
|
readonly undoableAction: {
|
|
166
|
-
<
|
|
154
|
+
<TParams, TReturn>(fn: CommandFn<TStore, TParams, TReturn>, revert: RevertFn<TStore, TParams, TReturn>): UndoableCommand<TStore, TParams, TReturn>;
|
|
167
155
|
<TReturn>(fn: CommandFn<TStore, void, TReturn>, revert: RevertFn<TStore, void, TReturn>): UndoableCommand<TStore, void, TReturn>;
|
|
168
156
|
};
|
|
169
157
|
/**
|
|
@@ -321,5 +309,5 @@ interface UseUndoRedoKeyboardOptions {
|
|
|
321
309
|
*/
|
|
322
310
|
declare function useUndoRedoKeyboard<TStore extends CommanderSlice>(store: UseBoundStore<StoreApi<TStore>>, options?: UseUndoRedoKeyboardOptions): void;
|
|
323
311
|
//#endregion
|
|
324
|
-
export { type AnyCommand, type
|
|
312
|
+
export { type AnyCommand, type AnyUndoableCommand, COMMAND_SYMBOL, type Command, type CommandContext, type CommandDispatch, type CommandFn, type CommandParams, type CommandReturn, type CommandStore, type Commander, type CommanderMiddleware, type CommanderOptions, type CommanderSlice, type ExtractState, type RevertFn, UNDOABLE_COMMAND_SYMBOL, type UndoEntry, type UndoRedoState, type UndoableCommand, type UseUndoRedoKeyboardOptions, clearUndoHistory, createCommander, isCommand, isUndoableCommand, performRedo, performUndo, useCommander, useUndoRedo, useUndoRedoKeyboard };
|
|
325
313
|
//# sourceMappingURL=index.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../../src/zustand-commander/types.ts","../../src/zustand-commander/commander.ts","../../src/zustand-commander/hooks.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../../src/zustand-commander/types.ts","../../src/zustand-commander/commander.ts","../../src/zustand-commander/hooks.ts"],"sourcesContent":[],"mappings":";;;;;;;AAoDoC,cAnCvB,cAmCuB,EAAA,OAAA,MAAA;AAUpC;;;AAEU,cA1CG,uBA0CH,EAAA,OAAA,MAAA;;;AAOV;;AACO,UAtCU,cAsCV,CAAA,MAAA,CAAA,CAAA;EACG;;;EAYO,SAAA,QAAO,EAAA,GAAA,GA/CG,MA+CH;EACZ;;;EAC8B,SAAA,QAAA,EAAA,CAAA,OAAA,EA5CX,OA4CW,CA5CH,MA4CG,CAAA,EAAA,GAAA,IAAA;EAA3B;;AAOf;;;;;EAG4B,SAAA,QAAA,EA7CP,eA6CO,CA7CS,MA6CT,CAAA;;;;;AAFX,KAjCL,SAiCK,CAAA,MAAA,EAAA,OAAA,EAAA,OAAA,CAAA,GAAA,CAAA,GAAA,EAhCV,cAgCU,CAhCK,MAgCL,CAAA,EAAA,MAAA,EA/BP,OA+BO,EAAA,GA9BZ,OA8BY;AAQjB;AAKA;AAaA;;AAC2B,KAnDf,QAmDe,CAAA,MAAA,EAAA,OAAA,EAAA,OAAA,CAAA,GAAA,CAAA,GAAA,EAlDpB,cAkDoB,CAlDL,MAkDK,CAAA,EAAA,MAAA,EAjDjB,OAiDiB,EAAA,MAAA,EAhDjB,OAgDiB,EAAA,GAAA,IAAA;;;;;AACM,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CAAA,eECK,cFDL,CAAA,CAAA,KAAA,EEE7B,aFF6B,CEEf,QFFe,CEEN,MFFM,CAAA,CAAA,CAAA,EEGnC,aFHmC;;;;AAED,UEgDpB,0BAAA,CFhDoB;EAWpB;EAYA,SAAA,UAAS,CAAA,EAAA,OAAA;EAoBN;EAAQ,SAAA,UAAA,CAAA,EAAA,OAAA;;;;;;;;;;;;AAMrB,iBEgBS,mBFhBT,CAAA,eEgB4C,cFhB5C,CAAA,CAAA,KAAA,EEiBE,aFjBF,CEiBgB,QFjBhB,CEiByB,MFjBzB,CAAA,CAAA,EAAA,OAAA,CAAA,EEkBI,0BFlBJ,CAAA,EAAA,IAAA"}
|
|
@@ -1,16 +1,7 @@
|
|
|
1
1
|
import { StoreApi, UseBoundStore } from "zustand";
|
|
2
|
-
import { Schema } from "effect";
|
|
3
2
|
|
|
4
3
|
//#region src/zustand-commander/types.d.ts
|
|
5
4
|
|
|
6
|
-
/**
|
|
7
|
-
* Any Effect Schema type (used for type constraints).
|
|
8
|
-
*/
|
|
9
|
-
type AnyEffectSchema = Schema.Schema<any, any, any>;
|
|
10
|
-
/**
|
|
11
|
-
* Infer the Type from an Effect Schema.
|
|
12
|
-
*/
|
|
13
|
-
type InferSchemaType<T> = T extends Schema.Schema<infer A, any, any> ? A : never;
|
|
14
5
|
/**
|
|
15
6
|
* Symbol used to identify Command objects at runtime.
|
|
16
7
|
*/
|
|
@@ -57,7 +48,6 @@ type RevertFn<TStore, TParams, TReturn> = (ctx: CommandContext<TStore>, params:
|
|
|
57
48
|
interface Command<TStore, TParams, TReturn> {
|
|
58
49
|
readonly [COMMAND_SYMBOL]: true;
|
|
59
50
|
readonly fn: CommandFn<TStore, TParams, TReturn>;
|
|
60
|
-
readonly paramsSchema: AnyEffectSchema | null;
|
|
61
51
|
}
|
|
62
52
|
/**
|
|
63
53
|
* An undoable command that supports undo/redo.
|
|
@@ -127,11 +117,10 @@ interface Commander<TStore> {
|
|
|
127
117
|
* Create a regular command (no undo support).
|
|
128
118
|
*
|
|
129
119
|
* @example
|
|
130
|
-
* // With params
|
|
131
|
-
* const addItem = commander.action(
|
|
132
|
-
* Schema.Struct({ name: Schema.String }),
|
|
120
|
+
* // With params
|
|
121
|
+
* const addItem = commander.action<{ name: string }>(
|
|
133
122
|
* (ctx, params) => {
|
|
134
|
-
* // modify state
|
|
123
|
+
* // modify state using params.name
|
|
135
124
|
* }
|
|
136
125
|
* );
|
|
137
126
|
*
|
|
@@ -141,7 +130,7 @@ interface Commander<TStore> {
|
|
|
141
130
|
* });
|
|
142
131
|
*/
|
|
143
132
|
readonly action: {
|
|
144
|
-
<
|
|
133
|
+
<TParams, TReturn = void>(fn: CommandFn<TStore, TParams, TReturn>): Command<TStore, TParams, TReturn>;
|
|
145
134
|
<TReturn = void>(fn: CommandFn<TStore, void, TReturn>): Command<TStore, void, TReturn>;
|
|
146
135
|
};
|
|
147
136
|
/**
|
|
@@ -149,8 +138,7 @@ interface Commander<TStore> {
|
|
|
149
138
|
* The revert function is called when undoing the command.
|
|
150
139
|
*
|
|
151
140
|
* @example
|
|
152
|
-
* const moveItem = commander.undoableAction(
|
|
153
|
-
* Schema.Struct({ id: Schema.String, toIndex: Schema.Number }),
|
|
141
|
+
* const moveItem = commander.undoableAction<{ id: string; toIndex: number }, { fromIndex: number }>(
|
|
154
142
|
* (ctx, params) => {
|
|
155
143
|
* const fromIndex = // get current index
|
|
156
144
|
* // perform move
|
|
@@ -163,7 +151,7 @@ interface Commander<TStore> {
|
|
|
163
151
|
* );
|
|
164
152
|
*/
|
|
165
153
|
readonly undoableAction: {
|
|
166
|
-
<
|
|
154
|
+
<TParams, TReturn>(fn: CommandFn<TStore, TParams, TReturn>, revert: RevertFn<TStore, TParams, TReturn>): UndoableCommand<TStore, TParams, TReturn>;
|
|
167
155
|
<TReturn>(fn: CommandFn<TStore, void, TReturn>, revert: RevertFn<TStore, void, TReturn>): UndoableCommand<TStore, void, TReturn>;
|
|
168
156
|
};
|
|
169
157
|
/**
|
|
@@ -321,5 +309,5 @@ interface UseUndoRedoKeyboardOptions {
|
|
|
321
309
|
*/
|
|
322
310
|
declare function useUndoRedoKeyboard<TStore extends CommanderSlice>(store: UseBoundStore<StoreApi<TStore>>, options?: UseUndoRedoKeyboardOptions): void;
|
|
323
311
|
//#endregion
|
|
324
|
-
export { type AnyCommand, type
|
|
312
|
+
export { type AnyCommand, type AnyUndoableCommand, COMMAND_SYMBOL, type Command, type CommandContext, type CommandDispatch, type CommandFn, type CommandParams, type CommandReturn, type CommandStore, type Commander, type CommanderMiddleware, type CommanderOptions, type CommanderSlice, type ExtractState, type RevertFn, UNDOABLE_COMMAND_SYMBOL, type UndoEntry, type UndoRedoState, type UndoableCommand, type UseUndoRedoKeyboardOptions, clearUndoHistory, createCommander, isCommand, isUndoableCommand, performRedo, performUndo, useCommander, useUndoRedo, useUndoRedoKeyboard };
|
|
325
313
|
//# sourceMappingURL=index.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/zustand-commander/types.ts","../../src/zustand-commander/commander.ts","../../src/zustand-commander/hooks.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/zustand-commander/types.ts","../../src/zustand-commander/commander.ts","../../src/zustand-commander/hooks.ts"],"sourcesContent":[],"mappings":";;;;;;;AAoDoC,cAnCvB,cAmCuB,EAAA,OAAA,MAAA;AAUpC;;;AAEU,cA1CG,uBA0CH,EAAA,OAAA,MAAA;;;AAOV;;AACO,UAtCU,cAsCV,CAAA,MAAA,CAAA,CAAA;EACG;;;EAYO,SAAA,QAAO,EAAA,GAAA,GA/CG,MA+CH;EACZ;;;EAC8B,SAAA,QAAA,EAAA,CAAA,OAAA,EA5CX,OA4CW,CA5CH,MA4CG,CAAA,EAAA,GAAA,IAAA;EAA3B;;AAOf;;;;;EAG4B,SAAA,QAAA,EA7CP,eA6CO,CA7CS,MA6CT,CAAA;;;;;AAF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EyB,UE+BR,aAAA,CF/BQ;EAAQ;EAAS,SAAA,OAAA,EAAA,OAAA;EAA3B;EAAS,SAAA,OAAA,EAAA,OAAA;EAOP;EACC,SAAA,SAAA,EAAA,MAAA;EAAQ;EAAS,SAAA,SAAA,EAAA,MAAA;EACvB;EACgB,SAAA,IAAA,EAAA,GAAA,GAAA,OAAA;EAAQ;EAAS,SAAA,IAAA,EAAA,GAAA,GAAA,OAAA;EAA1B;EAFT,SAAA,KAAA,EAAA,GAAA,GAAA,IAAA;;AAQV;AAKA;AAaA;;;;;;;;AAYA;;;;;AAcA;AAGsC,iBECtB,WFDsB,CAAA,eECK,cFDL,CAAA,CAAA,KAAA,EEE7B,aFF6B,CEEf,QFFe,CEEN,MFFM,CAAA,CAAA,CAAA,EEGnC,aFHmC;;;;AAED,UEgDpB,0BAAA,CFhDoB;EAWpB;EAYA,SAAA,UAAS,CAAA,EAAA,OAAA;EAoBN;EAAQ,SAAA,UAAA,CAAA,EAAA,OAAA;;;;;;;;;;;;AAMrB,iBEgBS,mBFhBT,CAAA,eEgB4C,cFhB5C,CAAA,CAAA,KAAA,EEiBE,aFjBF,CEiBgB,QFjBhB,CEiByB,MFjBzB,CAAA,CAAA,EAAA,OAAA,CAAA,EEkBI,0BFlBJ,CAAA,EAAA,IAAA"}
|
|
@@ -92,34 +92,24 @@ function createCommander(options = {}) {
|
|
|
92
92
|
};
|
|
93
93
|
};
|
|
94
94
|
};
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
paramsSchema: paramsSchemaOrFn
|
|
100
|
-
};
|
|
101
|
-
if (typeof paramsSchemaOrFn !== "function") throw new Error("Commander: action requires a function");
|
|
95
|
+
/**
|
|
96
|
+
* Create a regular command (no undo support).
|
|
97
|
+
*/
|
|
98
|
+
function action(fn) {
|
|
102
99
|
return {
|
|
103
100
|
[COMMAND_SYMBOL]: true,
|
|
104
|
-
fn
|
|
105
|
-
paramsSchema: null
|
|
101
|
+
fn
|
|
106
102
|
};
|
|
107
103
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
fn: fnOrRevert,
|
|
113
|
-
paramsSchema: paramsSchemaOrFn,
|
|
114
|
-
revert: maybeRevert
|
|
115
|
-
};
|
|
116
|
-
if (typeof paramsSchemaOrFn !== "function") throw new Error("Commander: undoableAction requires a function");
|
|
104
|
+
/**
|
|
105
|
+
* Create an undoable command with undo/redo support.
|
|
106
|
+
*/
|
|
107
|
+
function undoableAction(fn, revert) {
|
|
117
108
|
return {
|
|
118
109
|
[COMMAND_SYMBOL]: true,
|
|
119
110
|
[UNDOABLE_COMMAND_SYMBOL]: true,
|
|
120
|
-
fn
|
|
121
|
-
|
|
122
|
-
revert: fnOrRevert
|
|
111
|
+
fn,
|
|
112
|
+
revert
|
|
123
113
|
};
|
|
124
114
|
}
|
|
125
115
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["DEFAULT_OPTIONS: Required<CommanderOptions>","_storeApi: StoreApi<TStore & CommanderSlice> | null","ctx: CommandContext<TStore & CommanderSlice>","entry: UndoEntry<TParams, TReturn>","ctx: CommandContext<TStore>","newEntry: UndoEntry","dispatch: CommandDispatch<TStore>","ctx: CommandContext<TStore>"],"sources":["../../src/zustand-commander/types.ts","../../src/zustand-commander/commander.ts","../../src/zustand-commander/hooks.ts"],"sourcesContent":["/**\n * @voidhash/mimic-react/zustand-commander\n *\n * Type definitions for the zustand-commander package.\n *\n * @since 0.0.1\n */\n\nimport type { Schema } from \"effect\";\nimport type { StoreApi, UseBoundStore } from \"zustand\";\n\n// =============================================================================\n// Schema Types\n// =============================================================================\n\n/**\n * Any Effect Schema type (used for type constraints).\n */\nexport type AnyEffectSchema = Schema.Schema<any, any, any>;\n\n/**\n * Infer the Type from an Effect Schema.\n */\nexport type InferSchemaType<T> = T extends Schema.Schema<infer A, any, any>\n ? A\n : never;\n\n// =============================================================================\n// Command Symbol & Type Guard\n// =============================================================================\n\n/**\n * Symbol used to identify Command objects at runtime.\n */\nexport const COMMAND_SYMBOL = Symbol.for(\"zustand-commander/command\");\n\n/**\n * Symbol used to identify UndoableCommand objects at runtime.\n */\nexport const UNDOABLE_COMMAND_SYMBOL = Symbol.for(\n \"zustand-commander/undoable-command\"\n);\n\n// =============================================================================\n// Command Context\n// =============================================================================\n\n/**\n * Context provided to command functions.\n * Gives access to store state and dispatch capabilities.\n */\nexport interface CommandContext<TStore> {\n /**\n * Get the current store state.\n */\n readonly getState: () => TStore;\n\n /**\n * Set partial store state (for local/browser state updates).\n */\n readonly setState: (partial: Partial<TStore>) => void;\n\n /**\n * Dispatch another command.\n * Returns the result of the dispatched command.\n *\n * @example\n * dispatch(otherCommand)({ param: \"value\" });\n */\n readonly dispatch: CommandDispatch<TStore>;\n}\n\n// =============================================================================\n// Command Function Types\n// =============================================================================\n\n/**\n * The function signature for a command handler.\n */\nexport type CommandFn<TStore, TParams, TReturn> = (\n ctx: CommandContext<TStore>,\n params: TParams\n) => TReturn;\n\n/**\n * The function signature for an undoable command's revert handler.\n * Receives the original params and the result from the forward execution.\n */\nexport type RevertFn<TStore, TParams, TReturn> = (\n ctx: CommandContext<TStore>,\n params: TParams,\n result: TReturn\n) => void;\n\n// =============================================================================\n// Command Types\n// =============================================================================\n\n/**\n * A command that can be dispatched to modify store state.\n * Regular commands do not support undo/redo.\n */\nexport interface Command<TStore, TParams, TReturn> {\n readonly [COMMAND_SYMBOL]: true;\n readonly fn: CommandFn<TStore, TParams, TReturn>;\n readonly paramsSchema: AnyEffectSchema | null;\n}\n\n/**\n * An undoable command that supports undo/redo.\n * Must provide a revert function that knows how to undo the change.\n */\nexport interface UndoableCommand<TStore, TParams, TReturn>\n extends Command<TStore, TParams, TReturn> {\n readonly [UNDOABLE_COMMAND_SYMBOL]: true;\n readonly revert: RevertFn<TStore, TParams, TReturn>;\n}\n\n/**\n * Any command type (regular or undoable).\n */\nexport type AnyCommand = Command<any, any, any>;\n\n/**\n * Any undoable command type.\n */\nexport type AnyUndoableCommand = UndoableCommand<any, any, any>;\n\n// =============================================================================\n// Command Dispatch\n// =============================================================================\n\n/**\n * Dispatch function that accepts commands and returns a function to call with params.\n * Returns the result of the command execution.\n *\n * @example\n * const result = dispatch(myCommand)({ param: \"value\" });\n */\nexport type CommandDispatch<TStore> = <TParams, TReturn>(\n command: Command<TStore, TParams, TReturn>\n) => (params: TParams) => TReturn;\n\n// =============================================================================\n// Undo/Redo Stack Types\n// =============================================================================\n\n/**\n * An entry in the undo/redo stack.\n * Contains all information needed to revert or redo a command.\n */\nexport interface UndoEntry<TParams = unknown, TReturn = unknown> {\n /** The undoable command that was executed */\n readonly command: AnyUndoableCommand;\n /** The parameters that were passed to the command */\n readonly params: TParams;\n /** The result returned by the command (passed to revert) */\n readonly result: TReturn;\n /** Timestamp when the command was executed */\n readonly timestamp: number;\n}\n\n/**\n * State slice for undo/redo functionality.\n */\nexport interface CommanderSlice {\n readonly _commander: {\n /** Stack of commands that can be undone */\n readonly undoStack: ReadonlyArray<UndoEntry>;\n /** Stack of commands that can be redone */\n readonly redoStack: ReadonlyArray<UndoEntry>;\n };\n}\n\n// =============================================================================\n// Commander Types\n// =============================================================================\n\n/**\n * Options for creating a commander.\n */\nexport interface CommanderOptions {\n /**\n * Maximum number of undo entries to keep.\n * @default 100\n */\n readonly maxUndoStackSize?: number;\n}\n\n/**\n * A commander instance bound to a specific store type.\n * Used to create commands and the middleware.\n */\nexport interface Commander<TStore> {\n /**\n * Create a regular command (no undo support).\n *\n * @example\n * // With params schema\n * const addItem = commander.action(\n * Schema.Struct({ name: Schema.String }),\n * (ctx, params) => {\n * // modify state\n * }\n * );\n *\n * // Without params\n * const clearAll = commander.action((ctx) => {\n * // modify state\n * });\n */\n readonly action: {\n // With params schema\n <TParamsSchema extends AnyEffectSchema, TReturn = void>(\n paramsSchema: TParamsSchema,\n fn: CommandFn<TStore, InferSchemaType<TParamsSchema>, TReturn>\n ): Command<TStore, InferSchemaType<TParamsSchema>, TReturn>;\n\n // Without params (void)\n <TReturn = void>(\n fn: CommandFn<TStore, void, TReturn>\n ): Command<TStore, void, TReturn>;\n };\n\n /**\n * Create an undoable command with undo/redo support.\n * The revert function is called when undoing the command.\n *\n * @example\n * const moveItem = commander.undoableAction(\n * Schema.Struct({ id: Schema.String, toIndex: Schema.Number }),\n * (ctx, params) => {\n * const fromIndex = // get current index\n * // perform move\n * return { fromIndex }; // return data needed for revert\n * },\n * (ctx, params, result) => {\n * // revert: move back to original position\n * ctx.dispatch(moveItem)({ id: params.id, toIndex: result.fromIndex });\n * }\n * );\n */\n readonly undoableAction: {\n // With params schema\n <TParamsSchema extends AnyEffectSchema, TReturn>(\n paramsSchema: TParamsSchema,\n fn: CommandFn<TStore, InferSchemaType<TParamsSchema>, TReturn>,\n revert: RevertFn<TStore, InferSchemaType<TParamsSchema>, TReturn>\n ): UndoableCommand<TStore, InferSchemaType<TParamsSchema>, TReturn>;\n\n // Without params (void)\n <TReturn>(\n fn: CommandFn<TStore, void, TReturn>,\n revert: RevertFn<TStore, void, TReturn>\n ): UndoableCommand<TStore, void, TReturn>;\n };\n\n /**\n * Zustand middleware that adds commander functionality.\n * Adds undo/redo stacks to the store state.\n */\n readonly middleware: CommanderMiddleware<TStore>;\n}\n\n/**\n * Type for the commander middleware.\n * Note: TStore is intentionally unused here to match the Commander interface signature.\n * The middleware is generic over T (the inner store type) and adds CommanderSlice.\n */\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nexport type CommanderMiddleware<_TStore> = <T extends object>(\n config: (\n set: StoreApi<T & CommanderSlice>[\"setState\"],\n get: StoreApi<T & CommanderSlice>[\"getState\"],\n api: StoreApi<T & CommanderSlice>\n ) => T\n) => (\n set: StoreApi<T & CommanderSlice>[\"setState\"],\n get: StoreApi<T & CommanderSlice>[\"getState\"],\n api: StoreApi<T & CommanderSlice>\n) => T & CommanderSlice;\n\n// =============================================================================\n// Type Helpers\n// =============================================================================\n\n/**\n * Extract the params type from a command.\n */\nexport type CommandParams<T> = T extends Command<any, infer P, any> ? P : never;\n\n/**\n * Extract the return type from a command.\n */\nexport type CommandReturn<T> = T extends Command<any, any, infer R>\n ? R\n : undefined;\n\n/**\n * Extract the store type from a command.\n */\nexport type CommandStore<T> = T extends Command<infer S, any, any> ? S : never;\n\n// =============================================================================\n// Type Guards\n// =============================================================================\n\n/**\n * Type guard to check if a value is a Command.\n */\nexport function isCommand(value: unknown): value is AnyCommand {\n return (\n typeof value === \"object\" &&\n value !== null &&\n COMMAND_SYMBOL in value &&\n value[COMMAND_SYMBOL] === true\n );\n}\n\n/**\n * Type guard to check if a command is undoable.\n */\nexport function isUndoableCommand(\n value: unknown\n): value is AnyUndoableCommand {\n return (\n isCommand(value) &&\n UNDOABLE_COMMAND_SYMBOL in value &&\n value[UNDOABLE_COMMAND_SYMBOL] === true\n );\n}\n\n// =============================================================================\n// Store Type Helper\n// =============================================================================\n\n/**\n * Helper type to extract the state type from a zustand store.\n */\nexport type ExtractState<TStore> = TStore extends UseBoundStore<\n StoreApi<infer S>\n>\n ? S\n : TStore extends StoreApi<infer S>\n ? S\n : never;\n\n","/**\n * @voidhash/mimic-react/zustand-commander\n *\n * Commander creation and command definition.\n *\n * @since 0.0.1\n */\n\nimport type { StoreApi } from \"zustand\";\nimport {\n COMMAND_SYMBOL,\n UNDOABLE_COMMAND_SYMBOL,\n isUndoableCommand,\n type AnyEffectSchema,\n type Command,\n type Commander,\n type CommanderOptions,\n type CommanderSlice,\n type CommandContext,\n type CommandDispatch,\n type CommandFn,\n type InferSchemaType,\n type RevertFn,\n type UndoableCommand,\n type UndoEntry,\n} from \"./types.js\";\n\n// =============================================================================\n// Default Options\n// =============================================================================\n\nconst DEFAULT_OPTIONS: Required<CommanderOptions> = {\n maxUndoStackSize: 100,\n};\n\n// =============================================================================\n// Commander Implementation\n// =============================================================================\n\n/**\n * Creates a commander instance bound to a specific store type.\n *\n * @example\n * ```ts\n * // Create commander for your store type\n * const commander = createCommander<StoreState>();\n *\n * // Define commands\n * const addItem = commander.action(\n * Schema.Struct({ name: Schema.String }),\n * (ctx, params) => {\n * const { mimic } = ctx.getState();\n * mimic.document.transaction(root => {\n * // add item\n * });\n * }\n * );\n *\n * // Create store with middleware\n * const useStore = create(\n * commander.middleware(\n * mimic(document, (set, get) => ({\n * // your state\n * }))\n * )\n * );\n * ```\n */\nexport function createCommander<TStore extends object>(\n options: CommanderOptions = {}\n): Commander<TStore & CommanderSlice> {\n const { maxUndoStackSize } = { ...DEFAULT_OPTIONS, ...options };\n\n // Track the store API once middleware is applied\n let _storeApi: StoreApi<TStore & CommanderSlice> | null = null;\n\n /**\n * Creates the dispatch function for use within command handlers.\n */\n const createDispatch = (): CommandDispatch<TStore & CommanderSlice> => {\n return <TParams, TReturn>(\n command: Command<TStore & CommanderSlice, TParams, TReturn>\n ) => {\n return (params: TParams): TReturn => {\n if (!_storeApi) {\n throw new Error(\n \"Commander: Store not initialized. Make sure to use the commander middleware.\"\n );\n }\n\n // Create context for the command\n const ctx: CommandContext<TStore & CommanderSlice> = {\n getState: () => _storeApi!.getState(),\n setState: (partial) => _storeApi!.setState(partial as any),\n dispatch: createDispatch(),\n };\n\n // Execute the command\n const result = command.fn(ctx, params);\n\n // If it's an undoable command, add to undo stack\n if (isUndoableCommand(command)) {\n const entry: UndoEntry<TParams, TReturn> = {\n command,\n params,\n result,\n timestamp: Date.now(),\n };\n\n _storeApi.setState((state: TStore & CommanderSlice) => {\n const { undoStack, redoStack } = state._commander;\n\n // Add to undo stack, respecting max size\n const newUndoStack = [...undoStack, entry].slice(-maxUndoStackSize);\n\n // Clear redo stack when a new command is executed\n return {\n ...state,\n _commander: {\n undoStack: newUndoStack,\n redoStack: [],\n },\n };\n });\n }\n\n return result;\n };\n };\n };\n\n /**\n * Create a regular command (no undo support).\n */\n function action<TParamsSchema extends AnyEffectSchema, TReturn = void>(\n paramsSchema: TParamsSchema,\n fn: CommandFn<TStore & CommanderSlice, InferSchemaType<TParamsSchema>, TReturn>\n ): Command<TStore & CommanderSlice, InferSchemaType<TParamsSchema>, TReturn>;\n function action<TReturn = void>(\n fn: CommandFn<TStore & CommanderSlice, void, TReturn>\n ): Command<TStore & CommanderSlice, void, TReturn>;\n function action<TParamsSchema extends AnyEffectSchema, TReturn = void>(\n paramsSchemaOrFn:\n | TParamsSchema\n | CommandFn<TStore & CommanderSlice, void, TReturn>,\n maybeFn?: CommandFn<\n TStore & CommanderSlice,\n InferSchemaType<TParamsSchema>,\n TReturn\n >\n ): Command<TStore & CommanderSlice, any, TReturn> {\n // Check if we have two arguments (schema + fn) or just one (fn only)\n if (maybeFn !== undefined) {\n // First arg is schema, second is fn\n return {\n [COMMAND_SYMBOL]: true,\n fn: maybeFn,\n paramsSchema: paramsSchemaOrFn as TParamsSchema,\n };\n }\n\n // Single argument - must be the action function (no params schema)\n if (typeof paramsSchemaOrFn !== \"function\") {\n throw new Error(\"Commander: action requires a function\");\n }\n\n return {\n [COMMAND_SYMBOL]: true,\n fn: paramsSchemaOrFn as CommandFn<TStore & CommanderSlice, void, TReturn>,\n paramsSchema: null,\n };\n }\n\n /**\n * Create an undoable command with undo/redo support.\n */\n function undoableAction<TParamsSchema extends AnyEffectSchema, TReturn>(\n paramsSchema: TParamsSchema,\n fn: CommandFn<TStore & CommanderSlice, InferSchemaType<TParamsSchema>, TReturn>,\n revert: RevertFn<TStore & CommanderSlice, InferSchemaType<TParamsSchema>, TReturn>\n ): UndoableCommand<TStore & CommanderSlice, InferSchemaType<TParamsSchema>, TReturn>;\n function undoableAction<TReturn>(\n fn: CommandFn<TStore & CommanderSlice, void, TReturn>,\n revert: RevertFn<TStore & CommanderSlice, void, TReturn>\n ): UndoableCommand<TStore & CommanderSlice, void, TReturn>;\n function undoableAction<TParamsSchema extends AnyEffectSchema, TReturn>(\n paramsSchemaOrFn:\n | TParamsSchema\n | CommandFn<TStore & CommanderSlice, void, TReturn>,\n fnOrRevert:\n | CommandFn<TStore & CommanderSlice, InferSchemaType<TParamsSchema>, TReturn>\n | RevertFn<TStore & CommanderSlice, void, TReturn>,\n maybeRevert?: RevertFn<\n TStore & CommanderSlice,\n InferSchemaType<TParamsSchema>,\n TReturn\n >\n ): UndoableCommand<TStore & CommanderSlice, any, TReturn> {\n // Check if we have three arguments (schema + fn + revert) or two (fn + revert)\n if (maybeRevert !== undefined) {\n // First arg is schema, second is fn, third is revert\n return {\n [COMMAND_SYMBOL]: true,\n [UNDOABLE_COMMAND_SYMBOL]: true,\n fn: fnOrRevert as CommandFn<\n TStore & CommanderSlice,\n InferSchemaType<TParamsSchema>,\n TReturn\n >,\n paramsSchema: paramsSchemaOrFn as TParamsSchema,\n revert: maybeRevert,\n };\n }\n\n // Two arguments - fn + revert (no params schema)\n if (typeof paramsSchemaOrFn !== \"function\") {\n throw new Error(\"Commander: undoableAction requires a function\");\n }\n\n return {\n [COMMAND_SYMBOL]: true,\n [UNDOABLE_COMMAND_SYMBOL]: true,\n fn: paramsSchemaOrFn as CommandFn<TStore & CommanderSlice, void, TReturn>,\n paramsSchema: null,\n revert: fnOrRevert as RevertFn<TStore & CommanderSlice, void, TReturn>,\n };\n }\n\n /**\n * Zustand middleware that adds commander functionality.\n */\n const middleware = <T extends object>(\n config: (\n set: StoreApi<T & CommanderSlice>[\"setState\"],\n get: StoreApi<T & CommanderSlice>[\"getState\"],\n api: StoreApi<T & CommanderSlice>\n ) => T\n ) => {\n return (\n set: StoreApi<T & CommanderSlice>[\"setState\"],\n get: StoreApi<T & CommanderSlice>[\"getState\"],\n api: StoreApi<T & CommanderSlice>\n ): T & CommanderSlice => {\n // Store the API reference for dispatch\n _storeApi = api as unknown as StoreApi<TStore & CommanderSlice>;\n\n // Get user's state\n const userState = config(set, get, api);\n\n // Add commander slice\n return {\n ...userState,\n _commander: {\n undoStack: [],\n redoStack: [],\n },\n };\n };\n };\n\n return {\n action,\n undoableAction,\n middleware: middleware as Commander<TStore & CommanderSlice>[\"middleware\"],\n };\n}\n\n// =============================================================================\n// Undo/Redo Functions\n// =============================================================================\n\n/**\n * Perform an undo operation on the store.\n * Returns true if an undo was performed, false if undo stack was empty.\n */\nexport function performUndo<TStore extends CommanderSlice>(\n storeApi: StoreApi<TStore>\n): boolean {\n const state = storeApi.getState();\n const { undoStack, redoStack } = state._commander;\n\n // Pop the last entry from undo stack\n const entry = undoStack[undoStack.length - 1];\n if (!entry) {\n return false;\n }\n\n const newUndoStack = undoStack.slice(0, -1);\n\n // Create context for the revert function\n const ctx: CommandContext<TStore> = {\n getState: () => storeApi.getState(),\n setState: (partial) => storeApi.setState(partial as any),\n dispatch: createDispatchForUndo(storeApi),\n };\n\n // Execute the revert function\n entry.command.revert(ctx, entry.params, entry.result);\n\n // Move entry to redo stack\n storeApi.setState((state: TStore) => ({\n ...state,\n _commander: {\n undoStack: newUndoStack,\n redoStack: [...redoStack, entry],\n },\n }));\n\n return true;\n}\n\n/**\n * Perform a redo operation on the store.\n * Returns true if a redo was performed, false if redo stack was empty.\n */\nexport function performRedo<TStore extends CommanderSlice>(\n storeApi: StoreApi<TStore>\n): boolean {\n const state = storeApi.getState();\n const { undoStack, redoStack } = state._commander;\n\n // Pop the last entry from redo stack\n const entry = redoStack[redoStack.length - 1];\n if (!entry) {\n return false;\n }\n\n const newRedoStack = redoStack.slice(0, -1);\n\n // Create context for re-executing the command\n const ctx: CommandContext<TStore> = {\n getState: () => storeApi.getState(),\n setState: (partial) => storeApi.setState(partial as any),\n dispatch: createDispatchForUndo(storeApi),\n };\n\n // Re-execute the command\n const result = entry.command.fn(ctx, entry.params);\n\n // Create new entry with potentially new result\n const newEntry: UndoEntry = {\n command: entry.command,\n params: entry.params,\n result,\n timestamp: Date.now(),\n };\n\n // Move entry back to undo stack\n storeApi.setState((state: TStore) => ({\n ...state,\n _commander: {\n undoStack: [...undoStack, newEntry],\n redoStack: newRedoStack,\n },\n }));\n\n return true;\n}\n\n/**\n * Creates a dispatch function for use during undo/redo operations.\n * This dispatch does NOT add to undo stack (to avoid infinite loops).\n */\nfunction createDispatchForUndo<TStore>(\n storeApi: StoreApi<TStore>\n): CommandDispatch<TStore> {\n return <TParams, TReturn>(command: Command<TStore, TParams, TReturn>) => {\n return (params: TParams): TReturn => {\n const ctx: CommandContext<TStore> = {\n getState: () => storeApi.getState(),\n setState: (partial) => storeApi.setState(partial as any),\n dispatch: createDispatchForUndo(storeApi),\n };\n\n // Execute without adding to undo stack\n return command.fn(ctx, params);\n };\n };\n}\n\n/**\n * Clear the undo and redo stacks.\n */\nexport function clearUndoHistory<TStore extends CommanderSlice>(\n storeApi: StoreApi<TStore>\n): void {\n storeApi.setState((state: TStore) => ({\n ...state,\n _commander: {\n undoStack: [],\n redoStack: [],\n },\n }));\n}\n\n","/**\n * @voidhash/mimic-react/zustand-commander\n *\n * React hooks for zustand-commander.\n *\n * @since 0.0.1\n */\n\nimport { useCallback, useEffect, useMemo } from \"react\";\nimport { useStore, type StoreApi, type UseBoundStore } from \"zustand\";\nimport { performRedo, performUndo, clearUndoHistory } from \"./commander\";\nimport {\n isUndoableCommand,\n type Command,\n type CommandContext,\n type CommandDispatch,\n type CommanderSlice,\n type ExtractState,\n} from \"./types.js\";\n\n// =============================================================================\n// useCommander Hook\n// =============================================================================\n\n/**\n * Creates a dispatch function for commands.\n * This is for use outside of React components (e.g., in command handlers).\n */\nfunction createDispatchFromApi<TStore extends CommanderSlice>(\n storeApi: StoreApi<TStore>,\n maxUndoStackSize = 100\n): CommandDispatch<TStore> {\n const dispatch: CommandDispatch<TStore> = <TParams, TReturn>(\n command: Command<TStore, TParams, TReturn>\n ) => {\n return (params: TParams): TReturn => {\n // Create context for the command\n const ctx: CommandContext<TStore> = {\n getState: () => storeApi.getState(),\n setState: (partial) => storeApi.setState(partial as Partial<TStore>),\n dispatch,\n };\n\n // Execute the command\n const result = command.fn(ctx, params);\n\n // If it's an undoable command, add to undo stack\n if (isUndoableCommand(command)) {\n storeApi.setState((state: TStore) => {\n const { undoStack } = state._commander;\n\n // Add to undo stack, respecting max size\n const newUndoStack = [\n ...undoStack,\n {\n command,\n params,\n result,\n timestamp: Date.now(),\n },\n ].slice(-maxUndoStackSize);\n\n // Clear redo stack when a new command is executed\n return {\n ...state,\n _commander: {\n undoStack: newUndoStack,\n redoStack: [],\n },\n } as TStore;\n });\n }\n\n return result;\n };\n };\n\n return dispatch;\n}\n\n/**\n * React hook to get a dispatch function for commands.\n * The dispatch function executes commands and manages undo/redo state.\n *\n * @example\n * ```tsx\n * const dispatch = useCommander(useStore);\n *\n * const handleClick = () => {\n * dispatch(addCard)({ columnId: \"col-1\", title: \"New Card\" });\n * };\n * ```\n */\nexport function useCommander<TStore extends CommanderSlice>(\n store: UseBoundStore<StoreApi<TStore>>\n): CommandDispatch<TStore> {\n // Get the store API\n const storeApi = useMemo(() => {\n // UseBoundStore has the StoreApi attached\n return store as unknown as StoreApi<TStore>;\n }, [store]);\n\n // Create a stable dispatch function\n const dispatch = useMemo(\n () => createDispatchFromApi<TStore>(storeApi),\n [storeApi]\n );\n\n return dispatch as CommandDispatch<TStore>;\n}\n\n// =============================================================================\n// useUndoRedo Hook\n// =============================================================================\n\n/**\n * State and actions for undo/redo functionality.\n */\nexport interface UndoRedoState {\n /** Whether there are actions that can be undone */\n readonly canUndo: boolean;\n /** Whether there are actions that can be redone */\n readonly canRedo: boolean;\n /** Number of items in the undo stack */\n readonly undoCount: number;\n /** Number of items in the redo stack */\n readonly redoCount: number;\n /** Undo the last action */\n readonly undo: () => boolean;\n /** Redo the last undone action */\n readonly redo: () => boolean;\n /** Clear the undo/redo history */\n readonly clear: () => void;\n}\n\n/**\n * React hook for undo/redo functionality.\n * Provides state (canUndo, canRedo) and actions (undo, redo, clear).\n *\n * @example\n * ```tsx\n * const { canUndo, canRedo, undo, redo } = useUndoRedo(useStore);\n *\n * return (\n * <>\n * <button onClick={undo} disabled={!canUndo}>Undo</button>\n * <button onClick={redo} disabled={!canRedo}>Redo</button>\n * </>\n * );\n * ```\n */\nexport function useUndoRedo<TStore extends CommanderSlice>(\n store: UseBoundStore<StoreApi<TStore>>\n): UndoRedoState {\n // Get the store API\n const storeApi = useMemo(() => {\n return store as unknown as StoreApi<TStore>;\n }, [store]);\n\n // Subscribe to commander state\n const commanderState = useStore(\n store,\n (state: TStore) => state._commander\n );\n\n const canUndo = commanderState.undoStack.length > 0;\n const canRedo = commanderState.redoStack.length > 0;\n const undoCount = commanderState.undoStack.length;\n const redoCount = commanderState.redoStack.length;\n\n const undo = useCallback(() => {\n return performUndo(storeApi);\n }, [storeApi]);\n\n const redo = useCallback(() => {\n return performRedo(storeApi);\n }, [storeApi]);\n\n const clear = useCallback(() => {\n clearUndoHistory(storeApi);\n }, [storeApi]);\n\n return {\n canUndo,\n canRedo,\n undoCount,\n redoCount,\n undo,\n redo,\n clear,\n };\n}\n\n// =============================================================================\n// Keyboard Shortcut Hook\n// =============================================================================\n\n/**\n * Options for the keyboard shortcut hook.\n */\nexport interface UseUndoRedoKeyboardOptions {\n /** Enable Ctrl/Cmd+Z for undo (default: true) */\n readonly enableUndo?: boolean;\n /** Enable Ctrl/Cmd+Shift+Z or Ctrl+Y for redo (default: true) */\n readonly enableRedo?: boolean;\n}\n\n/**\n * React hook that adds keyboard shortcuts for undo/redo.\n * Listens for Ctrl/Cmd+Z (undo) and Ctrl/Cmd+Shift+Z or Ctrl+Y (redo).\n *\n * @example\n * ```tsx\n * // In your app component\n * useUndoRedoKeyboard(useStore);\n * ```\n */\nexport function useUndoRedoKeyboard<TStore extends CommanderSlice>(\n store: UseBoundStore<StoreApi<TStore>>,\n options: UseUndoRedoKeyboardOptions = {}\n): void {\n const { enableUndo = true, enableRedo = true } = options;\n\n const storeApi = useMemo(() => {\n return store as unknown as StoreApi<TStore>;\n }, [store]);\n\n // Set up keyboard listener\n useEffect(() => {\n if (typeof window === \"undefined\") {\n return;\n }\n\n const handleKeyDown = (event: KeyboardEvent) => {\n const isMac = navigator.platform.toUpperCase().indexOf(\"MAC\") >= 0;\n const modKey = isMac ? event.metaKey : event.ctrlKey;\n\n if (!modKey) return;\n\n // Undo: Ctrl/Cmd + Z (without Shift)\n if (enableUndo && event.key === \"z\" && !event.shiftKey) {\n event.preventDefault();\n performUndo(storeApi);\n return;\n }\n\n // Redo: Ctrl/Cmd + Shift + Z or Ctrl + Y\n if (enableRedo) {\n if ((event.key === \"z\" && event.shiftKey) || event.key === \"y\") {\n event.preventDefault();\n performRedo(storeApi);\n }\n }\n };\n\n window.addEventListener(\"keydown\", handleKeyDown);\n return () => window.removeEventListener(\"keydown\", handleKeyDown);\n }, [storeApi, enableUndo, enableRedo]);\n}\n"],"mappings":";;;;;;;;AAkCA,MAAa,iBAAiB,OAAO,IAAI,4BAA4B;;;;AAKrE,MAAa,0BAA0B,OAAO,IAC5C,qCACD;;;;AA6QD,SAAgB,UAAU,OAAqC;AAC7D,QACE,OAAO,UAAU,YACjB,UAAU,QACV,kBAAkB,SAClB,MAAM,oBAAoB;;;;;AAO9B,SAAgB,kBACd,OAC6B;AAC7B,QACE,UAAU,MAAM,IAChB,2BAA2B,SAC3B,MAAM,6BAA6B;;;;;ACzSvC,MAAMA,kBAA8C,EAClD,kBAAkB,KACnB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCD,SAAgB,gBACd,UAA4B,EAAE,EACM;CACpC,MAAM,EAAE,uDAA0B,kBAAoB;CAGtD,IAAIC,YAAsD;;;;CAK1D,MAAM,uBAAiE;AACrE,UACE,YACG;AACH,WAAQ,WAA6B;AACnC,QAAI,CAAC,UACH,OAAM,IAAI,MACR,+EACD;IAIH,MAAMC,MAA+C;KACnD,gBAAgB,UAAW,UAAU;KACrC,WAAW,YAAY,UAAW,SAAS,QAAe;KAC1D,UAAU,gBAAgB;KAC3B;IAGD,MAAM,SAAS,QAAQ,GAAG,KAAK,OAAO;AAGtC,QAAI,kBAAkB,QAAQ,EAAE;KAC9B,MAAMC,QAAqC;MACzC;MACA;MACA;MACA,WAAW,KAAK,KAAK;MACtB;AAED,eAAU,UAAU,UAAmC;MACrD,MAAM,EAAE,WAAW,cAAc,MAAM;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C1C,MAAM,UAAU,MAAM,SAEhC;AAGb,OAAI,cAAc,MAAM,QAAQ,OAAO,CAAC,MAAM,UAAU;AACtD,UAAM,gBAAgB;AACtB,gBAAY,SAAS;AACrB;;AAIF,OAAI,YACF;QAAK,MAAM,QAAQ,OAAO,MAAM,YAAa,MAAM,QAAQ,KAAK;AAC9D,WAAM,gBAAgB;AACtB,iBAAY,SAAS;;;;AAK3B,SAAO,iBAAiB,WAAW,cAAc;AACjD,eAAa,OAAO,oBAAoB,WAAW,cAAc;IAChE;EAAC;EAAU;EAAY;EAAW,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["DEFAULT_OPTIONS: Required<CommanderOptions>","_storeApi: StoreApi<TStore & CommanderSlice> | null","ctx: CommandContext<TStore & CommanderSlice>","entry: UndoEntry<TParams, TReturn>","ctx: CommandContext<TStore>","newEntry: UndoEntry","dispatch: CommandDispatch<TStore>","ctx: CommandContext<TStore>"],"sources":["../../src/zustand-commander/types.ts","../../src/zustand-commander/commander.ts","../../src/zustand-commander/hooks.ts"],"sourcesContent":["/**\n * @voidhash/mimic-react/zustand-commander\n *\n * Type definitions for the zustand-commander package.\n *\n * @since 0.0.1\n */\n\nimport type { StoreApi, UseBoundStore } from \"zustand\";\n\n// =============================================================================\n// Command Symbol & Type Guard\n// =============================================================================\n\n/**\n * Symbol used to identify Command objects at runtime.\n */\nexport const COMMAND_SYMBOL = Symbol.for(\"zustand-commander/command\");\n\n/**\n * Symbol used to identify UndoableCommand objects at runtime.\n */\nexport const UNDOABLE_COMMAND_SYMBOL = Symbol.for(\n \"zustand-commander/undoable-command\"\n);\n\n// =============================================================================\n// Command Context\n// =============================================================================\n\n/**\n * Context provided to command functions.\n * Gives access to store state and dispatch capabilities.\n */\nexport interface CommandContext<TStore> {\n /**\n * Get the current store state.\n */\n readonly getState: () => TStore;\n\n /**\n * Set partial store state (for local/browser state updates).\n */\n readonly setState: (partial: Partial<TStore>) => void;\n\n /**\n * Dispatch another command.\n * Returns the result of the dispatched command.\n *\n * @example\n * dispatch(otherCommand)({ param: \"value\" });\n */\n readonly dispatch: CommandDispatch<TStore>;\n}\n\n// =============================================================================\n// Command Function Types\n// =============================================================================\n\n/**\n * The function signature for a command handler.\n */\nexport type CommandFn<TStore, TParams, TReturn> = (\n ctx: CommandContext<TStore>,\n params: TParams\n) => TReturn;\n\n/**\n * The function signature for an undoable command's revert handler.\n * Receives the original params and the result from the forward execution.\n */\nexport type RevertFn<TStore, TParams, TReturn> = (\n ctx: CommandContext<TStore>,\n params: TParams,\n result: TReturn\n) => void;\n\n// =============================================================================\n// Command Types\n// =============================================================================\n\n/**\n * A command that can be dispatched to modify store state.\n * Regular commands do not support undo/redo.\n */\nexport interface Command<TStore, TParams, TReturn> {\n readonly [COMMAND_SYMBOL]: true;\n readonly fn: CommandFn<TStore, TParams, TReturn>;\n}\n\n/**\n * An undoable command that supports undo/redo.\n * Must provide a revert function that knows how to undo the change.\n */\nexport interface UndoableCommand<TStore, TParams, TReturn>\n extends Command<TStore, TParams, TReturn> {\n readonly [UNDOABLE_COMMAND_SYMBOL]: true;\n readonly revert: RevertFn<TStore, TParams, TReturn>;\n}\n\n/**\n * Any command type (regular or undoable).\n */\nexport type AnyCommand = Command<any, any, any>;\n\n/**\n * Any undoable command type.\n */\nexport type AnyUndoableCommand = UndoableCommand<any, any, any>;\n\n// =============================================================================\n// Command Dispatch\n// =============================================================================\n\n/**\n * Dispatch function that accepts commands and returns a function to call with params.\n * Returns the result of the command execution.\n *\n * @example\n * const result = dispatch(myCommand)({ param: \"value\" });\n */\nexport type CommandDispatch<TStore> = <TParams, TReturn>(\n command: Command<TStore, TParams, TReturn>\n) => (params: TParams) => TReturn;\n\n// =============================================================================\n// Undo/Redo Stack Types\n// =============================================================================\n\n/**\n * An entry in the undo/redo stack.\n * Contains all information needed to revert or redo a command.\n */\nexport interface UndoEntry<TParams = unknown, TReturn = unknown> {\n /** The undoable command that was executed */\n readonly command: AnyUndoableCommand;\n /** The parameters that were passed to the command */\n readonly params: TParams;\n /** The result returned by the command (passed to revert) */\n readonly result: TReturn;\n /** Timestamp when the command was executed */\n readonly timestamp: number;\n}\n\n/**\n * State slice for undo/redo functionality.\n */\nexport interface CommanderSlice {\n readonly _commander: {\n /** Stack of commands that can be undone */\n readonly undoStack: ReadonlyArray<UndoEntry>;\n /** Stack of commands that can be redone */\n readonly redoStack: ReadonlyArray<UndoEntry>;\n };\n}\n\n// =============================================================================\n// Commander Types\n// =============================================================================\n\n/**\n * Options for creating a commander.\n */\nexport interface CommanderOptions {\n /**\n * Maximum number of undo entries to keep.\n * @default 100\n */\n readonly maxUndoStackSize?: number;\n}\n\n/**\n * A commander instance bound to a specific store type.\n * Used to create commands and the middleware.\n */\nexport interface Commander<TStore> {\n /**\n * Create a regular command (no undo support).\n *\n * @example\n * // With params\n * const addItem = commander.action<{ name: string }>(\n * (ctx, params) => {\n * // modify state using params.name\n * }\n * );\n *\n * // Without params\n * const clearAll = commander.action((ctx) => {\n * // modify state\n * });\n */\n readonly action: {\n // With params (explicit type parameter)\n <TParams, TReturn = void>(\n fn: CommandFn<TStore, TParams, TReturn>\n ): Command<TStore, TParams, TReturn>;\n\n // Without params (void) - inferred when no type param provided\n <TReturn = void>(\n fn: CommandFn<TStore, void, TReturn>\n ): Command<TStore, void, TReturn>;\n };\n\n /**\n * Create an undoable command with undo/redo support.\n * The revert function is called when undoing the command.\n *\n * @example\n * const moveItem = commander.undoableAction<{ id: string; toIndex: number }, { fromIndex: number }>(\n * (ctx, params) => {\n * const fromIndex = // get current index\n * // perform move\n * return { fromIndex }; // return data needed for revert\n * },\n * (ctx, params, result) => {\n * // revert: move back to original position\n * ctx.dispatch(moveItem)({ id: params.id, toIndex: result.fromIndex });\n * }\n * );\n */\n readonly undoableAction: {\n // With params (explicit type parameter)\n <TParams, TReturn>(\n fn: CommandFn<TStore, TParams, TReturn>,\n revert: RevertFn<TStore, TParams, TReturn>\n ): UndoableCommand<TStore, TParams, TReturn>;\n\n // Without params (void)\n <TReturn>(\n fn: CommandFn<TStore, void, TReturn>,\n revert: RevertFn<TStore, void, TReturn>\n ): UndoableCommand<TStore, void, TReturn>;\n };\n\n /**\n * Zustand middleware that adds commander functionality.\n * Adds undo/redo stacks to the store state.\n */\n readonly middleware: CommanderMiddleware<TStore>;\n}\n\n/**\n * Type for the commander middleware.\n * Note: TStore is intentionally unused here to match the Commander interface signature.\n * The middleware is generic over T (the inner store type) and adds CommanderSlice.\n */\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nexport type CommanderMiddleware<_TStore> = <T extends object>(\n config: (\n set: StoreApi<T & CommanderSlice>[\"setState\"],\n get: StoreApi<T & CommanderSlice>[\"getState\"],\n api: StoreApi<T & CommanderSlice>\n ) => T\n) => (\n set: StoreApi<T & CommanderSlice>[\"setState\"],\n get: StoreApi<T & CommanderSlice>[\"getState\"],\n api: StoreApi<T & CommanderSlice>\n) => T & CommanderSlice;\n\n// =============================================================================\n// Type Helpers\n// =============================================================================\n\n/**\n * Extract the params type from a command.\n */\nexport type CommandParams<T> = T extends Command<any, infer P, any> ? P : never;\n\n/**\n * Extract the return type from a command.\n */\nexport type CommandReturn<T> = T extends Command<any, any, infer R>\n ? R\n : undefined;\n\n/**\n * Extract the store type from a command.\n */\nexport type CommandStore<T> = T extends Command<infer S, any, any> ? S : never;\n\n// =============================================================================\n// Type Guards\n// =============================================================================\n\n/**\n * Type guard to check if a value is a Command.\n */\nexport function isCommand(value: unknown): value is AnyCommand {\n return (\n typeof value === \"object\" &&\n value !== null &&\n COMMAND_SYMBOL in value &&\n value[COMMAND_SYMBOL] === true\n );\n}\n\n/**\n * Type guard to check if a command is undoable.\n */\nexport function isUndoableCommand(\n value: unknown\n): value is AnyUndoableCommand {\n return (\n isCommand(value) &&\n UNDOABLE_COMMAND_SYMBOL in value &&\n value[UNDOABLE_COMMAND_SYMBOL] === true\n );\n}\n\n// =============================================================================\n// Store Type Helper\n// =============================================================================\n\n/**\n * Helper type to extract the state type from a zustand store.\n */\nexport type ExtractState<TStore> = TStore extends UseBoundStore<\n StoreApi<infer S>\n>\n ? S\n : TStore extends StoreApi<infer S>\n ? S\n : never;\n\n","/**\n * @voidhash/mimic-react/zustand-commander\n *\n * Commander creation and command definition.\n *\n * @since 0.0.1\n */\n\nimport type { StoreApi } from \"zustand\";\nimport {\n COMMAND_SYMBOL,\n UNDOABLE_COMMAND_SYMBOL,\n isUndoableCommand,\n type Command,\n type Commander,\n type CommanderOptions,\n type CommanderSlice,\n type CommandContext,\n type CommandDispatch,\n type CommandFn,\n type RevertFn,\n type UndoableCommand,\n type UndoEntry,\n} from \"./types.js\";\n\n// =============================================================================\n// Default Options\n// =============================================================================\n\nconst DEFAULT_OPTIONS: Required<CommanderOptions> = {\n maxUndoStackSize: 100,\n};\n\n// =============================================================================\n// Commander Implementation\n// =============================================================================\n\n/**\n * Creates a commander instance bound to a specific store type.\n *\n * @example\n * ```ts\n * // Create commander for your store type\n * const commander = createCommander<StoreState>();\n *\n * // Define commands\n * const addItem = commander.action(\n * Schema.Struct({ name: Schema.String }),\n * (ctx, params) => {\n * const { mimic } = ctx.getState();\n * mimic.document.transaction(root => {\n * // add item\n * });\n * }\n * );\n *\n * // Create store with middleware\n * const useStore = create(\n * commander.middleware(\n * mimic(document, (set, get) => ({\n * // your state\n * }))\n * )\n * );\n * ```\n */\nexport function createCommander<TStore extends object>(\n options: CommanderOptions = {}\n): Commander<TStore & CommanderSlice> {\n const { maxUndoStackSize } = { ...DEFAULT_OPTIONS, ...options };\n\n // Track the store API once middleware is applied\n let _storeApi: StoreApi<TStore & CommanderSlice> | null = null;\n\n /**\n * Creates the dispatch function for use within command handlers.\n */\n const createDispatch = (): CommandDispatch<TStore & CommanderSlice> => {\n return <TParams, TReturn>(\n command: Command<TStore & CommanderSlice, TParams, TReturn>\n ) => {\n return (params: TParams): TReturn => {\n if (!_storeApi) {\n throw new Error(\n \"Commander: Store not initialized. Make sure to use the commander middleware.\"\n );\n }\n\n // Create context for the command\n const ctx: CommandContext<TStore & CommanderSlice> = {\n getState: () => _storeApi!.getState(),\n setState: (partial) => _storeApi!.setState(partial as any),\n dispatch: createDispatch(),\n };\n\n // Execute the command\n const result = command.fn(ctx, params);\n\n // If it's an undoable command, add to undo stack\n if (isUndoableCommand(command)) {\n const entry: UndoEntry<TParams, TReturn> = {\n command,\n params,\n result,\n timestamp: Date.now(),\n };\n\n _storeApi.setState((state: TStore & CommanderSlice) => {\n const { undoStack, redoStack } = state._commander;\n\n // Add to undo stack, respecting max size\n const newUndoStack = [...undoStack, entry].slice(-maxUndoStackSize);\n\n // Clear redo stack when a new command is executed\n return {\n ...state,\n _commander: {\n undoStack: newUndoStack,\n redoStack: [],\n },\n };\n });\n }\n\n return result;\n };\n };\n };\n\n /**\n * Create a regular command (no undo support).\n */\n function action<TParams, TReturn = void>(\n fn: CommandFn<TStore & CommanderSlice, TParams, TReturn>\n ): Command<TStore & CommanderSlice, TParams, TReturn> {\n return {\n [COMMAND_SYMBOL]: true,\n fn,\n };\n }\n\n /**\n * Create an undoable command with undo/redo support.\n */\n function undoableAction<TParams, TReturn>(\n fn: CommandFn<TStore & CommanderSlice, TParams, TReturn>,\n revert: RevertFn<TStore & CommanderSlice, TParams, TReturn>\n ): UndoableCommand<TStore & CommanderSlice, TParams, TReturn> {\n return {\n [COMMAND_SYMBOL]: true,\n [UNDOABLE_COMMAND_SYMBOL]: true,\n fn,\n revert,\n };\n }\n\n /**\n * Zustand middleware that adds commander functionality.\n */\n const middleware = <T extends object>(\n config: (\n set: StoreApi<T & CommanderSlice>[\"setState\"],\n get: StoreApi<T & CommanderSlice>[\"getState\"],\n api: StoreApi<T & CommanderSlice>\n ) => T\n ) => {\n return (\n set: StoreApi<T & CommanderSlice>[\"setState\"],\n get: StoreApi<T & CommanderSlice>[\"getState\"],\n api: StoreApi<T & CommanderSlice>\n ): T & CommanderSlice => {\n // Store the API reference for dispatch\n _storeApi = api as unknown as StoreApi<TStore & CommanderSlice>;\n\n // Get user's state\n const userState = config(set, get, api);\n\n // Add commander slice\n return {\n ...userState,\n _commander: {\n undoStack: [],\n redoStack: [],\n },\n };\n };\n };\n\n return {\n action,\n undoableAction,\n middleware: middleware as Commander<TStore & CommanderSlice>[\"middleware\"],\n };\n}\n\n// =============================================================================\n// Undo/Redo Functions\n// =============================================================================\n\n/**\n * Perform an undo operation on the store.\n * Returns true if an undo was performed, false if undo stack was empty.\n */\nexport function performUndo<TStore extends CommanderSlice>(\n storeApi: StoreApi<TStore>\n): boolean {\n const state = storeApi.getState();\n const { undoStack, redoStack } = state._commander;\n\n // Pop the last entry from undo stack\n const entry = undoStack[undoStack.length - 1];\n if (!entry) {\n return false;\n }\n\n const newUndoStack = undoStack.slice(0, -1);\n\n // Create context for the revert function\n const ctx: CommandContext<TStore> = {\n getState: () => storeApi.getState(),\n setState: (partial) => storeApi.setState(partial as any),\n dispatch: createDispatchForUndo(storeApi),\n };\n\n // Execute the revert function\n entry.command.revert(ctx, entry.params, entry.result);\n\n // Move entry to redo stack\n storeApi.setState((state: TStore) => ({\n ...state,\n _commander: {\n undoStack: newUndoStack,\n redoStack: [...redoStack, entry],\n },\n }));\n\n return true;\n}\n\n/**\n * Perform a redo operation on the store.\n * Returns true if a redo was performed, false if redo stack was empty.\n */\nexport function performRedo<TStore extends CommanderSlice>(\n storeApi: StoreApi<TStore>\n): boolean {\n const state = storeApi.getState();\n const { undoStack, redoStack } = state._commander;\n\n // Pop the last entry from redo stack\n const entry = redoStack[redoStack.length - 1];\n if (!entry) {\n return false;\n }\n\n const newRedoStack = redoStack.slice(0, -1);\n\n // Create context for re-executing the command\n const ctx: CommandContext<TStore> = {\n getState: () => storeApi.getState(),\n setState: (partial) => storeApi.setState(partial as any),\n dispatch: createDispatchForUndo(storeApi),\n };\n\n // Re-execute the command\n const result = entry.command.fn(ctx, entry.params);\n\n // Create new entry with potentially new result\n const newEntry: UndoEntry = {\n command: entry.command,\n params: entry.params,\n result,\n timestamp: Date.now(),\n };\n\n // Move entry back to undo stack\n storeApi.setState((state: TStore) => ({\n ...state,\n _commander: {\n undoStack: [...undoStack, newEntry],\n redoStack: newRedoStack,\n },\n }));\n\n return true;\n}\n\n/**\n * Creates a dispatch function for use during undo/redo operations.\n * This dispatch does NOT add to undo stack (to avoid infinite loops).\n */\nfunction createDispatchForUndo<TStore>(\n storeApi: StoreApi<TStore>\n): CommandDispatch<TStore> {\n return <TParams, TReturn>(command: Command<TStore, TParams, TReturn>) => {\n return (params: TParams): TReturn => {\n const ctx: CommandContext<TStore> = {\n getState: () => storeApi.getState(),\n setState: (partial) => storeApi.setState(partial as any),\n dispatch: createDispatchForUndo(storeApi),\n };\n\n // Execute without adding to undo stack\n return command.fn(ctx, params);\n };\n };\n}\n\n/**\n * Clear the undo and redo stacks.\n */\nexport function clearUndoHistory<TStore extends CommanderSlice>(\n storeApi: StoreApi<TStore>\n): void {\n storeApi.setState((state: TStore) => ({\n ...state,\n _commander: {\n undoStack: [],\n redoStack: [],\n },\n }));\n}\n\n","/**\n * @voidhash/mimic-react/zustand-commander\n *\n * React hooks for zustand-commander.\n *\n * @since 0.0.1\n */\n\nimport { useCallback, useEffect, useMemo } from \"react\";\nimport { useStore, type StoreApi, type UseBoundStore } from \"zustand\";\nimport { performRedo, performUndo, clearUndoHistory } from \"./commander\";\nimport {\n isUndoableCommand,\n type Command,\n type CommandContext,\n type CommandDispatch,\n type CommanderSlice,\n type ExtractState,\n} from \"./types.js\";\n\n// =============================================================================\n// useCommander Hook\n// =============================================================================\n\n/**\n * Creates a dispatch function for commands.\n * This is for use outside of React components (e.g., in command handlers).\n */\nfunction createDispatchFromApi<TStore extends CommanderSlice>(\n storeApi: StoreApi<TStore>,\n maxUndoStackSize = 100\n): CommandDispatch<TStore> {\n const dispatch: CommandDispatch<TStore> = <TParams, TReturn>(\n command: Command<TStore, TParams, TReturn>\n ) => {\n return (params: TParams): TReturn => {\n // Create context for the command\n const ctx: CommandContext<TStore> = {\n getState: () => storeApi.getState(),\n setState: (partial) => storeApi.setState(partial as Partial<TStore>),\n dispatch,\n };\n\n // Execute the command\n const result = command.fn(ctx, params);\n\n // If it's an undoable command, add to undo stack\n if (isUndoableCommand(command)) {\n storeApi.setState((state: TStore) => {\n const { undoStack } = state._commander;\n\n // Add to undo stack, respecting max size\n const newUndoStack = [\n ...undoStack,\n {\n command,\n params,\n result,\n timestamp: Date.now(),\n },\n ].slice(-maxUndoStackSize);\n\n // Clear redo stack when a new command is executed\n return {\n ...state,\n _commander: {\n undoStack: newUndoStack,\n redoStack: [],\n },\n } as TStore;\n });\n }\n\n return result;\n };\n };\n\n return dispatch;\n}\n\n/**\n * React hook to get a dispatch function for commands.\n * The dispatch function executes commands and manages undo/redo state.\n *\n * @example\n * ```tsx\n * const dispatch = useCommander(useStore);\n *\n * const handleClick = () => {\n * dispatch(addCard)({ columnId: \"col-1\", title: \"New Card\" });\n * };\n * ```\n */\nexport function useCommander<TStore extends CommanderSlice>(\n store: UseBoundStore<StoreApi<TStore>>\n): CommandDispatch<TStore> {\n // Get the store API\n const storeApi = useMemo(() => {\n // UseBoundStore has the StoreApi attached\n return store as unknown as StoreApi<TStore>;\n }, [store]);\n\n // Create a stable dispatch function\n const dispatch = useMemo(\n () => createDispatchFromApi<TStore>(storeApi),\n [storeApi]\n );\n\n return dispatch as CommandDispatch<TStore>;\n}\n\n// =============================================================================\n// useUndoRedo Hook\n// =============================================================================\n\n/**\n * State and actions for undo/redo functionality.\n */\nexport interface UndoRedoState {\n /** Whether there are actions that can be undone */\n readonly canUndo: boolean;\n /** Whether there are actions that can be redone */\n readonly canRedo: boolean;\n /** Number of items in the undo stack */\n readonly undoCount: number;\n /** Number of items in the redo stack */\n readonly redoCount: number;\n /** Undo the last action */\n readonly undo: () => boolean;\n /** Redo the last undone action */\n readonly redo: () => boolean;\n /** Clear the undo/redo history */\n readonly clear: () => void;\n}\n\n/**\n * React hook for undo/redo functionality.\n * Provides state (canUndo, canRedo) and actions (undo, redo, clear).\n *\n * @example\n * ```tsx\n * const { canUndo, canRedo, undo, redo } = useUndoRedo(useStore);\n *\n * return (\n * <>\n * <button onClick={undo} disabled={!canUndo}>Undo</button>\n * <button onClick={redo} disabled={!canRedo}>Redo</button>\n * </>\n * );\n * ```\n */\nexport function useUndoRedo<TStore extends CommanderSlice>(\n store: UseBoundStore<StoreApi<TStore>>\n): UndoRedoState {\n // Get the store API\n const storeApi = useMemo(() => {\n return store as unknown as StoreApi<TStore>;\n }, [store]);\n\n // Subscribe to commander state\n const commanderState = useStore(\n store,\n (state: TStore) => state._commander\n );\n\n const canUndo = commanderState.undoStack.length > 0;\n const canRedo = commanderState.redoStack.length > 0;\n const undoCount = commanderState.undoStack.length;\n const redoCount = commanderState.redoStack.length;\n\n const undo = useCallback(() => {\n return performUndo(storeApi);\n }, [storeApi]);\n\n const redo = useCallback(() => {\n return performRedo(storeApi);\n }, [storeApi]);\n\n const clear = useCallback(() => {\n clearUndoHistory(storeApi);\n }, [storeApi]);\n\n return {\n canUndo,\n canRedo,\n undoCount,\n redoCount,\n undo,\n redo,\n clear,\n };\n}\n\n// =============================================================================\n// Keyboard Shortcut Hook\n// =============================================================================\n\n/**\n * Options for the keyboard shortcut hook.\n */\nexport interface UseUndoRedoKeyboardOptions {\n /** Enable Ctrl/Cmd+Z for undo (default: true) */\n readonly enableUndo?: boolean;\n /** Enable Ctrl/Cmd+Shift+Z or Ctrl+Y for redo (default: true) */\n readonly enableRedo?: boolean;\n}\n\n/**\n * React hook that adds keyboard shortcuts for undo/redo.\n * Listens for Ctrl/Cmd+Z (undo) and Ctrl/Cmd+Shift+Z or Ctrl+Y (redo).\n *\n * @example\n * ```tsx\n * // In your app component\n * useUndoRedoKeyboard(useStore);\n * ```\n */\nexport function useUndoRedoKeyboard<TStore extends CommanderSlice>(\n store: UseBoundStore<StoreApi<TStore>>,\n options: UseUndoRedoKeyboardOptions = {}\n): void {\n const { enableUndo = true, enableRedo = true } = options;\n\n const storeApi = useMemo(() => {\n return store as unknown as StoreApi<TStore>;\n }, [store]);\n\n // Set up keyboard listener\n useEffect(() => {\n if (typeof window === \"undefined\") {\n return;\n }\n\n const handleKeyDown = (event: KeyboardEvent) => {\n const isMac = navigator.platform.toUpperCase().indexOf(\"MAC\") >= 0;\n const modKey = isMac ? event.metaKey : event.ctrlKey;\n\n if (!modKey) return;\n\n // Undo: Ctrl/Cmd + Z (without Shift)\n if (enableUndo && event.key === \"z\" && !event.shiftKey) {\n event.preventDefault();\n performUndo(storeApi);\n return;\n }\n\n // Redo: Ctrl/Cmd + Shift + Z or Ctrl + Y\n if (enableRedo) {\n if ((event.key === \"z\" && event.shiftKey) || event.key === \"y\") {\n event.preventDefault();\n performRedo(storeApi);\n }\n }\n };\n\n window.addEventListener(\"keydown\", handleKeyDown);\n return () => window.removeEventListener(\"keydown\", handleKeyDown);\n }, [storeApi, enableUndo, enableRedo]);\n}\n"],"mappings":";;;;;;;;AAiBA,MAAa,iBAAiB,OAAO,IAAI,4BAA4B;;;;AAKrE,MAAa,0BAA0B,OAAO,IAC5C,qCACD;;;;AAwQD,SAAgB,UAAU,OAAqC;AAC7D,QACE,OAAO,UAAU,YACjB,UAAU,QACV,kBAAkB,SAClB,MAAM,oBAAoB;;;;;AAO9B,SAAgB,kBACd,OAC6B;AAC7B,QACE,UAAU,MAAM,IAChB,2BAA2B,SAC3B,MAAM,6BAA6B;;;;;ACrRvC,MAAMA,kBAA8C,EAClD,kBAAkB,KACnB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCD,SAAgB,gBACd,UAA4B,EAAE,EACM;CACpC,MAAM,EAAE,uDAA0B,kBAAoB;CAGtD,IAAIC,YAAsD;;;;CAK1D,MAAM,uBAAiE;AACrE,UACE,YACG;AACH,WAAQ,WAA6B;AACnC,QAAI,CAAC,UACH,OAAM,IAAI,MACR,+EACD;IAIH,MAAMC,MAA+C;KACnD,gBAAgB,UAAW,UAAU;KACrC,WAAW,YAAY,UAAW,SAAS,QAAe;KAC1D,UAAU,gBAAgB;KAC3B;IAGD,MAAM,SAAS,QAAQ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,MAAM,YAAa,MAAM,QAAQ,KAAK;AAC9D,WAAM,gBAAgB;AACtB,iBAAY,SAAS;;;;AAK3B,SAAO,iBAAiB,WAAW,cAAc;AACjD,eAAa,OAAO,oBAAoB,WAAW,cAAc;IAChE;EAAC;EAAU;EAAY;EAAW,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@voidhash/mimic-react",
|
|
3
|
-
"version": "0.0.1-alpha.
|
|
3
|
+
"version": "0.0.1-alpha.111",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -37,12 +37,12 @@
|
|
|
37
37
|
"typescript": "5.8.3",
|
|
38
38
|
"vite-tsconfig-paths": "^5.1.4",
|
|
39
39
|
"vitest": "^3.2.4",
|
|
40
|
-
"@voidhash/tsconfig": "0.0.1-alpha.
|
|
40
|
+
"@voidhash/tsconfig": "0.0.1-alpha.111"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
43
|
"effect": "^3.16.0",
|
|
44
44
|
"react": "^18.0.0 || ^19.0.0",
|
|
45
|
-
"@voidhash/mimic": "0.0.1-alpha.
|
|
45
|
+
"@voidhash/mimic": "0.0.1-alpha.111"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
48
|
"build": "tsdown",
|
|
@@ -11,7 +11,6 @@ import {
|
|
|
11
11
|
COMMAND_SYMBOL,
|
|
12
12
|
UNDOABLE_COMMAND_SYMBOL,
|
|
13
13
|
isUndoableCommand,
|
|
14
|
-
type AnyEffectSchema,
|
|
15
14
|
type Command,
|
|
16
15
|
type Commander,
|
|
17
16
|
type CommanderOptions,
|
|
@@ -19,7 +18,6 @@ import {
|
|
|
19
18
|
type CommandContext,
|
|
20
19
|
type CommandDispatch,
|
|
21
20
|
type CommandFn,
|
|
22
|
-
type InferSchemaType,
|
|
23
21
|
type RevertFn,
|
|
24
22
|
type UndoableCommand,
|
|
25
23
|
type UndoEntry,
|
|
@@ -132,97 +130,27 @@ export function createCommander<TStore extends object>(
|
|
|
132
130
|
/**
|
|
133
131
|
* Create a regular command (no undo support).
|
|
134
132
|
*/
|
|
135
|
-
function action<
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
): Command<TStore & CommanderSlice, InferSchemaType<TParamsSchema>, TReturn>;
|
|
139
|
-
function action<TReturn = void>(
|
|
140
|
-
fn: CommandFn<TStore & CommanderSlice, void, TReturn>
|
|
141
|
-
): Command<TStore & CommanderSlice, void, TReturn>;
|
|
142
|
-
function action<TParamsSchema extends AnyEffectSchema, TReturn = void>(
|
|
143
|
-
paramsSchemaOrFn:
|
|
144
|
-
| TParamsSchema
|
|
145
|
-
| CommandFn<TStore & CommanderSlice, void, TReturn>,
|
|
146
|
-
maybeFn?: CommandFn<
|
|
147
|
-
TStore & CommanderSlice,
|
|
148
|
-
InferSchemaType<TParamsSchema>,
|
|
149
|
-
TReturn
|
|
150
|
-
>
|
|
151
|
-
): Command<TStore & CommanderSlice, any, TReturn> {
|
|
152
|
-
// Check if we have two arguments (schema + fn) or just one (fn only)
|
|
153
|
-
if (maybeFn !== undefined) {
|
|
154
|
-
// First arg is schema, second is fn
|
|
155
|
-
return {
|
|
156
|
-
[COMMAND_SYMBOL]: true,
|
|
157
|
-
fn: maybeFn,
|
|
158
|
-
paramsSchema: paramsSchemaOrFn as TParamsSchema,
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// Single argument - must be the action function (no params schema)
|
|
163
|
-
if (typeof paramsSchemaOrFn !== "function") {
|
|
164
|
-
throw new Error("Commander: action requires a function");
|
|
165
|
-
}
|
|
166
|
-
|
|
133
|
+
function action<TParams, TReturn = void>(
|
|
134
|
+
fn: CommandFn<TStore & CommanderSlice, TParams, TReturn>
|
|
135
|
+
): Command<TStore & CommanderSlice, TParams, TReturn> {
|
|
167
136
|
return {
|
|
168
137
|
[COMMAND_SYMBOL]: true,
|
|
169
|
-
fn
|
|
170
|
-
paramsSchema: null,
|
|
138
|
+
fn,
|
|
171
139
|
};
|
|
172
140
|
}
|
|
173
141
|
|
|
174
142
|
/**
|
|
175
143
|
* Create an undoable command with undo/redo support.
|
|
176
144
|
*/
|
|
177
|
-
function undoableAction<
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
): UndoableCommand<TStore & CommanderSlice, InferSchemaType<TParamsSchema>, TReturn>;
|
|
182
|
-
function undoableAction<TReturn>(
|
|
183
|
-
fn: CommandFn<TStore & CommanderSlice, void, TReturn>,
|
|
184
|
-
revert: RevertFn<TStore & CommanderSlice, void, TReturn>
|
|
185
|
-
): UndoableCommand<TStore & CommanderSlice, void, TReturn>;
|
|
186
|
-
function undoableAction<TParamsSchema extends AnyEffectSchema, TReturn>(
|
|
187
|
-
paramsSchemaOrFn:
|
|
188
|
-
| TParamsSchema
|
|
189
|
-
| CommandFn<TStore & CommanderSlice, void, TReturn>,
|
|
190
|
-
fnOrRevert:
|
|
191
|
-
| CommandFn<TStore & CommanderSlice, InferSchemaType<TParamsSchema>, TReturn>
|
|
192
|
-
| RevertFn<TStore & CommanderSlice, void, TReturn>,
|
|
193
|
-
maybeRevert?: RevertFn<
|
|
194
|
-
TStore & CommanderSlice,
|
|
195
|
-
InferSchemaType<TParamsSchema>,
|
|
196
|
-
TReturn
|
|
197
|
-
>
|
|
198
|
-
): UndoableCommand<TStore & CommanderSlice, any, TReturn> {
|
|
199
|
-
// Check if we have three arguments (schema + fn + revert) or two (fn + revert)
|
|
200
|
-
if (maybeRevert !== undefined) {
|
|
201
|
-
// First arg is schema, second is fn, third is revert
|
|
202
|
-
return {
|
|
203
|
-
[COMMAND_SYMBOL]: true,
|
|
204
|
-
[UNDOABLE_COMMAND_SYMBOL]: true,
|
|
205
|
-
fn: fnOrRevert as CommandFn<
|
|
206
|
-
TStore & CommanderSlice,
|
|
207
|
-
InferSchemaType<TParamsSchema>,
|
|
208
|
-
TReturn
|
|
209
|
-
>,
|
|
210
|
-
paramsSchema: paramsSchemaOrFn as TParamsSchema,
|
|
211
|
-
revert: maybeRevert,
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// Two arguments - fn + revert (no params schema)
|
|
216
|
-
if (typeof paramsSchemaOrFn !== "function") {
|
|
217
|
-
throw new Error("Commander: undoableAction requires a function");
|
|
218
|
-
}
|
|
219
|
-
|
|
145
|
+
function undoableAction<TParams, TReturn>(
|
|
146
|
+
fn: CommandFn<TStore & CommanderSlice, TParams, TReturn>,
|
|
147
|
+
revert: RevertFn<TStore & CommanderSlice, TParams, TReturn>
|
|
148
|
+
): UndoableCommand<TStore & CommanderSlice, TParams, TReturn> {
|
|
220
149
|
return {
|
|
221
150
|
[COMMAND_SYMBOL]: true,
|
|
222
151
|
[UNDOABLE_COMMAND_SYMBOL]: true,
|
|
223
|
-
fn
|
|
224
|
-
|
|
225
|
-
revert: fnOrRevert as RevertFn<TStore & CommanderSlice, void, TReturn>,
|
|
152
|
+
fn,
|
|
153
|
+
revert,
|
|
226
154
|
};
|
|
227
155
|
}
|
|
228
156
|
|
|
@@ -9,22 +9,22 @@
|
|
|
9
9
|
* @example
|
|
10
10
|
* ```ts
|
|
11
11
|
* import { createCommander, useCommander, useUndoRedo } from "@voidhash/mimic-react/zustand-commander";
|
|
12
|
-
* import { Schema } from "effect";
|
|
13
12
|
*
|
|
14
13
|
* // 1. Create commander bound to your store type
|
|
15
14
|
* const commander = createCommander<StoreState>();
|
|
16
15
|
*
|
|
17
16
|
* // 2. Define regular actions
|
|
18
|
-
* const selectCard = commander.action(
|
|
19
|
-
* Schema.Struct({ cardId: Schema.String }),
|
|
17
|
+
* const selectCard = commander.action<{ cardId: string }>(
|
|
20
18
|
* (ctx, params) => {
|
|
21
19
|
* ctx.setState({ selectedCardId: params.cardId });
|
|
22
20
|
* }
|
|
23
21
|
* );
|
|
24
22
|
*
|
|
25
23
|
* // 3. Define undoable actions
|
|
26
|
-
* const moveCard = commander.undoableAction
|
|
27
|
-
*
|
|
24
|
+
* const moveCard = commander.undoableAction<
|
|
25
|
+
* { cardId: string; toColumnId: string },
|
|
26
|
+
* { fromColumnId: string }
|
|
27
|
+
* >(
|
|
28
28
|
* (ctx, params) => {
|
|
29
29
|
* const { mimic } = ctx.getState();
|
|
30
30
|
* const fromColumnId = // get current column
|
|
@@ -99,9 +99,6 @@ export {
|
|
|
99
99
|
// =============================================================================
|
|
100
100
|
|
|
101
101
|
export type {
|
|
102
|
-
// Schema types
|
|
103
|
-
AnyEffectSchema,
|
|
104
|
-
InferSchemaType,
|
|
105
102
|
// Command types
|
|
106
103
|
Command,
|
|
107
104
|
UndoableCommand,
|
|
@@ -6,25 +6,8 @@
|
|
|
6
6
|
* @since 0.0.1
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import type { Schema } from "effect";
|
|
10
9
|
import type { StoreApi, UseBoundStore } from "zustand";
|
|
11
10
|
|
|
12
|
-
// =============================================================================
|
|
13
|
-
// Schema Types
|
|
14
|
-
// =============================================================================
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Any Effect Schema type (used for type constraints).
|
|
18
|
-
*/
|
|
19
|
-
export type AnyEffectSchema = Schema.Schema<any, any, any>;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Infer the Type from an Effect Schema.
|
|
23
|
-
*/
|
|
24
|
-
export type InferSchemaType<T> = T extends Schema.Schema<infer A, any, any>
|
|
25
|
-
? A
|
|
26
|
-
: never;
|
|
27
|
-
|
|
28
11
|
// =============================================================================
|
|
29
12
|
// Command Symbol & Type Guard
|
|
30
13
|
// =============================================================================
|
|
@@ -103,7 +86,6 @@ export type RevertFn<TStore, TParams, TReturn> = (
|
|
|
103
86
|
export interface Command<TStore, TParams, TReturn> {
|
|
104
87
|
readonly [COMMAND_SYMBOL]: true;
|
|
105
88
|
readonly fn: CommandFn<TStore, TParams, TReturn>;
|
|
106
|
-
readonly paramsSchema: AnyEffectSchema | null;
|
|
107
89
|
}
|
|
108
90
|
|
|
109
91
|
/**
|
|
@@ -196,11 +178,10 @@ export interface Commander<TStore> {
|
|
|
196
178
|
* Create a regular command (no undo support).
|
|
197
179
|
*
|
|
198
180
|
* @example
|
|
199
|
-
* // With params
|
|
200
|
-
* const addItem = commander.action(
|
|
201
|
-
* Schema.Struct({ name: Schema.String }),
|
|
181
|
+
* // With params
|
|
182
|
+
* const addItem = commander.action<{ name: string }>(
|
|
202
183
|
* (ctx, params) => {
|
|
203
|
-
* // modify state
|
|
184
|
+
* // modify state using params.name
|
|
204
185
|
* }
|
|
205
186
|
* );
|
|
206
187
|
*
|
|
@@ -210,13 +191,12 @@ export interface Commander<TStore> {
|
|
|
210
191
|
* });
|
|
211
192
|
*/
|
|
212
193
|
readonly action: {
|
|
213
|
-
// With params
|
|
214
|
-
<
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
): Command<TStore, InferSchemaType<TParamsSchema>, TReturn>;
|
|
194
|
+
// With params (explicit type parameter)
|
|
195
|
+
<TParams, TReturn = void>(
|
|
196
|
+
fn: CommandFn<TStore, TParams, TReturn>
|
|
197
|
+
): Command<TStore, TParams, TReturn>;
|
|
218
198
|
|
|
219
|
-
// Without params (void)
|
|
199
|
+
// Without params (void) - inferred when no type param provided
|
|
220
200
|
<TReturn = void>(
|
|
221
201
|
fn: CommandFn<TStore, void, TReturn>
|
|
222
202
|
): Command<TStore, void, TReturn>;
|
|
@@ -227,8 +207,7 @@ export interface Commander<TStore> {
|
|
|
227
207
|
* The revert function is called when undoing the command.
|
|
228
208
|
*
|
|
229
209
|
* @example
|
|
230
|
-
* const moveItem = commander.undoableAction(
|
|
231
|
-
* Schema.Struct({ id: Schema.String, toIndex: Schema.Number }),
|
|
210
|
+
* const moveItem = commander.undoableAction<{ id: string; toIndex: number }, { fromIndex: number }>(
|
|
232
211
|
* (ctx, params) => {
|
|
233
212
|
* const fromIndex = // get current index
|
|
234
213
|
* // perform move
|
|
@@ -241,12 +220,11 @@ export interface Commander<TStore> {
|
|
|
241
220
|
* );
|
|
242
221
|
*/
|
|
243
222
|
readonly undoableAction: {
|
|
244
|
-
// With params
|
|
245
|
-
<
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
): UndoableCommand<TStore, InferSchemaType<TParamsSchema>, TReturn>;
|
|
223
|
+
// With params (explicit type parameter)
|
|
224
|
+
<TParams, TReturn>(
|
|
225
|
+
fn: CommandFn<TStore, TParams, TReturn>,
|
|
226
|
+
revert: RevertFn<TStore, TParams, TReturn>
|
|
227
|
+
): UndoableCommand<TStore, TParams, TReturn>;
|
|
250
228
|
|
|
251
229
|
// Without params (void)
|
|
252
230
|
<TReturn>(
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { describe, it, expect, beforeEach } from "vitest";
|
|
2
2
|
import { createStore, type StoreApi } from "zustand";
|
|
3
|
-
import { Schema } from "effect";
|
|
4
3
|
import {
|
|
5
4
|
createCommander,
|
|
6
5
|
performUndo,
|
|
@@ -12,6 +11,7 @@ import {
|
|
|
12
11
|
UNDOABLE_COMMAND_SYMBOL,
|
|
13
12
|
type CommanderSlice,
|
|
14
13
|
type Command,
|
|
14
|
+
type CommandContext,
|
|
15
15
|
type UndoableCommand,
|
|
16
16
|
} from "../../src/zustand-commander/index.js";
|
|
17
17
|
|
|
@@ -116,9 +116,8 @@ describe("action (regular commands)", () => {
|
|
|
116
116
|
store = createTestStore(commander);
|
|
117
117
|
});
|
|
118
118
|
|
|
119
|
-
it("should create command with
|
|
120
|
-
const setCount = commander.action(
|
|
121
|
-
Schema.Struct({ value: Schema.Number }),
|
|
119
|
+
it("should create command with typed params", () => {
|
|
120
|
+
const setCount = commander.action<{ value: number }>(
|
|
122
121
|
(ctx, params) => {
|
|
123
122
|
ctx.setState({ count: params.value });
|
|
124
123
|
}
|
|
@@ -126,7 +125,6 @@ describe("action (regular commands)", () => {
|
|
|
126
125
|
|
|
127
126
|
expect(setCount).toBeDefined();
|
|
128
127
|
expect(setCount[COMMAND_SYMBOL]).toBe(true);
|
|
129
|
-
expect(setCount.paramsSchema).toBeDefined();
|
|
130
128
|
expect(typeof setCount.fn).toBe("function");
|
|
131
129
|
});
|
|
132
130
|
|
|
@@ -137,13 +135,11 @@ describe("action (regular commands)", () => {
|
|
|
137
135
|
|
|
138
136
|
expect(reset).toBeDefined();
|
|
139
137
|
expect(reset[COMMAND_SYMBOL]).toBe(true);
|
|
140
|
-
expect(reset.paramsSchema).toBeNull();
|
|
141
138
|
expect(typeof reset.fn).toBe("function");
|
|
142
139
|
});
|
|
143
140
|
|
|
144
141
|
it("should execute command and modify state", () => {
|
|
145
|
-
const setCount = commander.action(
|
|
146
|
-
Schema.Struct({ value: Schema.Number }),
|
|
142
|
+
const setCount = commander.action<{ value: number }>(
|
|
147
143
|
(ctx, params) => {
|
|
148
144
|
ctx.setState({ count: params.value });
|
|
149
145
|
}
|
|
@@ -166,8 +162,7 @@ describe("action (regular commands)", () => {
|
|
|
166
162
|
});
|
|
167
163
|
|
|
168
164
|
it("should NOT add to undo stack for regular actions", () => {
|
|
169
|
-
const setCount = commander.action(
|
|
170
|
-
Schema.Struct({ value: Schema.Number }),
|
|
165
|
+
const setCount = commander.action<{ value: number }>(
|
|
171
166
|
(ctx, params) => {
|
|
172
167
|
ctx.setState({ count: params.value });
|
|
173
168
|
}
|
|
@@ -190,8 +185,7 @@ describe("action (regular commands)", () => {
|
|
|
190
185
|
});
|
|
191
186
|
|
|
192
187
|
it("should return value from action", () => {
|
|
193
|
-
const getDoubled = commander.action(
|
|
194
|
-
Schema.Struct({ value: Schema.Number }),
|
|
188
|
+
const getDoubled = commander.action<{ value: number }, number>(
|
|
195
189
|
(_ctx, params) => {
|
|
196
190
|
return params.value * 2;
|
|
197
191
|
}
|
|
@@ -220,9 +214,8 @@ describe("undoableAction", () => {
|
|
|
220
214
|
store = createTestStore(commander);
|
|
221
215
|
});
|
|
222
216
|
|
|
223
|
-
it("should create undoable command with
|
|
224
|
-
const addItem = commander.undoableAction(
|
|
225
|
-
Schema.Struct({ item: Schema.String }),
|
|
217
|
+
it("should create undoable command with typed params", () => {
|
|
218
|
+
const addItem = commander.undoableAction<{ item: string }, { index: number }>(
|
|
226
219
|
(ctx, params) => {
|
|
227
220
|
const items = [...ctx.getState().items, params.item];
|
|
228
221
|
ctx.setState({ items });
|
|
@@ -237,13 +230,12 @@ describe("undoableAction", () => {
|
|
|
237
230
|
expect(addItem).toBeDefined();
|
|
238
231
|
expect(addItem[COMMAND_SYMBOL]).toBe(true);
|
|
239
232
|
expect(addItem[UNDOABLE_COMMAND_SYMBOL]).toBe(true);
|
|
240
|
-
expect(addItem.paramsSchema).toBeDefined();
|
|
241
233
|
expect(typeof addItem.fn).toBe("function");
|
|
242
234
|
expect(typeof addItem.revert).toBe("function");
|
|
243
235
|
});
|
|
244
236
|
|
|
245
237
|
it("should create undoable command without params", () => {
|
|
246
|
-
const increment = commander.undoableAction(
|
|
238
|
+
const increment = commander.undoableAction<void, { previousCount: number }>(
|
|
247
239
|
(ctx) => {
|
|
248
240
|
const current = ctx.getState().count;
|
|
249
241
|
ctx.setState({ count: current + 1 });
|
|
@@ -257,12 +249,10 @@ describe("undoableAction", () => {
|
|
|
257
249
|
expect(increment).toBeDefined();
|
|
258
250
|
expect(increment[COMMAND_SYMBOL]).toBe(true);
|
|
259
251
|
expect(increment[UNDOABLE_COMMAND_SYMBOL]).toBe(true);
|
|
260
|
-
expect(increment.paramsSchema).toBeNull();
|
|
261
252
|
});
|
|
262
253
|
|
|
263
254
|
it("should execute undoable action and modify state", () => {
|
|
264
|
-
const setCount = commander.undoableAction(
|
|
265
|
-
Schema.Struct({ value: Schema.Number }),
|
|
255
|
+
const setCount = commander.undoableAction<{ value: number }, { previousValue: number }>(
|
|
266
256
|
(ctx, params) => {
|
|
267
257
|
const prev = ctx.getState().count;
|
|
268
258
|
ctx.setState({ count: params.value });
|
|
@@ -644,16 +634,14 @@ describe("Command Dispatch in Context", () => {
|
|
|
644
634
|
const commander = createCommander<TestState>();
|
|
645
635
|
const store = createTestStore(commander);
|
|
646
636
|
|
|
647
|
-
const addItem = commander.action(
|
|
648
|
-
Schema.Struct({ item: Schema.String }),
|
|
637
|
+
const addItem = commander.action<{ item: string }>(
|
|
649
638
|
(ctx, params) => {
|
|
650
639
|
const items = [...ctx.getState().items, params.item];
|
|
651
640
|
ctx.setState({ items });
|
|
652
641
|
}
|
|
653
642
|
);
|
|
654
643
|
|
|
655
|
-
const addMultiple = commander.action(
|
|
656
|
-
Schema.Struct({ items: Schema.Array(Schema.String) }),
|
|
644
|
+
const addMultiple = commander.action<{ items: string[] }>(
|
|
657
645
|
(ctx, params) => {
|
|
658
646
|
for (const item of params.items) {
|
|
659
647
|
ctx.dispatch(addItem)({ item });
|
|
@@ -664,7 +652,7 @@ describe("Command Dispatch in Context", () => {
|
|
|
664
652
|
const storeApi = store as unknown as StoreApi<TestStore>;
|
|
665
653
|
|
|
666
654
|
// Create a proper dispatch function
|
|
667
|
-
const createCtx = ():
|
|
655
|
+
const createCtx = (): CommandContext<TestStore> => ({
|
|
668
656
|
getState: () => storeApi.getState(),
|
|
669
657
|
setState: (partial: Partial<TestStore>) => storeApi.setState(partial),
|
|
670
658
|
dispatch: <TParams, TReturn>(cmd: Command<TestStore, TParams, TReturn>) => {
|
|
@@ -672,7 +660,7 @@ describe("Command Dispatch in Context", () => {
|
|
|
672
660
|
},
|
|
673
661
|
});
|
|
674
662
|
|
|
675
|
-
const ctx = createCtx();
|
|
663
|
+
const ctx: CommandContext<TestStore> = createCtx();
|
|
676
664
|
|
|
677
665
|
addMultiple.fn(ctx, { items: ["x", "y", "z"] });
|
|
678
666
|
|
|
@@ -685,8 +673,7 @@ describe("Undo/Redo Integration", () => {
|
|
|
685
673
|
const commander = createCommander<TestState>();
|
|
686
674
|
const store = createTestStore(commander);
|
|
687
675
|
|
|
688
|
-
const setCount = commander.undoableAction(
|
|
689
|
-
Schema.Struct({ value: Schema.Number }),
|
|
676
|
+
const setCount = commander.undoableAction<{ value: number }, { previousValue: number }>(
|
|
690
677
|
(ctx, params) => {
|
|
691
678
|
const prev = ctx.getState().count;
|
|
692
679
|
ctx.setState({ count: params.value });
|