Skip to content

Commit

Permalink
cleanup for v2 (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
charkour committed Sep 24, 2022
1 parent bfbcd65 commit 2f13241
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
vi.mock('zustand/vanilla');
import { temporal } from '../../index';
import { temporal } from '../src/index';
import createVanilla, { StoreApi } from 'zustand/vanilla';
import { act } from 'react-dom/test-utils';
import shallow from 'zustand/shallow';
import { TemporalStateWithInternals, ZundoOptions } from '../../temporal';
import throttle from 'lodash.throttle';
import { TemporalState, Write } from '../../types';
import type {
TemporalStateWithInternals,
ZundoOptions,
TemporalState,
Write,
} from '../src/types';
import throttle from '../node_modules/lodash.throttle';

interface MyState {
count: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect, vi } from 'vitest';
vi.mock('zustand/vanilla');
import { createVanillaTemporal } from '../../temporal';
import { createVanillaTemporal } from '../src/temporal';
import createVanilla from 'zustand/vanilla';
import { act } from 'react-dom/test-utils';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
vi.mock('zustand/vanilla');
import { temporal } from '../../index';
import createVanilla, { StoreApi } from 'zustand/vanilla';
import { temporal } from '../src/index';
import createVanilla, { type StoreApi } from 'zustand/vanilla';
import { act } from 'react-dom/test-utils';
import { TemporalState, Write } from '../../types';
import type { TemporalState, Write } from '../src/types';

interface MyState {
count: number;
Expand Down
14 changes: 9 additions & 5 deletions packages/zundo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
"description": "🍜 undo/redo middleware for zustand",
"keywords": [
"undo",
"redo",
"history",
"middleware",
"zustand"
"zustand",
"react"
],
"homepage": "https://github.com/charkour/zundo",
"bugs": {
Expand All @@ -17,21 +19,23 @@
"type": "git",
"url": "git+https://github.com/charkour/zundo.git"
},
"funding": {
"type": "individual",
"url": "https://github.com/sponsors/charkour"
},
"license": "MIT",
"author": "Charles Kornoelje",
"main": "dist/index.js",
"module": "dist/zundo.esm.js",
"typings": "dist/index.d.ts",
"files": [
"dist",
"src",
"package.json"
],
"scripts": {
"build": "tsup",
"test": "vitest",
"dev": "vitest --watch",
"size": "size-limit"
"size": "size-limit",
"test": "vitest"
},
"devDependencies": {
"@size-limit/preset-small-lib": "^8.0.1",
Expand Down
14 changes: 6 additions & 8 deletions packages/zundo/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import {
import type {
StateCreator,
StoreMutatorIdentifier,
Mutate,
StoreApi,
} from 'zustand';
import {
createVanillaTemporal,
ZundoOptions,
} from './temporal';
import { PopArgument, TemporalState, Write } from './types';

import { createVanillaTemporal } from './temporal';
import type { PopArgument, TemporalState, Write, ZundoOptions } from './types';

type Zundo = <
TState,
Expand Down Expand Up @@ -53,14 +49,16 @@ const zundoImpl: ZundoImpl = (config, baseOptions) => (set, get, _store) => {
StoreApi<TState>,
[['temporal', StoreAddition]]
>;
// TODO: should temporal be only temporalStore.getState()?
// We can hide the rest of the store in the secret internals.
store.temporal = temporalStore;

const curriedUserLandSet = userlandSetFactory(
temporalStore.getState().__internal.handleUserSet,
);

const modifiedSetter: typeof set = (state, replace) => {
// TODO: get() can eventually be replaced with the state in the callback
// Get most up to date state. Should this be the same as the state in the callback?
const pastState = partialize(get());
set(state, replace);
curriedUserLandSet(pastState);
Expand Down
36 changes: 2 additions & 34 deletions packages/zundo/src/temporal.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,5 @@
import createVanilla, { StoreApi } from 'zustand/vanilla';

type onSave<State> = (pastState: State, currentState: State) => void;

export interface TemporalStateWithInternals<TState> {
pastStates: TState[];
futureStates: TState[];

undo: (steps?: number) => void;
redo: (steps?: number) => void;
clear: () => void;

trackingState: 'paused' | 'tracking';
pause: () => void;
resume: () => void;

setOnSave: (onSave: onSave<TState>) => void;
__internal: {
onSave: onSave<TState>;
handleUserSet: (pastState: TState) => void;
};
}

export interface ZundoOptions<State, TemporalState = State> {
partialize?: (state: State) => TemporalState;
limit?: number;
equality?: (a: State, b: State) => boolean;
/* called when saved */
onSave?: onSave<State>;
/* Middleware for the temporal setter */
handleSet?: (
handleSet: StoreApi<State>['setState'],
) => StoreApi<State>['setState'];
}
import createVanilla, { type StoreApi } from 'zustand/vanilla';
import type { TemporalStateWithInternals, ZundoOptions } from './types';

export const createVanillaTemporal = <TState>(
userSet: StoreApi<TState>['setState'],
Expand Down
10 changes: 0 additions & 10 deletions packages/zundo/src/testing/tsconfig.json

This file was deleted.

37 changes: 35 additions & 2 deletions packages/zundo/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@
import { TemporalStateWithInternals } from "./temporal";
import type { StoreApi } from 'zustand/vanilla';

type onSave<State> = (pastState: State, currentState: State) => void;

export interface TemporalStateWithInternals<TState> {
pastStates: TState[];
futureStates: TState[];

undo: (steps?: number) => void;
redo: (steps?: number) => void;
clear: () => void;

trackingState: 'paused' | 'tracking';
pause: () => void;
resume: () => void;

setOnSave: (onSave: onSave<TState>) => void;
__internal: {
onSave: onSave<TState>;
handleUserSet: (pastState: TState) => void;
};
}

export interface ZundoOptions<State, TemporalState = State> {
partialize?: (state: State) => TemporalState;
limit?: number;
equality?: (a: State, b: State) => boolean;
/* called when saved */
onSave?: onSave<State>;
/* Middleware for the temporal setter */
handleSet?: (
handleSet: StoreApi<State>['setState'],
) => StoreApi<State>['setState'];
}

export type PopArgument<T extends (...a: never[]) => unknown> = T extends (
...a: [...infer A, infer _]
Expand All @@ -11,4 +44,4 @@ export type Write<T, U> = Omit<T, keyof U> & U;
export type TemporalState<TState> = Omit<
TemporalStateWithInternals<TState>,
'__internal'
>;
>;

0 comments on commit 2f13241

Please sign in to comment.