UNPKG

fsmachine

Version:

> A simple and small TypeScript finite state machine

66 lines 2.56 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.dispatcherAsync = exports.dispatcher = void 0; const types_1 = require("./types"); function dispatcher(init, transitions, opts) { let state = init; const getState = () => state; const invalid = (ev) => { if (opts.onInvalid) { opts.onInvalid(state, ev); } if (opts.throw === false) return false; throw new types_1.InvalidTransition(ev, state); }; const dispatch = (ev) => { const fromMap = transitions.get(ev); if (!fromMap) return invalid(ev); const transition = fromMap.get(state); if (!transition) return invalid(ev); const from = state; state = transition.to; transition.cb(from, ev, transition.to); return true; }; return { dispatch, getState }; } exports.dispatcher = dispatcher; function dispatcherAsync(init, transitions, opts) { let state = init; const getState = () => state; const invalid = (ev) => { if (opts.onInvalid) { opts.onInvalid(state, ev); } if (opts.throw === false) return false; throw new types_1.InvalidTransition(ev, state); }; const dispatch = (ev) => __awaiter(this, void 0, void 0, function* () { const fromMap = transitions.get(ev); if (!fromMap) return invalid(ev); const transition = fromMap.get(state); if (!transition) return invalid(ev); const from = state; state = transition.to; yield transition.cb(from, ev, transition.to); return true; }); return { dispatch, getState }; } exports.dispatcherAsync = dispatcherAsync; //# sourceMappingURL=dispatch.js.map