diff options
| author | rxliuli <rxliuli@gmail.com> | 2025-11-04 05:03:50 +0800 |
|---|---|---|
| committer | rxliuli <rxliuli@gmail.com> | 2025-11-04 05:03:50 +0800 |
| commit | bce557cc2dc767628bed6aac87301a1be7c5431b (patch) | |
| tree | b51a051228d01fe3306cd7626d4a96768aadb944 /node_modules/@jet/environment/util/rewindable-value.js | |
init commit
Diffstat (limited to 'node_modules/@jet/environment/util/rewindable-value.js')
| -rw-r--r-- | node_modules/@jet/environment/util/rewindable-value.js | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/node_modules/@jet/environment/util/rewindable-value.js b/node_modules/@jet/environment/util/rewindable-value.js new file mode 100644 index 0000000..4d4744d --- /dev/null +++ b/node_modules/@jet/environment/util/rewindable-value.js @@ -0,0 +1,58 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.RewindableValue = void 0; +const clone_1 = require("./clone"); +/* eslint-disable no-underscore-dangle */ +/** + * A lightweight wrapper around a primitive value which allows its state + * to saved and later restored. + */ +class RewindableValue { + /** + * Create a rewindable value. + * + * @param initialValue - The initial value for the new instance. + */ + constructor(initialValue) { + this._values = [initialValue]; + } + /** + * Returns the current value of this instance. + */ + get() { + return this._values[this._values.length - 1]; + } + /** + * Replace the current value of this instance. + * + * @param newValue - The value to assign this instance. + */ + set(newValue) { + this._values[this._values.length - 1] = newValue; + } + /** + * Save the current state of this value. + * + * Calls to this method should be balanced by calls to `restore`. + */ + save() { + this._values.push(this._values[this._values.length - 1]); + } + /** + * Restore a previously saved value. + */ + restore() { + if (this._values.length === 1) { + throw new RangeError("Calls to restore must balance previous calls to save"); + } + this._values.pop(); + } + // section Cloneable + clone() { + const copy = (0, clone_1.shallowCloneOf)(this); + copy._values = this._values.slice(); + return copy; + } +} +exports.RewindableValue = RewindableValue; +//# sourceMappingURL=rewindable-value.js.map
\ No newline at end of file |
