UNPKG

json-crdt-repo

Version:

JSON CRDT server and syncing local-first browser client

71 lines (70 loc) 1.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UndoRedoStack = void 0; const timeout_1 = require("thingies/lib/timeout"); const timeout2 = (ms, promise) => (0, timeout_1.timeout)(ms, promise).catch(() => undefined); class UndoRedoStack { constructor() { this.undoStack = []; this.redoStack = []; this.locked = false; } undoLength() { return this.undoStack.length; } redoLength() { return this.redoStack.length; } push(undo) { const redoStack = this.redoStack; this.redoStack = []; this.undoStack.push(undo); return redoStack; } async undo() { if (this.locked) return 0; this.locked = true; try { const undo = this.undoStack.pop(); if (!undo) return -1; let redo = undo.undo(); if (redo && typeof redo === 'object' && typeof redo.then === 'function') { redo = await timeout2(2000, Promise.resolve(redo)); if (!redo) return -2; } this.redoStack.push(redo); return 1; } finally { this.locked = false; } } async redo() { if (this.locked) return 0; this.locked = true; try { const redo = this.redoStack.pop(); if (!redo) return -1; let undo = redo.redo(); if (undo && typeof undo === 'object' && typeof undo.then === 'function') { undo = await timeout2(2000, Promise.resolve(undo)); if (!undo) return -2; } this.undoStack.push(undo); return 1; } catch { return -3; } finally { this.locked = false; } } } exports.UndoRedoStack = UndoRedoStack;