1.0.0 • Published 6 months ago

@drainpixie/reki v1.0.0

Weekly downloads
-
License
LGPL-3.0
Repository
-
Last release
6 months ago

🌳 rekishi 歴史

a lithe undostack

🖥️ use

$ pnpm add @drainpixie/reki
import {
  Command,
  createStack,
  execute,
  undo,
  redo,
  clear,
  can,
  find,
} from "@drainpixie/reki";

interface Counter {
  value: number;
}

const increment = (amount: number): Command<Counter> => ({
  execute: () => ({ value: state.current.value + amount }),
  undo: () => ({ value: state.current.value - amount }),

  name: `increment @ ${amount}`,
});

const initial = { value: 0 };
let state = createStack<Counter>(initial, { maxStackSize: 10 });

state = execute(state, increment(1));
state = execute(state, increment(2));
state = execute(state, increment(3));

console.log(state.current.value); // -> 6
state = undo(state);

console.log(state.current.value); // -> 3
state = undo(state);

console.log(state.current.value); // -> 1
state = redo(state);
console.log(state.current.value); // -> 3

state = clear(state);
console.log(can.undo(state), can.redo(state), state.current.value); // -> false false 3

// NOTE: We do not handle your value, `clear` only clears the stack[s]
state.current = initial;

🖥️ dev

nix develop
pnpm install
1.0.0

6 months ago