chore: remove dest from repo

This commit is contained in:
Dmitriy Pleshevskiy 2021-08-20 11:13:30 +03:00
parent 3218ca46cb
commit e52190ff42
3 changed files with 2 additions and 190 deletions

2
.gitignore vendored
View file

@ -2,3 +2,5 @@ cov_profile/
node_modules/
package-lock.json
dest/

46
dest/fsm.d.ts vendored
View file

@ -1,46 +0,0 @@
declare type StateTransitions<Context> = WeakMap<State<Context>, WeakSet<State<Context>>>;
export declare const _states: unique symbol;
export declare const _stateTransitions: unique symbol;
export declare const _prevState: unique symbol;
export declare const _currState: unique symbol;
export declare class StateMachineBuilder<Context> {
[_states]: Map<string, Actions<Context>>;
[_stateTransitions]: Array<[string, Array<string>]> | undefined;
constructor();
withTransitions(transitions: Array<[string, Array<string>]>): this;
withStates(names: string[], actions?: Actions<Context>): this;
withState(name: string, actions?: Actions<Context>): this;
private addStateUnchecked;
build(currentStateName: string): StateMachine<unknown>;
private buildStates;
private buildTransitions;
}
export declare class StateMachine<Context> {
[_states]: State<Context>[];
[_stateTransitions]: StateTransitions<Context>;
[_prevState]: State<Context> | undefined;
[_currState]: State<Context>;
constructor(states: State<Context>[], transitions: StateTransitions<Context>, currentState: State<Context>);
changeState(sourceState: string | State<Context>, context?: Context): Promise<void>;
hasTransition(to: string | State<Context>): boolean;
allowedTransitionStates(): State<Context>[];
}
declare const _stateName: unique symbol;
declare const _stateActions: unique symbol;
interface Actions<Context> {
beforeExit?(fromState: State<Context>, toState: State<Context>, context: Context): boolean;
onEntry?(fromState: State<Context>, toState: State<Context>, context: Context): Promise<void> | void;
}
export declare class State<Context> {
[_stateActions]: Actions<Context>;
[_stateName]: string;
get name(): string;
constructor(name: string, actions?: Actions<Context>);
entry(fromState: State<Context>, toState: State<Context>, context: Context): Promise<void>;
exit(fromState: State<Context>, toState: State<Context>, context: Context): boolean;
toString(): string;
toJSON(): string;
}
export declare class FsmError extends Error {
}
export {};

View file

@ -1,144 +0,0 @@
"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.FsmError = exports.State = exports.StateMachine = exports.StateMachineBuilder = exports._currState = exports._prevState = exports._stateTransitions = exports._states = void 0;
exports._states = Symbol("states");
exports._stateTransitions = Symbol("state transitions");
exports._prevState = Symbol("previous state");
exports._currState = Symbol("current state");
class StateMachineBuilder {
constructor() {
this[exports._states] = new Map();
}
withTransitions(transitions) {
this[exports._stateTransitions] = transitions;
return this;
}
withStates(names, actions) {
names.forEach((name) => this.addStateUnchecked(name, actions));
return this;
}
withState(name, actions) {
this.addStateUnchecked(name, actions);
return this;
}
addStateUnchecked(name, actions) {
const oldActions = this[exports._states].get(name);
return this[exports._states].set(name, Object.assign(Object.assign({}, oldActions), actions));
}
build(currentStateName) {
const states = this.buildStates();
const transitions = this.buildTransitions(states);
const currState = validStateFromName(states, currentStateName);
return new StateMachine(states, transitions, currState);
}
buildStates() {
return Array.from(this[exports._states].entries()).map((params) => new State(...params));
}
buildTransitions(states) {
const sourceTransitions = this[exports._stateTransitions] || [];
return new WeakMap(sourceTransitions.map(([from, toStates]) => [
validStateFromName(states, from),
new WeakSet(toStates.map(validStateFromName.bind(null, states))),
]));
}
}
exports.StateMachineBuilder = StateMachineBuilder;
class StateMachine {
constructor(states, transitions, currentState) {
this[exports._states] = states;
this[exports._stateTransitions] = transitions;
this[exports._currState] = currentState;
}
changeState(sourceState, context) {
return __awaiter(this, void 0, void 0, function* () {
const fromState = validState(this[exports._currState]);
const toState = validState(normalizeState(this[exports._states], sourceState));
if (!this.hasTransition(toState) ||
!fromState.exit(fromState, toState, context)) {
throw new FsmError(`cannot change state from "${fromState.name}" to "${toState.name}"`);
}
yield toState.entry(fromState, toState, context);
this[exports._currState] = toState;
this[exports._prevState] = fromState;
});
}
hasTransition(to) {
return hasTransition(this[exports._stateTransitions], this[exports._currState], validState(normalizeState(this[exports._states], to)));
}
allowedTransitionStates() {
const fromState = validState(this[exports._currState]);
return this[exports._states].filter(hasTransition.bind(null, this[exports._stateTransitions], fromState));
}
}
exports.StateMachine = StateMachine;
const _stateName = Symbol("state name");
const _stateActions = Symbol("state actions");
class State {
constructor(name, actions = {}) {
this[_stateName] = name;
this[_stateActions] = actions;
}
get name() {
return this[_stateName];
}
entry(fromState, toState, context) {
return __awaiter(this, void 0, void 0, function* () {
const action = this[_stateActions].onEntry;
if (isFn(action)) {
yield action(fromState, toState, context);
}
});
}
exit(fromState, toState, context) {
const action = this[_stateActions].beforeExit;
return isFn(action) ? action(fromState, toState, context) : true;
}
toString() {
return this.name;
}
toJSON() {
return this.toString();
}
}
exports.State = State;
function stateFromName(states, name) {
return states.find((state) => state.name === name);
}
function validStateFromName(states, name) {
return validState(stateFromName(states, name));
}
function normalizeState(states, state) {
return isStr(state) ? stateFromName(states, state) : state;
}
function validState(val) {
if (!isState(val)) {
throw new TypeError("an instance of State class is expected");
}
return val;
}
function isState(val) {
return val instanceof State;
}
function hasTransition(transitions, from, to) {
var _a;
return ((_a = transitions.get(from)) === null || _a === void 0 ? void 0 : _a.has(to)) || false;
}
function isStr(val) {
return typeof val === "string";
}
// deno-lint-ignore ban-types
function isFn(val) {
return typeof val === "function";
}
class FsmError extends Error {
}
exports.FsmError = FsmError;