From 074715f9f08cfdc1d8f0542e1bb5cfaffe8ec078 Mon Sep 17 00:00:00 2001 From: Layton Whiteley Date: Tue, 9 Jan 2024 05:24:28 +0100 Subject: [PATCH] chore: continue support for boolean second param --- .github/workflows/ci.yml | 1 + packages/history-utility/README.md | 2 +- packages/history-utility/docs/modules.md | 18 +++++----- packages/history-utility/project.json | 2 +- packages/history-utility/src/helpers.ts | 9 +++++ .../history-utility/src/history-utility.ts | 35 +++++++++++++++++-- packages/history-utility/tsconfig.json | 2 +- packages/history-utility/tsconfig.lib.json | 2 +- packages/history-utility/tsconfig.spec.json | 1 + tsconfig.base.json | 2 +- 10 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 packages/history-utility/src/helpers.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2d56ed0..aeed23f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,3 +38,4 @@ jobs: - run: pnpm nx format:check - run: pnpm nx affected -t lint,test --parallel=3 + - run: pnpm nx run-many -t build --parallel=3 --projects=tag:publish diff --git a/packages/history-utility/README.md b/packages/history-utility/README.md index fc7bac5..7692124 100644 --- a/packages/history-utility/README.md +++ b/packages/history-utility/README.md @@ -47,7 +47,7 @@ export default function App() { } ``` -### Notable Breaking changes +### Notable changes - the `history` object has changes - `history.snapshots` is renamed to `history.nodes` diff --git a/packages/history-utility/docs/modules.md b/packages/history-utility/docs/modules.md index 21e234d..73a272e 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/7853d84/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/7853d84/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/7853d84/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/7853d84/packages/history-utility/src/history-utility.ts#L128) diff --git a/packages/history-utility/project.json b/packages/history-utility/project.json index fa50b49..c1f2e6b 100644 --- a/packages/history-utility/project.json +++ b/packages/history-utility/project.json @@ -37,7 +37,7 @@ "options": { "commands": [ "cd packages/history-utility && pnpm typedoc --plugin typedoc-plugin-markdown src/index.ts && rm docs/README.md", - "sleep 3s && pnpm nx format:write" + "sleep 5s && pnpm nx format:write" ] } } 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..dd04b77 100644 --- a/packages/history-utility/tsconfig.json +++ b/packages/history-utility/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "../../tsconfig.base.json", "compilerOptions": { - "module": "commonjs", + "module": "ES2020", "forceConsistentCasingInFileNames": true, "strict": true, "noImplicitOverride": true, 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,