valtio-history 0.1.0 → 0.2.1

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/README.md CHANGED
@@ -6,6 +6,8 @@ valtio utility for creating a proxy state with history tracking
6
6
 
7
7
  https://valtio.pmnd.rs/docs/api/utils/proxyWithHistory
8
8
 
9
+ see detailed [api docs](https://github.com/valtiojs/valtio-history/blob/main/packages/history-utility/docs/modules.md) for more info.
10
+
9
11
  ---
10
12
 
11
13
  ## Migrating from `valtio/utils`
@@ -50,3 +52,4 @@ export default function App() {
50
52
  - the `history` object has changes
51
53
  - `history.snapshots` is renamed to `history.nodes`
52
54
  - a `HistoryNode` has the structure `{ snapshot: Snapshot<T>; createdAt: Date; updatedAt?: Date; }`
55
+ - The second parameter of `proxyWithHistory` is now an object instead of a `boolean`. `{ skipSubscribe?: boolean }`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "valtio-history",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "author": "Daishi Kato",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,7 +21,8 @@
21
21
  "peerDependencies": {
22
22
  "valtio": ">=1.0.0"
23
23
  },
24
- "type": "commonjs",
24
+ "type": "module",
25
25
  "main": "./src/index.js",
26
- "typings": "./src/index.d.ts"
26
+ "typings": "./src/index.d.ts",
27
+ "module": "./src/index.js"
27
28
  }
package/src/index.d.ts CHANGED
@@ -1 +1,200 @@
1
- export * from './history-utility';
1
+ import type { INTERNAL_Snapshot as Snapshot } from 'valtio/vanilla';
2
+ export type HistoryNode<T> = {
3
+ /**
4
+ * The snapshot being tracked
5
+ */
6
+ snapshot: Snapshot<T>;
7
+ /**
8
+ * The date when the node was created
9
+ */
10
+ createdAt: Date;
11
+ /**
12
+ * The date when the node was updated. Will be undefined if
13
+ * the node was never updated.
14
+ */
15
+ updatedAt?: Date;
16
+ };
17
+ export type History<T> = {
18
+ /**
19
+ * field for holding sandbox changes; used to avoid infinite loops
20
+ */
21
+ wip?: Snapshot<T>;
22
+ /**
23
+ * the nodes of the history for each change
24
+ */
25
+ nodes: HistoryNode<T>[];
26
+ /**
27
+ * the history index of the current snapshot
28
+ */
29
+ index: number;
30
+ };
31
+ export type HistoryOptions = {
32
+ /**
33
+ * determines if the internal subscribe behaviour should be skipped.
34
+ */
35
+ skipSubscribe?: boolean;
36
+ };
37
+ /**
38
+ * This creates a new proxy with history support (ProxyHistoryObject).
39
+ * It includes following main properties:<br>
40
+ * - value: any value (does not have to be an object)<br>
41
+ * - history: an object holding the history of snapshots and other metadata<br>
42
+ * - history.index: the history index of the current snapshot<br>
43
+ * - history.nodes: the nodes of the history for each change<br>
44
+ * - history.wip: field for holding sandbox changes; used to avoid infinite loops<br>
45
+ * - canUndo: a function to return true if undo is available <br>
46
+ * - undo: a function to go back history <br>
47
+ * - canRedo: a function to return true if redo is available <br>
48
+ * - redo: a function to go forward history <br>
49
+ * - saveHistory: a function to save history <br>
50
+ * - getCurrentChangeDate: gets the date of the current change <br>
51
+ * - remove: a function to remove a specified history index <br>
52
+ * - replace: a function to replace a snapshot at a specified history index <br>
53
+ * - getNode: a function to get the node at a specified history index <br>
54
+ *
55
+ * <br>
56
+ * Notes: <br>
57
+ * - Suspense/promise is not supported. <br>
58
+ *
59
+ * @param initialValue - any value to be tracked
60
+ * @param options - use to configure the proxyWithHistory utility.
61
+ * @returns proxyObject
62
+ *
63
+ * @example
64
+ * import { proxyWithHistory } from 'valtio-history'
65
+ * const state = proxyWithHistory({
66
+ * count: 1,
67
+ * })
68
+ */
69
+ export declare function proxyWithHistory<V>(initialValue: V, options?: HistoryOptions | boolean): {
70
+ /**
71
+ * any value to be tracked (does not have to be an object)
72
+ */
73
+ value: V;
74
+ /**
75
+ * an object holding the history of snapshots and other metadata <br>
76
+ * - history.index: the history index to the current snapshot <br>
77
+ * - history.nodes: the nodes of the history for each change <br>
78
+ * - history.wip: field for holding sandbox changes; used to avoid infinite loops<br>
79
+ */
80
+ history: History<V> & {
81
+ $$valtioRef: true;
82
+ };
83
+ /**
84
+ * get the date when a node was entered into history.
85
+ *
86
+ * @returns date
87
+ */
88
+ getCurrentChangeDate: () => Date | undefined;
89
+ /**
90
+ * utility method to get a history node.
91
+ * The snapshot within this node is already cloned and
92
+ * will not affect the original value if updated.
93
+ *
94
+ * @param index
95
+ * @returns historyNode
96
+ */
97
+ getNode: (index: number) => {
98
+ snapshot: V extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
99
+ $$valtioRef: true;
100
+ } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? V : V extends Promise<unknown> ? Awaited<V> : V extends object ? { readonly [K in keyof V]: V[K] extends infer T ? T extends V[K] ? T extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
101
+ $$valtioRef: true;
102
+ } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T : T extends Promise<unknown> ? Awaited<T> : T extends object ? { readonly [K_1 in keyof T]: T[K_1] extends infer T_1 ? T_1 extends T[K_1] ? T_1 extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
103
+ $$valtioRef: true;
104
+ } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_1 : T_1 extends Promise<unknown> ? Awaited<T_1> : T_1 extends object ? { readonly [K_2 in keyof T_1]: T_1[K_2] extends infer T_2 ? T_2 extends T_1[K_2] ? T_2 extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
105
+ $$valtioRef: true;
106
+ } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_2 : T_2 extends Promise<unknown> ? Awaited<T_2> : T_2 extends object ? { readonly [K_3 in keyof T_2]: T_2[K_3] extends infer T_3 ? T_3 extends T_2[K_3] ? T_3 extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
107
+ $$valtioRef: true;
108
+ } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_3 : T_3 extends Promise<unknown> ? Awaited<T_3> : T_3 extends object ? { readonly [K_4 in keyof T_3]: T_3[K_4] extends infer T_4 ? T_4 extends T_3[K_4] ? T_4 extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
109
+ $$valtioRef: true;
110
+ } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_4 : T_4 extends Promise<unknown> ? Awaited<T_4> : T_4 extends object ? { readonly [K_5 in keyof T_4]: T_4[K_5] extends infer T_5 ? T_5 extends T_4[K_5] ? T_5 extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
111
+ $$valtioRef: true;
112
+ } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_5 : T_5 extends Promise<unknown> ? Awaited<T_5> : T_5 extends object ? { readonly [K_6 in keyof T_5]: T_5[K_6] extends infer T_6 ? T_6 extends T_5[K_6] ? T_6 extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
113
+ $$valtioRef: true;
114
+ } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_6 : T_6 extends Promise<unknown> ? Awaited<T_6> : T_6 extends object ? { readonly [K_7 in keyof T_6]: T_6[K_7] extends infer T_7 ? T_7 extends T_6[K_7] ? T_7 extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
115
+ $$valtioRef: true;
116
+ } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_7 : T_7 extends Promise<unknown> ? Awaited<T_7> : T_7 extends object ? { readonly [K_8 in keyof T_7]: T_7[K_8] extends infer T_8 ? T_8 extends T_7[K_8] ? T_8 extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
117
+ $$valtioRef: true;
118
+ } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_8 : T_8 extends Promise<unknown> ? Awaited<T_8> : T_8 extends object ? { readonly [K_9 in keyof T_8]: T_8[K_9] extends infer T_9 ? T_9 extends T_8[K_9] ? T_9 extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
119
+ $$valtioRef: true;
120
+ } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_9 : T_9 extends Promise<unknown> ? Awaited<T_9> : T_9 extends object ? { readonly [K_10 in keyof T_9]: any; } : T_9 : never : never; } : T_8 : never : never; } : T_7 : never : never; } : T_6 : never : never; } : T_5 : never : never; } : T_4 : never : never; } : T_3 : never : never; } : T_2 : never : never; } : T_1 : never : never; } : T : never : never; } : V;
121
+ /**
122
+ * The date when the node was created
123
+ */
124
+ createdAt: Date;
125
+ /**
126
+ * The date when the node was updated. Will be undefined if
127
+ * the node was never updated.
128
+ */
129
+ updatedAt?: Date | undefined;
130
+ } | undefined;
131
+ /**
132
+ * utility to clone a snapshot
133
+ */
134
+ clone: <T_10>(value: T_10) => T_10;
135
+ /**
136
+ * a function to go to a specific index in history
137
+ */
138
+ goTo: (index: number) => void;
139
+ /**
140
+ * a function to return true if undo is available
141
+ * @returns boolean
142
+ */
143
+ canUndo: () => boolean;
144
+ /**
145
+ * a function to go back in history
146
+ */
147
+ undo: () => void;
148
+ /**
149
+ * a function to return true if redo is available
150
+ * @returns boolean
151
+ */
152
+ canRedo: () => boolean;
153
+ /**
154
+ * a function to go forward in history
155
+ */
156
+ redo: () => void;
157
+ /**
158
+ * a function to execute saving history when changes are made to `value`
159
+ */
160
+ saveHistory: () => void;
161
+ /**
162
+ * a function that returns true when the history should be updated
163
+ *
164
+ * @param ops - subscribeOps from subscribe callback
165
+ * @returns boolean
166
+ */
167
+ shouldSaveHistory: (ops: ([op: "set", path: (string | symbol)[], value: unknown, prevValue: unknown] | [op: "delete", path: (string | symbol)[], prevValue: unknown] | [op: "resolve", path: (string | symbol)[], value: unknown] | [op: "reject", path: (string | symbol)[], error: unknown])[]) => boolean;
168
+ /**
169
+ * a function to subscribe to changes made to `value`
170
+ */
171
+ subscribe: () => () => void;
172
+ /**
173
+ * The remove method is only invoked when there are
174
+ * more than one nodes and when a valid index is provided.
175
+ * If the current index is removed,
176
+ * An index greater than the current index will be preferred as the next
177
+ * value.
178
+ *
179
+ * @param index - index of the node to remove
180
+ * @returns removedNode
181
+ */
182
+ remove: (index: number) => HistoryNode<V> | undefined;
183
+ /**
184
+ * utility to replace a value in history. The history
185
+ * changes will not be affected, only the value to be replaced.
186
+ * If a base value is needed to operate on,
187
+ * the `getNode` utility can be used to retrieve
188
+ * a cloned historyNode.
189
+ *
190
+ * <br> <br>
191
+ * Notes: <br>
192
+ * - No operations are done on the value provided to this utility. <br>
193
+ * - This is an advanced method, please ensure the value provided
194
+ * is a snapshot of the same type of the value being tracked. <br>
195
+ *
196
+ * @param index - index to replace value for
197
+ * @param value - the updated snapshot to be stored at the index
198
+ */
199
+ replace: (index: number, value: Snapshot<V>) => void;
200
+ };
package/src/index.js CHANGED
@@ -1,5 +1,252 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./history-utility"), exports);
1
+ import { unstable_buildProxyFunction as buildProxyFunction, proxy, ref, snapshot, subscribe, } from 'valtio/vanilla';
2
+ const isObject = (value) => !!value && typeof value === 'object';
3
+ let refSet;
4
+ const deepClone = (value) => {
5
+ if (!refSet) {
6
+ refSet = buildProxyFunction()[2];
7
+ }
8
+ if (!isObject(value) || refSet.has(value)) {
9
+ return value;
10
+ }
11
+ const baseObject = Array.isArray(value)
12
+ ? []
13
+ : Object.create(Object.getPrototypeOf(value));
14
+ Reflect.ownKeys(value).forEach((key) => {
15
+ baseObject[key] = deepClone(value[key]);
16
+ });
17
+ return baseObject;
18
+ };
19
+ const normalizeOptions = (options) => {
20
+ if (typeof options === 'boolean') {
21
+ if (import.meta.env?.MODE !== 'production') {
22
+ console.warn(`The second parameter of 'proxyWithHistory' as boolean is deprecated and support for boolean will be removed
23
+ in the next major version. Please use the object syntax instead:
24
+
25
+ { skipSubscribe: boolean }
26
+ `);
27
+ }
28
+ return { skipSubscribe: options };
29
+ }
30
+ const defaultOptions = {
31
+ skipSubscribe: false,
32
+ };
33
+ if (!options)
34
+ return defaultOptions;
35
+ return {
36
+ ...defaultOptions,
37
+ ...options,
38
+ };
39
+ };
40
+ /**
41
+ * This creates a new proxy with history support (ProxyHistoryObject).
42
+ * It includes following main properties:<br>
43
+ * - value: any value (does not have to be an object)<br>
44
+ * - history: an object holding the history of snapshots and other metadata<br>
45
+ * - history.index: the history index of the current snapshot<br>
46
+ * - history.nodes: the nodes of the history for each change<br>
47
+ * - history.wip: field for holding sandbox changes; used to avoid infinite loops<br>
48
+ * - canUndo: a function to return true if undo is available <br>
49
+ * - undo: a function to go back history <br>
50
+ * - canRedo: a function to return true if redo is available <br>
51
+ * - redo: a function to go forward history <br>
52
+ * - saveHistory: a function to save history <br>
53
+ * - getCurrentChangeDate: gets the date of the current change <br>
54
+ * - remove: a function to remove a specified history index <br>
55
+ * - replace: a function to replace a snapshot at a specified history index <br>
56
+ * - getNode: a function to get the node at a specified history index <br>
57
+ *
58
+ * <br>
59
+ * Notes: <br>
60
+ * - Suspense/promise is not supported. <br>
61
+ *
62
+ * @param initialValue - any value to be tracked
63
+ * @param options - use to configure the proxyWithHistory utility.
64
+ * @returns proxyObject
65
+ *
66
+ * @example
67
+ * import { proxyWithHistory } from 'valtio-history'
68
+ * const state = proxyWithHistory({
69
+ * count: 1,
70
+ * })
71
+ */
72
+ export function proxyWithHistory(initialValue, options) {
73
+ const utilOptions = normalizeOptions(options);
74
+ const proxyObject = proxy({
75
+ /**
76
+ * any value to be tracked (does not have to be an object)
77
+ */
78
+ value: initialValue,
79
+ /**
80
+ * an object holding the history of snapshots and other metadata <br>
81
+ * - history.index: the history index to the current snapshot <br>
82
+ * - history.nodes: the nodes of the history for each change <br>
83
+ * - history.wip: field for holding sandbox changes; used to avoid infinite loops<br>
84
+ */
85
+ history: ref({
86
+ wip: undefined,
87
+ nodes: [],
88
+ index: -1,
89
+ }),
90
+ /**
91
+ * get the date when a node was entered into history.
92
+ *
93
+ * @returns date
94
+ */
95
+ getCurrentChangeDate: () => {
96
+ const node = proxyObject.history.nodes[proxyObject.history.index];
97
+ return node?.createdAt;
98
+ },
99
+ /**
100
+ * utility method to get a history node.
101
+ * The snapshot within this node is already cloned and
102
+ * will not affect the original value if updated.
103
+ *
104
+ * @param index
105
+ * @returns historyNode
106
+ */
107
+ getNode: (index) => {
108
+ const node = proxyObject.history.nodes[index];
109
+ return node
110
+ ? { ...node, snapshot: proxyObject.clone(node.snapshot) }
111
+ : undefined;
112
+ },
113
+ /**
114
+ * utility to clone a snapshot
115
+ */
116
+ clone: deepClone,
117
+ /**
118
+ * a function to go to a specific index in history
119
+ */
120
+ goTo: (index) => {
121
+ const node = proxyObject.history.nodes[index];
122
+ if (!node)
123
+ return;
124
+ proxyObject.history.wip = proxyObject.clone(node.snapshot);
125
+ proxyObject.value = proxyObject.history.wip;
126
+ proxyObject.history.index = index;
127
+ },
128
+ /**
129
+ * a function to return true if undo is available
130
+ * @returns boolean
131
+ */
132
+ canUndo: () => proxyObject.history.index > 0,
133
+ /**
134
+ * a function to go back in history
135
+ */
136
+ undo: () => {
137
+ if (proxyObject.canUndo()) {
138
+ proxyObject.history.wip = proxyObject.clone(proxyObject.history.nodes[--proxyObject.history.index]?.snapshot);
139
+ proxyObject.value = proxyObject.history.wip;
140
+ }
141
+ },
142
+ /**
143
+ * a function to return true if redo is available
144
+ * @returns boolean
145
+ */
146
+ canRedo: () => proxyObject.history.index < proxyObject.history.nodes.length - 1,
147
+ /**
148
+ * a function to go forward in history
149
+ */
150
+ redo: () => {
151
+ if (proxyObject.canRedo()) {
152
+ proxyObject.history.wip = proxyObject.clone(proxyObject.history.nodes[++proxyObject.history.index]?.snapshot);
153
+ proxyObject.value = proxyObject.history.wip;
154
+ }
155
+ },
156
+ /**
157
+ * a function to execute saving history when changes are made to `value`
158
+ */
159
+ saveHistory: () => {
160
+ proxyObject.history.nodes.splice(proxyObject.history.index + 1);
161
+ proxyObject.history.nodes.push({
162
+ createdAt: new Date(),
163
+ snapshot: snapshot(proxyObject).value,
164
+ });
165
+ ++proxyObject.history.index;
166
+ },
167
+ /**
168
+ * a function that returns true when the history should be updated
169
+ *
170
+ * @param ops - subscribeOps from subscribe callback
171
+ * @returns boolean
172
+ */
173
+ shouldSaveHistory: (ops) => ops.every((op) => op[1][0] === 'value' &&
174
+ (op[0] !== 'set' || op[2] !== proxyObject.history.wip)),
175
+ /**
176
+ * a function to subscribe to changes made to `value`
177
+ */
178
+ subscribe: () => subscribe(proxyObject, (ops) => {
179
+ if (proxyObject.shouldSaveHistory(ops))
180
+ proxyObject.saveHistory();
181
+ }),
182
+ // history rewrite utilities
183
+ /**
184
+ * The remove method is only invoked when there are
185
+ * more than one nodes and when a valid index is provided.
186
+ * If the current index is removed,
187
+ * An index greater than the current index will be preferred as the next
188
+ * value.
189
+ *
190
+ * @param index - index of the node to remove
191
+ * @returns removedNode
192
+ */
193
+ remove: (index) => {
194
+ const node = proxyObject.history.nodes[index];
195
+ const isCurrentIndex = proxyObject.history.index === index;
196
+ const lastIndex = proxyObject.history.nodes.length - 1;
197
+ const isLastIndex = lastIndex === index;
198
+ if (!node || proxyObject.history.nodes.length < 2)
199
+ return;
200
+ if (isCurrentIndex) {
201
+ const resolvedIndex = isLastIndex ? index - 1 : index + 1;
202
+ const resolvedNode = proxyObject.history.nodes[resolvedIndex];
203
+ proxyObject.history.wip = proxyObject.clone(resolvedNode?.snapshot);
204
+ proxyObject.value = proxyObject.history.wip;
205
+ if (isLastIndex)
206
+ proxyObject.history.index--;
207
+ }
208
+ proxyObject.history.nodes.splice(index, 1);
209
+ if (!isCurrentIndex && proxyObject.history.index > index) {
210
+ proxyObject.history.index--;
211
+ }
212
+ return node;
213
+ },
214
+ /**
215
+ * utility to replace a value in history. The history
216
+ * changes will not be affected, only the value to be replaced.
217
+ * If a base value is needed to operate on,
218
+ * the `getNode` utility can be used to retrieve
219
+ * a cloned historyNode.
220
+ *
221
+ * <br> <br>
222
+ * Notes: <br>
223
+ * - No operations are done on the value provided to this utility. <br>
224
+ * - This is an advanced method, please ensure the value provided
225
+ * is a snapshot of the same type of the value being tracked. <br>
226
+ *
227
+ * @param index - index to replace value for
228
+ * @param value - the updated snapshot to be stored at the index
229
+ */
230
+ replace: (index, value) => {
231
+ const node = proxyObject.history.nodes[index];
232
+ const isCurrentIndex = proxyObject.history.index === index;
233
+ if (!node)
234
+ return;
235
+ proxyObject.history.nodes[index] = {
236
+ ...node,
237
+ snapshot: value,
238
+ updatedAt: new Date(),
239
+ };
240
+ if (isCurrentIndex) {
241
+ proxyObject.history.wip = value;
242
+ proxyObject.value = proxyObject.history.wip;
243
+ }
244
+ },
245
+ });
246
+ proxyObject.saveHistory();
247
+ if (!utilOptions.skipSubscribe) {
248
+ proxyObject.subscribe();
249
+ }
250
+ return proxyObject;
251
+ }
5
252
  //# sourceMappingURL=index.js.map
