UNPKG

@airgap/coinlib-core

Version:

The @airgap/coinlib-core is a protocol agnostic library to prepare, sign and broadcast cryptocurrency transactions.

42 lines 1.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StateMachine = void 0; var errors_1 = require("../errors"); var coinlib_error_1 = require("../errors/coinlib-error"); var StateMachine = /** @class */ (function () { function StateMachine(initialState, validTransitions) { this.state = initialState; this.validTransitions = validTransitions; } StateMachine.prototype.transitionTo = function (state) { if (this.canTransitionTo(state)) { this.state = state; } else { throw new errors_1.OperationFailedError(coinlib_error_1.Domain.ACTIONS, "Invalid state transition: ".concat(this.state, " -> ").concat(state)); } }; StateMachine.prototype.getState = function () { return this.state; }; StateMachine.prototype.addValidStateTransition = function (from, to) { var states = this.validTransitions.get(to); if (states !== undefined && states.indexOf(from) === -1) { states.push(from); this.validTransitions.set(to, states); } else { this.validTransitions.set(to, [from]); } }; StateMachine.prototype.canTransitionTo = function (state) { var states = this.validTransitions.get(state); if (states !== undefined) { return states.indexOf(this.state) !== -1; } return false; }; return StateMachine; }()); exports.StateMachine = StateMachine; //# sourceMappingURL=StateMachine.js.map