Skip to content

Commit

Permalink
rename atomWithHistory to withHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
David Maskasky committed Sep 5, 2024
1 parent cbbccd4 commit 814e485
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 25 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
npm i jotai-history
```

## atomWithHistory
## withHistory

### Signature

```ts
function atomWithHistory<T>(targetAtom: Atom<T>, limit: number): Atom<T[]>
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 @@ -25,10 +25,10 @@ The history atom tracks changes to the `targetAtom` and maintains a list of prev

```tsx
import { atom, useAtomValue, useSetAtom } from 'jotai'
import { atomWithHistory } from 'jotai/utils'
import { withHistory } from 'jotai-history'
const countAtom = atom(0)
const countWithPrevious = atomWithHistory(countAtom, 2)
const countWithPrevious = withHistory(countAtom, 2)
export function CountComponent() {
const [count, previousCount] = useAtomValue(countWithPrevious)
Expand All @@ -44,7 +44,7 @@ export function CountComponent() {
}
```

## atomWithUndo
## withUndo

### Signature

Expand All @@ -55,10 +55,10 @@ type Undoable<T> = {
canUndo: boolean
canRedo: boolean
}
function atomWithUndo<T>(targetAtom: PrimitiveAtom<T>, limit: number): Atom<Undoable>
function withUndo<T>(targetAtom: PrimitiveAtom<T>, limit: number): Atom<Undoable>
```

`atomWithHistory` 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.
`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.

The returned object includes:

Expand All @@ -71,10 +71,10 @@ The returned object includes:

```tsx
import { atom, useAtom, useAtomValue } from 'jotai'
import { atomWithUndo } from 'jotai/utils'
import { withUndo } from 'jotai-history'
const counterAtom = atom(0)
const undoCounterAtom = atomWithUndo(counterAtom, 5)
const undoCounterAtom = withUndo(counterAtom, 5)
export function CounterComponent() {
const { undo, redo, canUndo, canRedo } = useAtomValue(undoCounterAtom)
Expand All @@ -100,4 +100,4 @@ https://codesandbox.io/p/sandbox/musing-orla-g6qj3q?file=%2Fsrc%2FApp.tsx%3A10%2

## Memory Management

⚠️ Since `atomWithHistory` and `atomWithUndo` keeps a history of states, it's important to manage memory by setting a reasonable `limit`. Excessive history can lead to memory bloat, especially in applications with frequent state updates.
⚠️ Since `withHistory` and `withUndo` keeps a history of states, it's important to manage memory by setting a reasonable `limit`. Excessive history can lead to memory bloat, especially in applications with frequent state updates.
6 changes: 3 additions & 3 deletions __tests__/atomWithHistory.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { atom, createStore } from 'jotai/vanilla'
import type { Atom, PrimitiveAtom } from 'jotai/vanilla'
import { atomWithHistory } from '../src/atomWithHistory'
import { withHistory } from '../src/withHistory'

describe('atomWithHistory', () => {
describe('withHistory', () => {
let store: ReturnType<typeof createStore>
let baseAtom: PrimitiveAtom<number>
let historyAtom: Atom<number[]>
Expand All @@ -11,7 +11,7 @@ describe('atomWithHistory', () => {
beforeEach(() => {
store = createStore()
baseAtom = atom(0) // Initial value is 0
historyAtom = atomWithHistory(baseAtom, 3) // Limit history to 3 entries
historyAtom = withHistory(baseAtom, 3) // Limit history to 3 entries
unsub = store.sub(historyAtom, () => {}) // Subscribe to trigger onMount
})

Expand Down
8 changes: 4 additions & 4 deletions __tests__/atomWithUndo.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { atom, createStore } from 'jotai/vanilla'
import type { PrimitiveAtom } from 'jotai/vanilla'
import { atomWithUndo } from '../src/atomWithUndo'
import { withUndo } from '../src/withUndo'

describe('atomWithUndo', () => {
describe('withUndo', () => {
let store: ReturnType<typeof createStore>
let baseAtom: PrimitiveAtom<number>
let undoableAtom: ReturnType<typeof atomWithUndo<number>>
let undoableAtom: ReturnType<typeof withUndo<number>>
let unsub: () => void
const undoable = {
undo() {
Expand All @@ -25,7 +25,7 @@ describe('atomWithUndo', () => {
beforeEach(() => {
store = createStore()
baseAtom = atom(0)
undoableAtom = atomWithUndo(baseAtom, 3) // Limit history to 3 entries
undoableAtom = withUndo(baseAtom, 3) // Limit history to 3 entries
unsub = store.sub(undoableAtom, () => {}) // Subscribe to trigger onMount
})

Expand Down
4 changes: 2 additions & 2 deletions examples/01_typescript/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react'
import { useAtom, useAtomValue } from 'jotai/react'
import { atom } from 'jotai/vanilla'
import { atomWithUndo } from 'jotai-history'
import { withUndo } from 'jotai-history'

const searchTextAtom = atom('')
const undoRedoAtom = atomWithUndo<string>(searchTextAtom, 10)
const undoRedoAtom = withUndo<string>(searchTextAtom, 10)

function UndoRedoControls() {
const { undo, redo, canUndo, canRedo } = useAtomValue(undoRedoAtom)
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { atomWithHistory } from './atomWithHistory'
export { atomWithUndo } from './atomWithUndo'
export { withHistory } from './withHistory'
export { withUndo } from './withUndo'
2 changes: 1 addition & 1 deletion src/atomWithHistory.ts → src/withHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { Atom } from 'jotai/vanilla'
* @param limit the maximum number of history states to keep
* @returns an atom with an array of history states
*/
export function atomWithHistory<T>(targetAtom: Atom<T>, limit: number) {
export function withHistory<T>(targetAtom: Atom<T>, limit: number) {
const refAtom = atom(
() => ({ history: [] as T[] }),
(get, _set) => () => void (get(refAtom).history.length = 0)
Expand Down
6 changes: 3 additions & 3 deletions src/atomWithUndo.ts → src/withUndo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { atom } from 'jotai/vanilla'
import type { PrimitiveAtom } from 'jotai/vanilla'
import { atomWithHistory } from './atomWithHistory'
import { withHistory } from './withHistory'

type Undoable = {
undo: () => void
Expand All @@ -14,8 +14,8 @@ type Undoable = {
* @param limit the maximum number of history states to keep
* @returns an atom with undo/redo functionality
*/
export function atomWithUndo<T>(targetAtom: PrimitiveAtom<T>, limit: number) {
const historyAtom = atomWithHistory(targetAtom, limit)
export function withUndo<T>(targetAtom: PrimitiveAtom<T>, limit: number) {
const historyAtom = withHistory(targetAtom, limit)
const UNDO = Symbol('undo')
const REDO = Symbol('redo')
type DoAction = typeof UNDO | typeof REDO
Expand Down

0 comments on commit 814e485

Please sign in to comment.