1.0.0 • Published 6 months ago
@drainpixie/reki v1.0.0
🌳 rekishi 歴史
a lithe undostack
🖥️ use
$ pnpm add @drainpixie/rekiimport {
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 install1.0.0
6 months ago