Skip to content

Commit

Permalink
update readme for historydepthlimit
Browse files Browse the repository at this point in the history
  • Loading branch information
charkour committed Oct 24, 2021
1 parent 1e28d97 commit a0f7075
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const useStoreWithUndo = create<StoreState>(
### Middleware Options

```tsx
options: { omit: string[], allowUnchanged: boolean } = { omit: [], allowUnchanged: undefined }
options: { omit?: string[], allowUnchanged?: boolean, historyDepthLimit?: number }
```

#### **Omit fields from being tracked in history**
Expand Down Expand Up @@ -110,6 +110,19 @@ const useStore = create<StoreState>(
);
```

#### **Limit number of states stored**

For performance reasons, you may want to limit the number of previous and future states stored in history. Setting `historyDepthLimit` will limit the number of previous and future states stored in the `zundo` store. By default, no limit is set.

```tsx
const useStore = create<StoreState>(
undoMiddleware(
set => ({ ... }),
{ historyDepthLimit: 100 }
)
);
```

## API

### `undoMiddleware(config: StateCreator<TState>)`
Expand Down
7 changes: 2 additions & 5 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,11 @@ const handleStoreUpdates = (
const isUndo = action === 'undo';
const currentActionStates = isUndo ? prevStates : futureStates;
const otherActionStates = isUndo ? futureStates : prevStates;
const stateDepthLimit = isUndo
? // default to historyLimit if no future limit.
options?.futureDepthLimit || options?.historyDepthLimit
: options?.historyDepthLimit;
const limit = options?.historyDepthLimit;

if (currentActionStates.length > 0) {
// check history limit
if (stateDepthLimit && otherActionStates.length >= stateDepthLimit) {
if (limit && otherActionStates.length >= limit) {
// pop front
otherActionStates.shift();
}
Expand Down
1 change: 0 additions & 1 deletion src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export interface Options {
omit?: string[];
allowUnchanged?: boolean;
historyDepthLimit?: number;
futureDepthLimit?: number;
}

// custom zustand middleware to get previous state
Expand Down

0 comments on commit a0f7075

Please sign in to comment.