Skip to content

Commit

Permalink
chore: continue support for boolean second param
Browse files Browse the repository at this point in the history
  • Loading branch information
lwhiteley committed Jan 9, 2024
1 parent a1e6061 commit 074715f
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 17 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion packages/history-utility/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
18 changes: 9 additions & 9 deletions packages/history-utility/docs/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

---

Expand All @@ -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)

---

Expand All @@ -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

Expand Down Expand Up @@ -114,10 +114,10 @@ Notes: <br>

#### 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

Expand All @@ -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. <br> <br> Notes: <br> - No operations are done on the value provided to this utility. <br> - This is an advanced method, please ensure the value provided is a snapshot of the same type of the value being tracked. <br> |
| `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) |
Expand All @@ -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)
2 changes: 1 addition & 1 deletion packages/history-utility/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
}
Expand Down
9 changes: 9 additions & 0 deletions packages/history-utility/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -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);
}
};
35 changes: 32 additions & 3 deletions packages/history-utility/src/history-utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = {
/**
Expand Down Expand Up @@ -68,6 +69,30 @@ const deepClone = <T>(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:<br>
Expand Down Expand Up @@ -100,7 +125,11 @@ const deepClone = <T>(value: T): T => {
* count: 1,
* })
*/
export function proxyWithHistory<V>(initialValue: V, options?: HistoryOptions) {
export function proxyWithHistory<V>(
initialValue: V,
options?: HistoryOptions | boolean
) {
const utilOptions = normalizeOptions(options);
const proxyObject = proxy({
/**
* any value to be tracked (does not have to be an object)
Expand Down Expand Up @@ -201,7 +230,7 @@ export function proxyWithHistory<V>(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
Expand Down Expand Up @@ -296,7 +325,7 @@ export function proxyWithHistory<V>(initialValue: V, options?: HistoryOptions) {

proxyObject.saveHistory();

if (!options?.skipSubscribe) {
if (!utilOptions.skipSubscribe) {
proxyObject.subscribe();
}

Expand Down
2 changes: 1 addition & 1 deletion packages/history-utility/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"module": "ES2020",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/history-utility/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"]
"types": ["node", "vite/client"]
},
"include": ["src/**/*.ts", "src/**/*.tsx"],
"exclude": [
Expand Down
1 change: 1 addition & 0 deletions packages/history-utility/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"jsx": "react-jsx",
"types": [
"vitest/globals",
"vitest/importMeta",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es2015",
"target": "es2020",
"module": "esnext",
"lib": ["es2020", "dom"],
"skipLibCheck": true,
Expand Down

0 comments on commit 074715f

Please sign in to comment.