Skip to content

Commit

Permalink
Update to version 0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
David Maskasky committed Sep 16, 2024
1 parent 814e485 commit 391dcad
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
33 changes: 20 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# History

[jotai-history](https://jotai.org/docs/integrations/history) is a utility package for advanced state history management
[jotai-history](https://jotai.org/docs/extensions/history) is a utility package for advanced state history management.

## install

Expand All @@ -13,7 +13,7 @@ npm i jotai-history
### Signature

```ts
function withHistory<T>(targetAtom: Atom<T>, limit: number): Atom<T[]>
declare function withHistory<T>(targetAtom: Atom<T>, limit: number): Atom<T[]>
```

This function creates an atom that keeps a history of states for a given `targetAtom`. The `limit` parameter determines the maximum number of history states to keep.
Expand All @@ -23,14 +23,14 @@ The history atom tracks changes to the `targetAtom` and maintains a list of prev

### Usage

```tsx
```jsx
import { atom, useAtomValue, useSetAtom } from 'jotai'
import { withHistory } from 'jotai-history'
const countAtom = atom(0)
const countWithPrevious = withHistory(countAtom, 2)
export function CountComponent() {
function CountComponent() {
const [count, previousCount] = useAtomValue(countWithPrevious)
const setCount = useSetAtom(countAtom)
Expand All @@ -49,13 +49,16 @@ export function CountComponent() {
### Signature

```ts
type Undoable<T> = {
undo: Function
redo: Function
type Undoable = {
undo: () => void
redo: () => void
canUndo: boolean
canRedo: boolean
}
function withUndo<T>(targetAtom: PrimitiveAtom<T>, limit: number): Atom<Undoable>
declare function withUndo<T>(
targetAtom: WritableAtom<T, [T], any>,
limit: number,
): Atom<Undoable>
```

`withHistory` provides undo and redo capabilities for an atom. It keeps track of the value history of `targetAtom` and provides methods to move back and forth through that history.
Expand All @@ -69,15 +72,20 @@ The returned object includes:

### Usage

```tsx
```jsx
import { atom, useAtom, useAtomValue } from 'jotai'
import { withUndo } from 'jotai-history'
const counterAtom = atom(0)
const undoCounterAtom = withUndo(counterAtom, 5)
export function CounterComponent() {
const { undo, redo, canUndo, canRedo } = useAtomValue(undoCounterAtom)
function CounterComponent() {
const {
undo,
redo,
canUndo,
canRedo,
} = useAtomValue(undoCounterAtom)
const [value, setValue] = useAtom(counterAtom)
return (
Expand All @@ -95,8 +103,7 @@ export function CounterComponent() {
}
```

## Example
https://codesandbox.io/p/sandbox/musing-orla-g6qj3q?file=%2Fsrc%2FApp.tsx%3A10%2C23
<CodeSandbox id="g6qj3q" />

## Memory Management

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jotai-history",
"description": "👻⌛",
"version": "0.2.0",
"version": "0.2.1",
"author": "David Maskasky",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/withHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { Atom } from 'jotai/vanilla'
export function withHistory<T>(targetAtom: Atom<T>, limit: number) {
const refAtom = atom(
() => ({ history: [] as T[] }),
(get, _set) => () => void (get(refAtom).history.length = 0)
(get) => () => void (get(refAtom).history.length = 0)
)
refAtom.onMount = (mount) => mount()
refAtom.debugPrivate = true
Expand Down
7 changes: 5 additions & 2 deletions src/withUndo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { atom } from 'jotai/vanilla'
import type { PrimitiveAtom } from 'jotai/vanilla'
import type { WritableAtom } from 'jotai/vanilla'
import { withHistory } from './withHistory'

type Undoable = {
Expand All @@ -14,7 +14,10 @@ type Undoable = {
* @param limit the maximum number of history states to keep
* @returns an atom with undo/redo functionality
*/
export function withUndo<T>(targetAtom: PrimitiveAtom<T>, limit: number) {
export function withUndo<T>(
targetAtom: WritableAtom<T, [T], any>,
limit: number
) {
const historyAtom = withHistory(targetAtom, limit)
const UNDO = Symbol('undo')
const REDO = Symbol('redo')
Expand Down

0 comments on commit 391dcad

Please sign in to comment.