this repo has no description
at main 58 lines 1.6 kB view raw
1"use strict"; 2Object.defineProperty(exports, "__esModule", { value: true }); 3exports.RewindableValue = void 0; 4const clone_1 = require("./clone"); 5/* eslint-disable no-underscore-dangle */ 6/** 7 * A lightweight wrapper around a primitive value which allows its state 8 * to saved and later restored. 9 */ 10class RewindableValue { 11 /** 12 * Create a rewindable value. 13 * 14 * @param initialValue - The initial value for the new instance. 15 */ 16 constructor(initialValue) { 17 this._values = [initialValue]; 18 } 19 /** 20 * Returns the current value of this instance. 21 */ 22 get() { 23 return this._values[this._values.length - 1]; 24 } 25 /** 26 * Replace the current value of this instance. 27 * 28 * @param newValue - The value to assign this instance. 29 */ 30 set(newValue) { 31 this._values[this._values.length - 1] = newValue; 32 } 33 /** 34 * Save the current state of this value. 35 * 36 * Calls to this method should be balanced by calls to `restore`. 37 */ 38 save() { 39 this._values.push(this._values[this._values.length - 1]); 40 } 41 /** 42 * Restore a previously saved value. 43 */ 44 restore() { 45 if (this._values.length === 1) { 46 throw new RangeError("Calls to restore must balance previous calls to save"); 47 } 48 this._values.pop(); 49 } 50 // section Cloneable 51 clone() { 52 const copy = (0, clone_1.shallowCloneOf)(this); 53 copy._values = this._values.slice(); 54 return copy; 55 } 56} 57exports.RewindableValue = RewindableValue; 58//# sourceMappingURL=rewindable-value.js.map