summaryrefslogtreecommitdiff
path: root/node_modules/@jet/environment/util/rewindable-value.js
blob: 4d4744d92ff1abda218db381071eb17f4a46d459 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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