UNPKG

timewarp-sim

Version:

🕰️ Deterministic time simulation and manipulation library for TypeScript

44 lines (43 loc) 1.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TimeController = void 0; const TimeState_1 = require("./TimeState"); const TimeHooks_1 = require("./TimeHooks"); class TimeController { constructor() { this.state = new TimeState_1.TimeState(); this.hooks = new TimeHooks_1.TimeHooks(); } now() { return new Date(this.state.now); } freeze() { this.state.freeze(); this.hooks.notifyAll(this.state.now); } unfreeze() { this.state.unfreeze(); this.hooks.notifyAll(Date.now()); } travelTo(date) { this.state.setTime(date.getTime()); this.hooks.notifyAll(this.state.now); } advance(ms) { this.state.advance(ms); this.hooks.notifyAll(this.state.now); } isFrozen() { return this.state.isFrozen(); } onTimeChange(callback) { this.hooks.onTimeChange(callback); } removeTimeChangeListener(callback) { this.hooks.removeListener(callback); } nowTimestamp() { return this.state.now; } } exports.TimeController = TimeController;