deth
Version:
Ethereum node focused on Developer Experience
26 lines (25 loc) • 721 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Object that can be snapshotted ("saved") any time and then reverted to a given state.
*/
class SnapshotObject {
constructor(value, copyFn) {
this.value = value;
this.copyFn = copyFn;
this.snapshots = [];
}
makeSnapshot() {
const copy = this.copyFn(this.value);
const id = this.makeSnapshot.length;
this.snapshots.push(copy);
return id;
}
revert(id) {
if (this.makeSnapshot.length < id) {
throw new Error(`Snapshot id ${id} doesn't exist`);
}
this.value = this.snapshots[id];
}
}
exports.SnapshotObject = SnapshotObject;