diff --git a/packages/history-utility/docs/modules.md b/packages/history-utility/docs/modules.md
index 21e234d..3a38f1c 100644
--- a/packages/history-utility/docs/modules.md
+++ b/packages/history-utility/docs/modules.md
@@ -36,7 +36,7 @@
#### Defined in
-[packages/history-utility/src/history-utility.ts:26](https://github.com/valtiojs/valtio-history/blob/d0466ae/packages/history-utility/src/history-utility.ts#L26)
+[packages/history-utility/src/history-utility.ts:27](https://github.com/valtiojs/valtio-history/blob/1d69722/packages/history-utility/src/history-utility.ts#L27)
---
@@ -60,7 +60,7 @@
#### Defined in
-[packages/history-utility/src/history-utility.ts:10](https://github.com/valtiojs/valtio-history/blob/d0466ae/packages/history-utility/src/history-utility.ts#L10)
+[packages/history-utility/src/history-utility.ts:11](https://github.com/valtiojs/valtio-history/blob/1d69722/packages/history-utility/src/history-utility.ts#L11)
---
@@ -76,7 +76,7 @@
#### Defined in
-[packages/history-utility/src/history-utility.ts:43](https://github.com/valtiojs/valtio-history/blob/d0466ae/packages/history-utility/src/history-utility.ts#L43)
+[packages/history-utility/src/history-utility.ts:44](https://github.com/valtiojs/valtio-history/blob/1d69722/packages/history-utility/src/history-utility.ts#L44)
## Functions
@@ -114,10 +114,10 @@ Notes:
#### Parameters
-| Name | Type | Description |
-| :------------- | :-------------------------------------------- | :--------------------------------------------- |
-| `initialValue` | `V` | any value to be tracked |
-| `options?` | [`HistoryOptions`](modules.md#historyoptions) | use to configure the proxyWithHistory utility. |
+| Name | Type | Description |
+| :------------- | :--------------------------------------------------------- | :--------------------------------------------- |
+| `initialValue` | `V` | any value to be tracked |
+| `options?` | `boolean` \| [`HistoryOptions`](modules.md#historyoptions) | use to configure the proxyWithHistory utility. |
#### Returns
@@ -138,7 +138,7 @@ proxyObject
| `remove` | (`index`: `number`) => `undefined` \| [`HistoryNode`](modules.md#historynode)\<`V`\> | The remove method is only invoked when there are more than one nodes and when a valid index is provided. If the current index is removed, An index greater than the current index will be preferred as the next value. |
| `replace` | (`index`: `number`, `value`: `INTERNAL_Snapshot`\<`V`\>) => `void` | utility to replace a value in history. The history changes will not be affected, only the value to be replaced. If a base value is needed to operate on, the `getNode` utility can be used to retrieve a cloned historyNode.
Notes:
- No operations are done on the value provided to this utility.
- This is an advanced method, please ensure the value provided is a snapshot of the same type of the value being tracked.
|
| `saveHistory` | () => `void` | a function to execute saving history when changes are made to `value` |
-| `shouldSaveHistory` | (`ops`: `Op`[]) => `boolean` | a function to return true if history should be saved |
+| `shouldSaveHistory` | (`ops`: `Op`[]) => `boolean` | a function that returns true when the history should be updated |
| `subscribe` | () => () => `void` | a function to subscribe to changes made to `value` |
| `undo` | () => `void` | a function to go back in history |
| `value` | `V` | any value to be tracked (does not have to be an object) |
@@ -154,4 +154,4 @@ const state = proxyWithHistory({
#### Defined in
-[packages/history-utility/src/history-utility.ts:103](https://github.com/valtiojs/valtio-history/blob/d0466ae/packages/history-utility/src/history-utility.ts#L103)
+[packages/history-utility/src/history-utility.ts:128](https://github.com/valtiojs/valtio-history/blob/1d69722/packages/history-utility/src/history-utility.ts#L128)
diff --git a/packages/history-utility/src/helpers.ts b/packages/history-utility/src/helpers.ts
new file mode 100644
index 0000000..4cbc7f0
--- /dev/null
+++ b/packages/history-utility/src/helpers.ts
@@ -0,0 +1,9 @@
+const isProduction =
+ import.meta?.env?.MODE === 'production' ||
+ process?.env?.['NODE_ENV'] === 'production';
+
+export const warn = (...args: unknown[]) => {
+ if (!isProduction) {
+ console.warn(...args);
+ }
+};
diff --git a/packages/history-utility/src/history-utility.ts b/packages/history-utility/src/history-utility.ts
index 52c058c..160723d 100644
--- a/packages/history-utility/src/history-utility.ts
+++ b/packages/history-utility/src/history-utility.ts
@@ -6,6 +6,7 @@ import {
subscribe,
} from 'valtio/vanilla';
import type { INTERNAL_Snapshot as Snapshot } from 'valtio/vanilla';
+import { warn } from './helpers';
export type HistoryNode = {
/**
@@ -68,6 +69,30 @@ const deepClone = (value: T): T => {
return baseObject;
};
+const normalizeOptions = (
+ options?: HistoryOptions | boolean
+): HistoryOptions => {
+ if (typeof options === 'boolean') {
+ warn(`The second parameter of 'proxyWithHistory' as boolean is deprecated and support for boolean will be removed
+ in the next major version. Please use the object syntax instead:
+
+ { skipSubscribe: boolean }
+ `);
+ return { skipSubscribe: options };
+ }
+
+ const defaultOptions = {
+ skipSubscribe: false,
+ };
+
+ if (!options) return defaultOptions;
+
+ return {
+ ...defaultOptions,
+ ...options,
+ };
+};
+
/**
* This creates a new proxy with history support (ProxyHistoryObject).
* It includes following main properties:
@@ -100,7 +125,11 @@ const deepClone = (value: T): T => {
* count: 1,
* })
*/
-export function proxyWithHistory(initialValue: V, options?: HistoryOptions) {
+export function proxyWithHistory(
+ initialValue: V,
+ options?: HistoryOptions | boolean
+) {
+ const utilOptions = normalizeOptions(options);
const proxyObject = proxy({
/**
* any value to be tracked (does not have to be an object)
@@ -201,7 +230,7 @@ export function proxyWithHistory(initialValue: V, options?: HistoryOptions) {
++proxyObject.history.index;
},
/**
- * a function to return true if history should be saved
+ * a function that returns true when the history should be updated
*
* @param ops - subscribeOps from subscribe callback
* @returns boolean
@@ -296,7 +325,7 @@ export function proxyWithHistory(initialValue: V, options?: HistoryOptions) {
proxyObject.saveHistory();
- if (!options?.skipSubscribe) {
+ if (!utilOptions.skipSubscribe) {
proxyObject.subscribe();
}
diff --git a/packages/history-utility/tsconfig.json b/packages/history-utility/tsconfig.json
index 3748580..e66af41 100644
--- a/packages/history-utility/tsconfig.json
+++ b/packages/history-utility/tsconfig.json
@@ -1,14 +1,13 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
- "module": "commonjs",
+ "module": "ES2020",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
- "noFallthroughCasesInSwitch": true,
- "jsx": "react-jsx"
+ "noFallthroughCasesInSwitch": true
},
"files": [],
"include": [],
diff --git a/packages/history-utility/tsconfig.lib.json b/packages/history-utility/tsconfig.lib.json
index 142cd8a..9077b43 100644
--- a/packages/history-utility/tsconfig.lib.json
+++ b/packages/history-utility/tsconfig.lib.json
@@ -3,7 +3,7 @@
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"declaration": true,
- "types": ["node"]
+ "types": ["node", "vite/client"]
},
"include": ["src/**/*.ts", "src/**/*.tsx"],
"exclude": [
diff --git a/packages/history-utility/tsconfig.spec.json b/packages/history-utility/tsconfig.spec.json
index 3c002c2..543310b 100644
--- a/packages/history-utility/tsconfig.spec.json
+++ b/packages/history-utility/tsconfig.spec.json
@@ -2,6 +2,7 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
+ "jsx": "react-jsx",
"types": [
"vitest/globals",
"vitest/importMeta",
diff --git a/tsconfig.base.json b/tsconfig.base.json
index 7ce7251..0486726 100644
--- a/tsconfig.base.json
+++ b/tsconfig.base.json
@@ -8,7 +8,7 @@
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
- "target": "es2015",
+ "target": "es2020",
"module": "esnext",
"lib": ["es2020", "dom"],
"skipLibCheck": true,