valtio-history 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "valtio-history",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "author": "Daishi Kato",
5
5
  "repository": {
6
6
  "type": "git",
package/src/index.d.ts CHANGED
@@ -1 +1,232 @@
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
+ * @deprecated @see {@link https://github.com/valtiojs/valtio-history/issues/10}
87
+ * @returns date
88
+ */
89
+ getCurrentChangeDate: () => Date | undefined;
90
+ /**
91
+ * get the date when a node was entered into history.
92
+ *
93
+ * @returns date
94
+ */
95
+ readonly currentChangeDate: Date | undefined;
96
+ /**
97
+ * the current history node index.
98
+ *
99
+ * @returns number
100
+ */
101
+ readonly currentIndex: number;
102
+ /**the total number of the history nodes.
103
+ *
104
+ * @returns number
105
+ */
106
+ readonly historyNodeCount: number;
107
+ /**
108
+ * utility method to get a history node.
109
+ * The snapshot within this node is already cloned and
110
+ * will not affect the original value if updated.
111
+ *
112
+ * @param index
113
+ * @returns historyNode
114
+ */
115
+ getNode: (index: number) => {
116
+ snapshot: V 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) ? 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> | {
119
+ $$valtioRef: true;
120
+ } | 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> | {
121
+ $$valtioRef: true;
122
+ } | 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> | {
123
+ $$valtioRef: true;
124
+ } | 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> | {
125
+ $$valtioRef: true;
126
+ } | 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> | {
127
+ $$valtioRef: true;
128
+ } | 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> | {
129
+ $$valtioRef: true;
130
+ } | 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> | {
131
+ $$valtioRef: true;
132
+ } | 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> | {
133
+ $$valtioRef: true;
134
+ } | 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> | {
135
+ $$valtioRef: true;
136
+ } | 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> | {
137
+ $$valtioRef: true;
138
+ } | 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;
139
+ /**
140
+ * The date when the node was created
141
+ */
142
+ createdAt: Date;
143
+ /**
144
+ * The date when the node was updated. Will be undefined if
145
+ * the node was never updated.
146
+ */
147
+ updatedAt?: Date | undefined;
148
+ } | undefined;
149
+ /**
150
+ * utility to clone a snapshot
151
+ */
152
+ clone: <T_10>(value: T_10) => T_10;
153
+ /**
154
+ * a function to go to a specific index in history
155
+ */
156
+ goTo: (index: number) => void;
157
+ /**
158
+ * a getter to return true if undo is available
159
+ * @returns boolean
160
+ */
161
+ readonly isUndoEnabled: boolean;
162
+ /**
163
+ * a getter to return true if redo is available
164
+ * @returns boolean
165
+ */
166
+ readonly isRedoEnabled: boolean;
167
+ /**
168
+ * a function to return true if undo is available
169
+ *
170
+ * @deprecated @see {@link https://github.com/valtiojs/valtio-history/issues/10}
171
+ * @returns boolean
172
+ */
173
+ canUndo: () => boolean;
174
+ /**
175
+ * a function to go back in history
176
+ */
177
+ undo: () => void;
178
+ /**
179
+ * a function to return true if redo is available
180
+ *
181
+ * @deprecated @see {@link https://github.com/valtiojs/valtio-history/issues/10}
182
+ * @returns boolean
183
+ */
184
+ canRedo: () => boolean;
185
+ /**
186
+ * a function to go forward in history
187
+ */
188
+ redo: () => void;
189
+ /**
190
+ * a function to execute saving history when changes are made to `value`
191
+ */
192
+ saveHistory: () => void;
193
+ /**
194
+ * a function that returns true when the history should be updated
195
+ *
196
+ * @param ops - subscribeOps from subscribe callback
197
+ * @returns boolean
198
+ */
199
+ 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;
200
+ /**
201
+ * a function to subscribe to changes made to `value`
202
+ */
203
+ subscribe: () => () => void;
204
+ /**
205
+ * The remove method is only invoked when there are
206
+ * more than one nodes and when a valid index is provided.
207
+ * If the current index is removed,
208
+ * An index greater than the current index will be preferred as the next
209
+ * value.
210
+ *
211
+ * @param index - index of the node to remove
212
+ * @returns removedNode
213
+ */
214
+ remove: (index: number) => HistoryNode<V> | undefined;
215
+ /**
216
+ * utility to replace a value in history. The history
217
+ * changes will not be affected, only the value to be replaced.
218
+ * If a base value is needed to operate on,
219
+ * the `getNode` utility can be used to retrieve
220
+ * a cloned historyNode.
221
+ *
222
+ * <br> <br>
223
+ * Notes: <br>
224
+ * - No operations are done on the value provided to this utility. <br>
225
+ * - This is an advanced method, please ensure the value provided
226
+ * is a snapshot of the same type of the value being tracked. <br>
227
+ *
228
+ * @param index - index to replace value for
229
+ * @param value - the updated snapshot to be stored at the index
230
+ */
231
+ replace: (index: number, value: Snapshot<V>) => void;
232
+ };
package/src/index.js CHANGED
@@ -1,2 +1,295 @@
1
- export * from './history-utility';
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
+ * @deprecated @see {@link https://github.com/valtiojs/valtio-history/issues/10}
94
+ * @returns date
95
+ */
96
+ getCurrentChangeDate: () => {
97
+ const node = proxyObject.history.nodes[proxyObject.history.index];
98
+ return node?.createdAt;
99
+ },
100
+ /**
101
+ * get the date when a node was entered into history.
102
+ *
103
+ * @returns date
104
+ */
105
+ get currentChangeDate() {
106
+ const node = this.history.nodes[this.history.index];
107
+ return node?.createdAt;
108
+ },
109
+ /**
110
+ * the current history node index.
111
+ *
112
+ * @returns number
113
+ */
114
+ get currentIndex() {
115
+ return this.history.index;
116
+ },
117
+ /**the total number of the history nodes.
118
+ *
119
+ * @returns number
120
+ */
121
+ get historyNodeCount() {
122
+ return this.history.nodes.length;
123
+ },
124
+ /**
125
+ * utility method to get a history node.
126
+ * The snapshot within this node is already cloned and
127
+ * will not affect the original value if updated.
128
+ *
129
+ * @param index
130
+ * @returns historyNode
131
+ */
132
+ getNode: (index) => {
133
+ const node = proxyObject.history.nodes[index];
134
+ return node
135
+ ? { ...node, snapshot: proxyObject.clone(node.snapshot) }
136
+ : undefined;
137
+ },
138
+ /**
139
+ * utility to clone a snapshot
140
+ */
141
+ clone: deepClone,
142
+ /**
143
+ * a function to go to a specific index in history
144
+ */
145
+ goTo: (index) => {
146
+ const node = proxyObject.history.nodes[index];
147
+ if (!node)
148
+ return;
149
+ proxyObject.history.wip = proxyObject.clone(node.snapshot);
150
+ proxyObject.value = proxyObject.history.wip;
151
+ proxyObject.history.index = index;
152
+ },
153
+ /**
154
+ * a getter to return true if undo is available
155
+ * @returns boolean
156
+ */
157
+ get isUndoEnabled() {
158
+ return this.history.index > 0;
159
+ },
160
+ /**
161
+ * a getter to return true if redo is available
162
+ * @returns boolean
163
+ */
164
+ get isRedoEnabled() {
165
+ return this.history.index < this.history.nodes.length - 1;
166
+ },
167
+ /**
168
+ * a function to return true if undo is available
169
+ *
170
+ * @deprecated @see {@link https://github.com/valtiojs/valtio-history/issues/10}
171
+ * @returns boolean
172
+ */
173
+ canUndo: () => proxyObject.history.index > 0,
174
+ /**
175
+ * a function to go back in history
176
+ */
177
+ undo: () => {
178
+ if (proxyObject.canUndo()) {
179
+ proxyObject.history.wip = proxyObject.clone(proxyObject.history.nodes[--proxyObject.history.index]?.snapshot);
180
+ proxyObject.value = proxyObject.history.wip;
181
+ }
182
+ },
183
+ /**
184
+ * a function to return true if redo is available
185
+ *
186
+ * @deprecated @see {@link https://github.com/valtiojs/valtio-history/issues/10}
187
+ * @returns boolean
188
+ */
189
+ canRedo: () => proxyObject.history.index < proxyObject.history.nodes.length - 1,
190
+ /**
191
+ * a function to go forward in history
192
+ */
193
+ redo: () => {
194
+ if (proxyObject.canRedo()) {
195
+ proxyObject.history.wip = proxyObject.clone(proxyObject.history.nodes[++proxyObject.history.index]?.snapshot);
196
+ proxyObject.value = proxyObject.history.wip;
197
+ }
198
+ },
199
+ /**
200
+ * a function to execute saving history when changes are made to `value`
201
+ */
202
+ saveHistory: () => {
203
+ proxyObject.history.nodes.splice(proxyObject.history.index + 1);
204
+ proxyObject.history.nodes.push({
205
+ createdAt: new Date(),
206
+ snapshot: snapshot(proxyObject).value,
207
+ });
208
+ ++proxyObject.history.index;
209
+ },
210
+ /**
211
+ * a function that returns true when the history should be updated
212
+ *
213
+ * @param ops - subscribeOps from subscribe callback
214
+ * @returns boolean
215
+ */
216
+ shouldSaveHistory: (ops) => ops.every((op) => op[1][0] === 'value' &&
217
+ (op[0] !== 'set' || op[2] !== proxyObject.history.wip)),
218
+ /**
219
+ * a function to subscribe to changes made to `value`
220
+ */
221
+ subscribe: () => subscribe(proxyObject, (ops) => {
222
+ if (proxyObject.shouldSaveHistory(ops))
223
+ proxyObject.saveHistory();
224
+ }),
225
+ // history rewrite utilities
226
+ /**
227
+ * The remove method is only invoked when there are
228
+ * more than one nodes and when a valid index is provided.
229
+ * If the current index is removed,
230
+ * An index greater than the current index will be preferred as the next
231
+ * value.
232
+ *
233
+ * @param index - index of the node to remove
234
+ * @returns removedNode
235
+ */
236
+ remove: (index) => {
237
+ const node = proxyObject.history.nodes[index];
238
+ const isCurrentIndex = proxyObject.history.index === index;
239
+ const lastIndex = proxyObject.history.nodes.length - 1;
240
+ const isLastIndex = lastIndex === index;
241
+ if (!node || proxyObject.history.nodes.length < 2)
242
+ return;
243
+ if (isCurrentIndex) {
244
+ const resolvedIndex = isLastIndex ? index - 1 : index + 1;
245
+ const resolvedNode = proxyObject.history.nodes[resolvedIndex];
246
+ proxyObject.history.wip = proxyObject.clone(resolvedNode?.snapshot);
247
+ proxyObject.value = proxyObject.history.wip;
248
+ if (isLastIndex)
249
+ proxyObject.history.index--;
250
+ }
251
+ proxyObject.history.nodes.splice(index, 1);
252
+ if (!isCurrentIndex && proxyObject.history.index > index) {
253
+ proxyObject.history.index--;
254
+ }
255
+ return node;
256
+ },
257
+ /**
258
+ * utility to replace a value in history. The history
259
+ * changes will not be affected, only the value to be replaced.
260
+ * If a base value is needed to operate on,
261
+ * the `getNode` utility can be used to retrieve
262
+ * a cloned historyNode.
263
+ *
264
+ * <br> <br>
265
+ * Notes: <br>
266
+ * - No operations are done on the value provided to this utility. <br>
267
+ * - This is an advanced method, please ensure the value provided
268
+ * is a snapshot of the same type of the value being tracked. <br>
269
+ *
270
+ * @param index - index to replace value for
271
+ * @param value - the updated snapshot to be stored at the index
272
+ */
273
+ replace: (index, value) => {
274
+ const node = proxyObject.history.nodes[index];
275
+ const isCurrentIndex = proxyObject.history.index === index;
276
+ if (!node)
277
+ return;
278
+ proxyObject.history.nodes[index] = {
279
+ ...node,
280
+ snapshot: value,
281
+ updatedAt: new Date(),
282
+ };
283
+ if (isCurrentIndex) {
284
+ proxyObject.history.wip = value;
285
+ proxyObject.value = proxyObject.history.wip;
286
+ }
287
+ },
288
+ });
289
+ proxyObject.saveHistory();
290
+ if (!utilOptions.skipSubscribe) {
291
+ proxyObject.subscribe();
292
+ }
293
+ return proxyObject;
294
+ }
2
295
  //# 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,cAAc,mBAAmB,CAAC"}
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,MAAM,SAAS,GAAG,CAAI,KAAQ,EAAK,EAAE;IACnC,IAAI,CAAC,MAAM,EAAE;QACX,MAAM,GAAG,kBAAkB,EAAE,CAAC,CAAC,CAAC,CAAC;KAClC;IACD,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE;QACzC,OAAO,KAAK,CAAC;KACd;IACD,MAAM,UAAU,GAAM,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QACxC,CAAC,CAAC,EAAE;QACJ,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,CAAC;IAChD,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;QACrC,UAAU,CAAC,GAAc,CAAC,GAAG,SAAS,CAAC,KAAK,CAAC,GAAc,CAAC,CAAC,CAAC;IAChE,CAAC,CAAC,CAAC;IACH,OAAO,UAAU,CAAC;AACpB,CAAC,CAAC;AAEF,MAAM,gBAAgB,GAAG,CACvB,OAAkC,EAClB,EAAE;IAClB,IAAI,OAAO,OAAO,KAAK,SAAS,EA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- 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
- };
@@ -1,252 +0,0 @@
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
- }
252
- //# 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,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,MAAM,SAAS,GAAG,CAAI,KAAQ,EAAK,EAAE;IACnC,IAAI,CAAC,MAAM,EAAE;QACX,MAAM,GAAG,kBAAkB,EAAE,CAAC,CAAC,CAAC,CAAC;KAClC;IACD,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE;QACzC,OAAO,KAAK,CAAC;KACd;IACD,MAAM,UAAU,GAAM,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QACxC,CAAC,CAAC,EAAE;QACJ,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,CAAC;IAChD,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;QACrC,UAAU,CAAC,GAAc,CAAC,GAAG,SAAS,CAAC,K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