Skip to content

Commit

Permalink
feat: add history rewrite utility methods (#2)
Browse files Browse the repository at this point in the history
* feat: add remove utility method

The remove function is used to remove a specified history index

* feat: add replace utility function

the replace method is a function to replace a snapshot at a specified history index

* docs: generate api markdown for package
  • Loading branch information
lwhiteley committed Jan 8, 2024
1 parent 52f75f8 commit 25c3b7c
Show file tree
Hide file tree
Showing 10 changed files with 2,797 additions and 6,030 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ https://valtio.pmnd.rs/docs/api/utils/proxyWithHistory

### Main Packages

| Name | Info |
| ------------------------------------------ | ------------------------------------------------------------------------------------------------------ |
| [valtio-history](packages/history-utility) | [![npm version](https://badge.fury.io/js/valtio-history.svg)](https://badge.fury.io/js/valtio-history) |
| Name | Docs | Badges |
| ------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
| [valtio-history](packages/history-utility) | [website](https://valtio.pmnd.rs/docs/api/utils/proxyWithHistory) <br/> [api](packages/history-utility/docs/modules.md) | [![npm version](https://badge.fury.io/js/valtio-history.svg)](https://badge.fury.io/js/valtio-history) |

---

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
"prettier": "^2.6.2",
"release-it": "^17.0.1",
"ts-node": "10.9.1",
"typedoc": "^0.25.6",
"typedoc-plugin-markdown": "^3.17.1",
"typescript": "~5.2.2",
"url-loader": "^4.1.1",
"valtio": "^1.12.1",
Expand Down
7 changes: 5 additions & 2 deletions packages/history-utility/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export default function App() {
history,
canUndo,
canRedo,
getCurrentChangeDate
getCurrentChangeDate,
getNode,
remove,
replace,
} = useSnapshot(state);

...
Expand All @@ -46,4 +49,4 @@ export default function App() {

- the `history` object has changes
- `history.snapshots` is renamed to `history.nodes`
- a `HistoryNode` has the structure `{ createdAt: Date; snapshot: Snapshot<T> }`
- a `HistoryNode` has the structure `{ snapshot: Snapshot<T>; createdAt: Date; updatedAt?: Date; }`
1 change: 1 addition & 0 deletions packages/history-utility/docs/.nojekyll
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TypeDoc added this file to prevent GitHub Pages from using Jekyll. You can turn off this behavior by setting the `githubPages` option to false.
138 changes: 138 additions & 0 deletions packages/history-utility/docs/modules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
[valtio-history](README.md) / Exports

# valtio-history

## Table of contents

### Type Aliases

- [History](modules.md#history)
- [HistoryNode](modules.md#historynode)

### Functions

- [proxyWithHistory](modules.md#proxywithhistory)

## Type Aliases

### History

Ƭ **History**\<`T`\>: `Object`

#### Type parameters

| Name |
| :--- |
| `T` |

#### Type declaration

| Name | Type | Description |
| :------ | :----------------------------------------------- | :-------------------------------------------------------------- |
| `index` | `number` | the history index of the current snapshot |
| `nodes` | [`HistoryNode`](modules.md#historynode)\<`T`\>[] | the nodes of the history for each change |
| `wip?` | `Snapshot`\<`T`\> | field for holding sandbox changes; used to avoid infinite loops |

#### Defined in

[packages/history-utility/src/history-utility.ts:26](https://github.com/valtiojs/valtio-history/blob/3130a40/packages/history-utility/src/history-utility.ts#L26)

---

### HistoryNode

Ƭ **HistoryNode**\<`T`\>: `Object`

#### Type parameters

| Name |
| :--- |
| `T` |

#### Type declaration

| Name | Type | Description |
| :----------- | :---------------- | :----------------------------------------------------------------------------------- |
| `createdAt` | `Date` | The date when the node was created |
| `snapshot` | `Snapshot`\<`T`\> | The snapshot being tracked |
| `updatedAt?` | `Date` | The date when the node was updated. Will be undefined if the node was never updated. |

#### Defined in

[packages/history-utility/src/history-utility.ts:10](https://github.com/valtiojs/valtio-history/blob/3130a40/packages/history-utility/src/history-utility.ts#L10)

## Functions

### proxyWithHistory

**proxyWithHistory**\<`V`\>(`initialValue`, `skipSubscribe?`): `Object`

This creates a new proxy with history support (ProxyHistoryObject).
It includes following main properties:<br>

- value: any value (does not have to be an object)<br>
- history: an object holding the history of snapshots and other metadata<br>
- history.index: the history index of the current snapshot<br>
- history.nodes: the nodes of the history for each change<br>
- history.wip: field for holding sandbox changes; used to avoid infinite loops<br>
- canUndo: a function to return true if undo is available <br>
- undo: a function to go back history <br>
- canRedo: a function to return true if redo is available <br>
- redo: a function to go forward history <br>
- saveHistory: a function to save history <br>
- getCurrentChangeDate: gets the date of the current change <br>
- remove: a function to remove a specified history index <br>
- replace: a function to replace a snapshot at a specified history index <br>
- getNode: a function to get the node at a specified history index <br>

<br>
Notes: <br>
- Suspense/promise is not supported. <br>

#### Type parameters

| Name |
| :--- |
| `V` |

#### Parameters

| Name | Type | Default value | Description |
| :-------------- | :-------- | :------------ | :---------------------------------------------------------------- |
| `initialValue` | `V` | `undefined` | any object to track |
| `skipSubscribe` | `boolean` | `false` | determines if the internal subscribe behaviour should be skipped. |

#### Returns

`Object`

proxyObject

| Name | Type | Description |
| :--------------------- | :-------------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `canRedo` | () => `boolean` | a function to return true if redo is available |
| `canUndo` | () => `boolean` | a function to return true if undo is available |
| `clone` | \<T\>(`value`: `T`) => `T` | utility to clone a snapshot |
| `getCurrentChangeDate` | () => `undefined` \| `Date` | get the date when a node was entered into history. |
| `getNode` | (`index`: `number`) => `undefined` \| \{ `createdAt`: `Date` ; `snapshot`: `Snapshot`\<`V`\> ; `updatedAt?`: `Date` } | utility method to get a history node. The snapshot within this node is already cloned and will not affect the original value if updated. |
| `history` | [`History`](modules.md#history)\<`V`\> & `AsRef` | an object holding the history of snapshots and other metadata <br> - history.index: the history index to the current snapshot <br> - history.nodes: the nodes of the history for each change <br> - history.wip: field for holding sandbox changes; used to avoid infinite loops<br> |
| `redo` | () => `void` | a function to go forward in history |
| `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` |
| `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) |

**`Example`**

```ts
import { proxyWithHistory } from 'valtio-history';
const state = proxyWithHistory({
count: 1,
});
```

#### Defined in

[packages/history-utility/src/history-utility.ts:94](https://github.com/valtiojs/valtio-history/blob/3130a40/packages/history-utility/src/history-utility.ts#L94)
9 changes: 9 additions & 0 deletions packages/history-utility/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
"options": {
"reportsDirectory": "../../coverage/packages/history-utility"
}
},
"docs": {
"executor": "nx:run-commands",
"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"
]
}
}
},
"tags": ["publish"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { describe, it } from 'vitest';

import { proxyWithHistory } from '../history-utility';

describe('proxyWithHistory', () => {
describe('react: basic', () => {
describe('proxyWithHistory: react', () => {
describe('basic', () => {
it('should do simple count', async () => {
const state = proxyWithHistory(0);

Expand Down
Loading

0 comments on commit 25c3b7c

Please sign in to comment.