UNPKG

jotai-history

Version:
43 lines • 1.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.withUndoableHistory = withUndoableHistory; const vanilla_1 = require("jotai/vanilla"); const actions_1 = require("./actions.js"); const withHistory_1 = require("./withHistory.js"); const withUndo_1 = require("./withUndo.js"); function withUndoableHistory(targetAtom, limit) { var _a; const historyAtom = withPrivate((0, withHistory_1.withHistory)(targetAtom, limit)); let undoAtom; if (isWritableAtom(targetAtom)) { // eslint-disable-next-line prefer-rest-params const getArgs = (_a = arguments[2]) !== null && _a !== void 0 ? _a : Array.of; undoAtom = withPrivate((0, withUndo_1.withUndo)(historyAtom, targetAtom, limit, getArgs)); } return (0, vanilla_1.atom)((get) => Object.assign(get(historyAtom), isWritableAtom(targetAtom) ? get(undoAtom) : {}), (_, set, ...args) => { const [action] = args; if (action === actions_1.RESET) { set(historyAtom, action); if (undoAtom) { set(undoAtom, action); } } else if (!isWritableAtom(targetAtom)) { return; } else if (action === actions_1.UNDO || action === actions_1.REDO) { set(undoAtom, action); } else { return set(targetAtom, ...args); } }); } function isWritableAtom(atom) { return 'write' in atom; } function withPrivate(atom) { atom.debugPrivate = true; return atom; } //# sourceMappingURL=withUndoableHistory.js.map