package/src/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/history-utility/src/index.ts"],"names":[],"mappings":";;;AAAA,4DAAkC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/history-utility/src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,2BAA2B,IAAI,kBAAkB,EACjD,KAAK,EACL,GAAG,EACH,QAAQ,EACR,SAAS,GACV,MAAM,gBAAgB,CAAC;AA2CxB,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAmB,EAAE,CACnD,CAAC,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,CAAC;AAEvC,IAAI,MAAmC,CAAC;AAExC,M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,183 +0,0 @@
1
- import type { INTERNAL_Snapshot as Snapshot } from 'valtio/vanilla';
2
- export type HistoryNode<T> = {
3
- /**
4
- * The snapshot being tracked
5
- */
6
- snapshot: Snapshot<T>;
7
- /**
8
- * The date when the node was created
9
- */
10
- createdAt: Date;
11
- /**
12
- * The date when the node was updated. Will be undefined if
13
- * the node was never updated.
14
- */
15
- updatedAt?: Date;
16
- };
17
- export type History<T> = {
18
- /**
19
- * field for holding sandbox changes; used to avoid infinite loops
20
- */
21
- wip?: Snapshot<T>;
22
- /**
23
- * the nodes of the history for each change
24
- */
25
- nodes: HistoryNode<T>[];
26
- /**
27
- * the history index of the current snapshot
28
- */
29
- index: number;
30
- };
31
- /**
32
- * This creates a new proxy with history support (ProxyHistoryObject).
33
- * It includes following main properties:<br>
34
- * - value: any value (does not have to be an object)<br>
35
- * - history: an object holding the history of snapshots and other metadata<br>
36
- * - history.index: the history index of the current snapshot<br>
37
- * - history.nodes: the nodes of the history for each change<br>
38
- * - history.wip: field for holding sandbox changes; used to avoid infinite loops<br>
39
- * - canUndo: a function to return true if undo is available <br>
40
- * - undo: a function to go back history <br>
41
- * - canRedo: a function to return true if redo is available <br>
42
- * - redo: a function to go forward history <br>
43
- * - saveHistory: a function to save history <br>
44
- * - getCurrentChangeDate: gets the date of the current change <br>
45
- * - remove: a function to remove a specified history index <br>
46
- * - replace: a function to replace a snapshot at a specified history index <br>
47
- * - getNode: a function to get the node at a specified history index <br>
48
- *
49
- * <br>
50
- * Notes: <br>
51
- * - Suspense/promise is not supported. <br>
52
- *
53
- * @param initialValue - any object to track
54
- * @param skipSubscribe - determines if the internal subscribe behaviour should be skipped.
55
- * @returns proxyObject
56
- *
57
- * @example
58
- * import { proxyWithHistory } from 'valtio-history'
59
- * const state = proxyWithHistory({
60
- * count: 1,
61
- * })
62
- */
63
- export declare function proxyWithHistory<V>(initialValue: V, skipSubscribe?: boolean): {
64
- /**
65
- * any value to be tracked (does not have to be an object)
66
- */
67
- value: V;
68
- /**
69
- * an object holding the history of snapshots and other metadata <br>
70
- * - history.index: the history index to the current snapshot <br>
71
- * - history.nodes: the nodes of the history for each change <br>
72
- * - history.wip: field for holding sandbox changes; used to avoid infinite loops<br>
73
- */
74
- history: History<V> & {
75
- $$valtioRef: true;
76
- };
77
- /**
78
- * get the date when a node was entered into history.
79
- *
80
- * @returns date
81
- */
82
- getCurrentChangeDate: () => Date | undefined;
83
- /**
84
- * utility method to get a history node.
85
- * The snapshot within this node is already cloned and
86
- * will not affect the original value if updated.
87
- *
88
- * @param index
89
- * @returns historyNode
90
- */
91
- getNode: (index: number) => {
92
- snapshot: V extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
93
- $$valtioRef: true;
94
- } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? V : V extends Promise<unknown> ? Awaited<V> : V extends object ? { readonly [K in keyof V]: V[K] extends infer T ? T extends V[K] ? T extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
95
- $$valtioRef: true;
96
- } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T : T extends Promise<unknown> ? Awaited<T> : T extends object ? { readonly [K_1 in keyof T]: T[K_1] extends infer T_1 ? T_1 extends T[K_1] ? T_1 extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
97
- $$valtioRef: true;
98
- } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_1 : T_1 extends Promise<unknown> ? Awaited<T_1> : T_1 extends object ? { readonly [K_2 in keyof T_1]: T_1[K_2] extends infer T_2 ? T_2 extends T_1[K_2] ? T_2 extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
99
- $$valtioRef: true;
100
- } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_2 : T_2 extends Promise<unknown> ? Awaited<T_2> : T_2 extends object ? { readonly [K_3 in keyof T_2]: T_2[K_3] extends infer T_3 ? T_3 extends T_2[K_3] ? T_3 extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
101
- $$valtioRef: true;
102
- } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_3 : T_3 extends Promise<unknown> ? Awaited<T_3> : T_3 extends object ? { readonly [K_4 in keyof T_3]: T_3[K_4] extends infer T_4 ? T_4 extends T_3[K_4] ? T_4 extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
103
- $$valtioRef: true;
104
- } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_4 : T_4 extends Promise<unknown> ? Awaited<T_4> : T_4 extends object ? { readonly [K_5 in keyof T_4]: T_4[K_5] extends infer T_5 ? T_5 extends T_4[K_5] ? T_5 extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
105
- $$valtioRef: true;
106
- } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_5 : T_5 extends Promise<unknown> ? Awaited<T_5> : T_5 extends object ? { readonly [K_6 in keyof T_5]: T_5[K_6] extends infer T_6 ? T_6 extends T_5[K_6] ? T_6 extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
107
- $$valtioRef: true;
108
- } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_6 : T_6 extends Promise<unknown> ? Awaited<T_6> : T_6 extends object ? { readonly [K_7 in keyof T_6]: T_6[K_7] extends infer T_7 ? T_7 extends T_6[K_7] ? T_7 extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
109
- $$valtioRef: true;
110
- } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_7 : T_7 extends Promise<unknown> ? Awaited<T_7> : T_7 extends object ? { readonly [K_8 in keyof T_7]: T_7[K_8] extends infer T_8 ? T_8 extends T_7[K_8] ? T_8 extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
111
- $$valtioRef: true;
112
- } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_8 : T_8 extends Promise<unknown> ? Awaited<T_8> : T_8 extends object ? { readonly [K_9 in keyof T_8]: T_8[K_9] extends infer T_9 ? T_9 extends T_8[K_9] ? T_9 extends RegExp | Date | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | {
113
- $$valtioRef: true;
114
- } | Error | ((...args: any[]) => any) | (string | number | bigint | boolean | symbol | null | undefined) ? T_9 : T_9 extends Promise<unknown> ? Awaited<T_9> : T_9 extends object ? { readonly [K_10 in keyof T_9]: any; } : T_9 : never : never; } : T_8 : never : never; } : T_7 : never : never; } : T_6 : never : never; } : T_5 : never : never; } : T_4 : never : never; } : T_3 : never : never; } : T_2 : never : never; } : T_1 : never : never; } : T : never : never; } : V;
115
- /**
116
- * The date when the node was created
117
- */
118
- createdAt: Date;
119
- /**
120
- * The date when the node was updated. Will be undefined if
121
- * the node was never updated.
122
- */
123
- updatedAt?: Date | undefined;
124
- } | undefined;
125
- /**
126
- * utility to clone a snapshot
127
- */
128
- clone: <T_10>(value: T_10) => T_10;
129
- /**
130
- * a function to return true if undo is available
131
- * @returns boolean
132
- */
133
- canUndo: () => boolean;
134
- /**
135
- * a function to go back in history
136
- */
137
- undo: () => void;
138
- /**
139
- * a function to return true if redo is available
140
- * @returns boolean
141
- */
142
- canRedo: () => boolean;
143
- /**
144
- * a function to go forward in history
145
- */
146
- redo: () => void;
147
- /**
148
- * a function to execute saving history when changes are made to `value`
149
- */
150
- saveHistory: () => void;
151
- /**
152
- * a function to subscribe to changes made to `value`
153
- */
154
- subscribe: () => () => void;
155
- /**
156
- * The remove method is only invoked when there are
157
- * more than one nodes and when a valid index is provided.
158
- * If the current index is removed,
159
- * An index greater than the current index will be preferred as the next
160
- * value.
161
- *
162
- * @param index - index of the node to remove
163
- * @returns removedNode
164
- */
165
- remove: (index: number) => HistoryNode<V> | undefined;
166
- /**
167
- * utility to replace a value in history. The history
168
- * changes will not be affected, only the value to be replaced.
169
- * If a base value is needed to operate on,
170
- * the `getNode` utility can be used to retrieve
171
- * a cloned historyNode.
172
- *
173
- * <br> <br>
174
- * Notes: <br>
175
- * - No operations are done on the value provided to this utility. <br>
176
- * - This is an advanced method, please ensure the value provided
177
- * is a snapshot of the same type of the value being tracked. <br>
178
- *
179
- * @param index - index to replace value for
180
- * @param value - the updated snapshot to be stored at the index
181
- */
182
- replace: (index: number, value: Snapshot<V>) => void;
183
- };
@@ -1,214 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.proxyWithHistory = void 0;
4
- const vanilla_1 = require("valtio/vanilla");
5
- const isObject = (value) => !!value && typeof value === 'object';
6
- let refSet;
7
- const deepClone = (value) => {
8
- if (!refSet) {
9
- refSet = (0, vanilla_1.unstable_buildProxyFunction)()[2];
10
- }
11
- if (!isObject(value) || refSet.has(value)) {
12
- return value;
13
- }
14
- const baseObject = Array.isArray(value)
15
- ? []
16
- : Object.create(Object.getPrototypeOf(value));
17
- Reflect.ownKeys(value).forEach((key) => {
18
- baseObject[key] = deepClone(value[key]);
19
- });
20
- return baseObject;
21
- };
22
- /**
23
- * This creates a new proxy with history support (ProxyHistoryObject).
24
- * It includes following main properties:<br>
25
- * - value: any value (does not have to be an object)<br>
26
- * - history: an object holding the history of snapshots and other metadata<br>
27
- * - history.index: the history index of the current snapshot<br>
28
- * - history.nodes: the nodes of the history for each change<br>
29
- * - history.wip: field for holding sandbox changes; used to avoid infinite loops<br>
30
- * - canUndo: a function to return true if undo is available <br>
31
- * - undo: a function to go back history <br>
32
- * - canRedo: a function to return true if redo is available <br>
33
- * - redo: a function to go forward history <br>
34
- * - saveHistory: a function to save history <br>
35
- * - getCurrentChangeDate: gets the date of the current change <br>
36
- * - remove: a function to remove a specified history index <br>
37
- * - replace: a function to replace a snapshot at a specified history index <br>
38
- * - getNode: a function to get the node at a specified history index <br>
39
- *
40
- * <br>
41
- * Notes: <br>
42
- * - Suspense/promise is not supported. <br>
43
- *
44
- * @param initialValue - any object to track
45
- * @param skipSubscribe - determines if the internal subscribe behaviour should be skipped.
46
- * @returns proxyObject
47
- *
48
- * @example
49
- * import { proxyWithHistory } from 'valtio-history'
50
- * const state = proxyWithHistory({
51
- * count: 1,
52
- * })
53
- */
54
- function proxyWithHistory(initialValue, skipSubscribe = false) {
55
- const proxyObject = (0, vanilla_1.proxy)({
56
- /**
57
- * any value to be tracked (does not have to be an object)
58
- */
59
- value: initialValue,
60
- /**
61
- * an object holding the history of snapshots and other metadata <br>
62
- * - history.index: the history index to the current snapshot <br>
63
- * - history.nodes: the nodes of the history for each change <br>
64
- * - history.wip: field for holding sandbox changes; used to avoid infinite loops<br>
65
- */
66
- history: (0, vanilla_1.ref)({
67
- wip: undefined,
68
- nodes: [],
69
- index: -1,
70
- }),
71
- /**
72
- * get the date when a node was entered into history.
73
- *
74
- * @returns date
75
- */
76
- getCurrentChangeDate: () => {
77
- const node = proxyObject.history.nodes[proxyObject.history.index];
78
- return node === null || node === void 0 ? void 0 : node.createdAt;
79
- },
80
- /**
81
- * utility method to get a history node.
82
- * The snapshot within this node is already cloned and
83
- * will not affect the original value if updated.
84
- *
85
- * @param index
86
- * @returns historyNode
87
- */
88
- getNode: (index) => {
89
- const node = proxyObject.history.nodes[index];
90
- return node
91
- ? Object.assign(Object.assign({}, node), { snapshot: proxyObject.clone(node.snapshot) }) : undefined;
92
- },
93
- /**
94
- * utility to clone a snapshot
95
- */
96
- clone: deepClone,
97
- /**
98
- * a function to return true if undo is available
99
- * @returns boolean
100
- */
101
- canUndo: () => proxyObject.history.index > 0,
102
- /**
103
- * a function to go back in history
104
- */
105
- undo: () => {
106
- var _a;
107
- if (proxyObject.canUndo()) {
108
- proxyObject.history.wip = proxyObject.clone((_a = proxyObject.history.nodes[--proxyObject.history.index]) === null || _a === void 0 ? void 0 : _a.snapshot);
109
- proxyObject.value = proxyObject.history.wip;
110
- }
111
- },
112
- /**
113
- * a function to return true if redo is available
114
- * @returns boolean
115
- */
116
- canRedo: () => proxyObject.history.index < proxyObject.history.nodes.length - 1,
117
- /**
118
- * a function to go forward in history
119
- */
120
- redo: () => {
121
- var _a;
122
- if (proxyObject.canRedo()) {
123
- proxyObject.history.wip = proxyObject.clone((_a = proxyObject.history.nodes[++proxyObject.history.index]) === null || _a === void 0 ? void 0 : _a.snapshot);
124
- proxyObject.value = proxyObject.history.wip;
125
- }
126
- },
127
- /**
128
- * a function to execute saving history when changes are made to `value`
129
- */
130
- saveHistory: () => {
131
- proxyObject.history.nodes.splice(proxyObject.history.index + 1);
132
- proxyObject.history.nodes.push({
133
- createdAt: new Date(),
134
- snapshot: (0, vanilla_1.snapshot)(proxyObject).value,
135
- });
136
- ++proxyObject.history.index;
137
- },
138
- /**
139
- * a function to subscribe to changes made to `value`
140
- */
141
- subscribe: () => (0, vanilla_1.subscribe)(proxyObject, (ops) => {
142
- if (ops.every((op) => op[1][0] === 'value' &&
143
- (op[0] !== 'set' || op[2] !== proxyObject.history.wip))) {
144
- proxyObject.saveHistory();
145
- }
146
- }),
147
- // history rewrite utilities
148
- /**
149
- * The remove method is only invoked when there are
150
- * more than one nodes and when a valid index is provided.
151
- * If the current index is removed,
152
- * An index greater than the current index will be preferred as the next
153
- * value.
154
- *
155
- * @param index - index of the node to remove
156
- * @returns removedNode
157
- */
158
- remove: (index) => {
159
- const node = proxyObject.history.nodes[index];
160
- const isCurrentIndex = proxyObject.history.index === index;
161
- const lastIndex = proxyObject.history.nodes.length - 1;
162
- const isLastIndex = lastIndex === index;
163
- if (!node || proxyObject.history.nodes.length < 2)
164
- return;
165
- if (isCurrentIndex) {
166
- const resolvedIndex = isLastIndex ? index - 1 : index + 1;
167
- const resolvedNode = proxyObject.history.nodes[resolvedIndex];
168
- proxyObject.history.wip = proxyObject.clone(resolvedNode === null || resolvedNode === void 0 ? void 0 : resolvedNode.snapshot);
169
- proxyObject.value = proxyObject.history.wip;
170
- if (isLastIndex)
171
- proxyObject.history.index--;
172
- }
173
- proxyObject.history.nodes.splice(index, 1);
174
- if (!isCurrentIndex && proxyObject.history.index > index) {
175
- proxyObject.history.index--;
176
- }
177
- return node;
178
- },
179
- /**
180
- * utility to replace a value in history. The history
181
- * changes will not be affected, only the value to be replaced.
182
- * If a base value is needed to operate on,
183
- * the `getNode` utility can be used to retrieve
184
- * a cloned historyNode.
185
- *
186
- * <br> <br>
187
- * Notes: <br>
188
- * - No operations are done on the value provided to this utility. <br>
189
- * - This is an advanced method, please ensure the value provided
190
- * is a snapshot of the same type of the value being tracked. <br>
191
- *
192
- * @param index - index to replace value for
193
- * @param value - the updated snapshot to be stored at the index
194
- */
195
- replace: (index, value) => {
196
- const node = proxyObject.history.nodes[index];
197
- const isCurrentIndex = proxyObject.history.index === index;
198
- if (!node)
199
- return;
200
- proxyObject.history.nodes[index] = Object.assign(Object.assign({}, node), { snapshot: value, updatedAt: new Date() });
201
- if (isCurrentIndex) {
202
- proxyObject.history.wip = value;
203
- proxyObject.value = proxyObject.history.wip;
204
- }
205
- },
206
- });
207
- proxyObject.saveHistory();
208
- if (!skipSubscribe) {
209
- proxyObject.subscribe();
210
- }
211
- return proxyObject;
212
- }
213
- exports.proxyWithHistory = proxyWithHistory;
214
- //# sourceMappingURL=history-utility.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"history-utility.js","sourceRoot":"","sources":["../../../../packages/history-utility/src/history-utility.ts"],"names":[],"mappings":";;;AAAA,4CAMwB;AAkCxB,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAmB,EAAE,CACnD,CAAC,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,CAAC;AAEvC,IAAI,MAAmC,CAAC;AAExC,MAAM,SAAS,GAAG,CAAI,KAAQ,EAAK,EAAE;IACnC,IAAI,CAAC,MAAM,EAAE;QACX,M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