UNPKG

deth

Version:

Ethereum node focused on Developer Experience

23 lines (22 loc) 610 B
/** * Object that can be snapshotted ("saved") any time and then reverted to a given state. */ export 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]; } }