Skip to content

Commit

Permalink
Update to version 1.0.1
Browse files Browse the repository at this point in the history
TransitionOnBefore functions returing false will fail the state change
  • Loading branch information
StefanoBalocco committed Jul 23, 2021
1 parent 3443122 commit 23b4004
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 97 deletions.
12 changes: 7 additions & 5 deletions jCFSM.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
declare namespace jCFSM {
export type FunctionOnEnter = (currentState: string, nextState: string) => (void | Promise<void>);
export type FunctionOnLeave = (currentState: string, prevState: string) => (void | Promise<void>);
export type FunctionOnTransition = () => (void | Promise<void>);
export type FunctionOnTransitionAfter = () => (void | Promise<void>);
export type FunctionOnTransitionBefore = () => (any | Promise<any>);
class StateMachine {
private _inTransition;
private _currentState;
private _states;
private _transitions;
Expand All @@ -15,10 +17,10 @@ declare namespace jCFSM {
StateOnLeaveDel(state: string, func: FunctionOnLeave): boolean;
TransitionAdd(from: string, to: string): boolean;
TransitionDel(from: string, to: string): boolean;
TransitionOnBeforeAdd(from: string, to: string, func: FunctionOnTransition): boolean;
TransitionOnBeforeDel(from: string, to: string, func: FunctionOnTransition): boolean;
TransitionOnAfterAdd(from: string, to: string, func: FunctionOnTransition): boolean;
TransitionOnAfterDel(from: string, to: string, func: FunctionOnTransition): boolean;
TransitionOnBeforeAdd(from: string, to: string, func: FunctionOnTransitionBefore): boolean;
TransitionOnBeforeDel(from: string, to: string, func: FunctionOnTransitionBefore): boolean;
TransitionOnAfterAdd(from: string, to: string, func: FunctionOnTransitionAfter): boolean;
TransitionOnAfterDel(from: string, to: string, func: FunctionOnTransitionAfter): boolean;
StateGet(): string;
StateSet(nextState: string): Promise<boolean>;
}
Expand Down
99 changes: 54 additions & 45 deletions jCFSM.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';
"use strict";
var jCFSM;
(function (jCFSM) {
class StateMachine {
constructor(initialState) {
this._inTransition = false;
this._states = {};
this._transitions = {};
this.StateAdd(initialState);
Expand Down Expand Up @@ -152,57 +153,65 @@ var jCFSM;
}
async StateSet(nextState) {
let returnValue = false;
if ('undefined' !== typeof (this._states[nextState])) {
if (('undefined' !== typeof (this._transitions[this._currentState])) && ('undefined' !== typeof (this._transitions[this._currentState][nextState]))) {
returnValue = true;
let countFirstLevel;
countFirstLevel = this._states[this._currentState].OnLeave.length;
for (let indexFirstLevel = 0; indexFirstLevel < countFirstLevel; indexFirstLevel++) {
if ('function' === typeof (this._states[this._currentState].OnLeave[indexFirstLevel])) {
if ('AsyncFunction' === this._states[this._currentState].OnLeave[indexFirstLevel].constructor.name) {
await this._states[this._currentState].OnLeave[indexFirstLevel](this._currentState, nextState);
}
else {
this._states[this._currentState].OnLeave[indexFirstLevel](this._currentState, nextState);
}
}
}
countFirstLevel = this._transitions[this._currentState][nextState].OnBefore.length;
for (let indexFirstLevel = 0; indexFirstLevel < countFirstLevel; indexFirstLevel++) {
if ('function' === typeof (this._transitions[this._currentState][nextState].OnBefore[indexFirstLevel])) {
if ('AsyncFunction' === this._transitions[this._currentState][nextState].OnBefore[indexFirstLevel].constructor.name) {
await this._transitions[this._currentState][nextState].OnBefore[indexFirstLevel]();
}
else {
this._transitions[this._currentState][nextState].OnBefore[indexFirstLevel]();
if (!this._inTransition) {
this._inTransition = true;
if ('undefined' !== typeof (this._states[nextState])) {
if (('undefined' !== typeof (this._transitions[this._currentState])) && ('undefined' !== typeof (this._transitions[this._currentState][nextState]))) {
returnValue = true;
let countFL;
countFL = this._transitions[this._currentState][nextState].OnBefore.length;
for (let indexFL = 0; (returnValue && (indexFL < countFL)); indexFL++) {
if ('function' === typeof (this._transitions[this._currentState][nextState].OnBefore[indexFL])) {
let tmpValue = null;
if ('AsyncFunction' === this._transitions[this._currentState][nextState].OnBefore[indexFL].constructor.name) {
tmpValue = await this._transitions[this._currentState][nextState].OnBefore[indexFL]();
}
else {
tmpValue = this._transitions[this._currentState][nextState].OnBefore[indexFL]();
}
returnValue = (false !== tmpValue);
}
}
}
let previousState = this._currentState;
this._currentState = nextState;
countFirstLevel = this._transitions[previousState][this._currentState].OnAfter.length;
for (let indexFirstLevel = 0; indexFirstLevel < countFirstLevel; indexFirstLevel++) {
if ('function' === typeof (this._transitions[previousState][this._currentState].OnAfter[indexFirstLevel])) {
if ('AsyncFunction' === this._transitions[previousState][this._currentState].OnAfter[indexFirstLevel].constructor.name) {
await this._transitions[previousState][this._currentState].OnAfter[indexFirstLevel]();
}
else {
this._transitions[previousState][this._currentState].OnAfter[indexFirstLevel]();
if (returnValue) {
countFL = this._states[this._currentState].OnLeave.length;
for (let indexFL = 0; indexFL < countFL; indexFL++) {
if ('function' === typeof (this._states[this._currentState].OnLeave[indexFL])) {
if ('AsyncFunction' === this._states[this._currentState].OnLeave[indexFL].constructor.name) {
await this._states[this._currentState].OnLeave[indexFL](this._currentState, nextState);
}
else {
this._states[this._currentState].OnLeave[indexFL](this._currentState, nextState);
}
}
}
}
}
countFirstLevel = this._states[this._currentState].OnEnter.length;
for (let indexFirstLevel = 0; indexFirstLevel < countFirstLevel; indexFirstLevel++) {
if ('function' === typeof (this._states[this._currentState].OnEnter[indexFirstLevel])) {
if ('AsyncFunction' === this._states[this._currentState].OnEnter[indexFirstLevel].constructor.name) {
await this._states[this._currentState].OnEnter[indexFirstLevel](this._currentState, previousState);
let previousState = this._currentState;
this._currentState = nextState;
countFL = this._transitions[previousState][this._currentState].OnAfter.length;
for (let indexFL = 0; indexFL < countFL; indexFL++) {
if ('function' === typeof (this._transitions[previousState][this._currentState].OnAfter[indexFL])) {
if ('AsyncFunction' === this._transitions[previousState][this._currentState].OnAfter[indexFL].constructor.name) {
await this._transitions[previousState][this._currentState].OnAfter[indexFL]();
}
else {
this._transitions[previousState][this._currentState].OnAfter[indexFL]();
}
}
}
else {
this._states[this._currentState].OnEnter[indexFirstLevel](this._currentState, previousState);
countFL = this._states[this._currentState].OnEnter.length;
for (let indexFL = 0; indexFL < countFL; indexFL++) {
if ('function' === typeof (this._states[this._currentState].OnEnter[indexFL])) {
if ('AsyncFunction' === this._states[this._currentState].OnEnter[indexFL].constructor.name) {
await this._states[this._currentState].OnEnter[indexFL](this._currentState, previousState);
}
else {
this._states[this._currentState].OnEnter[indexFL](this._currentState, previousState);
}
}
}
}
}
}
this._inTransition = false;
}
return (returnValue);
}
Expand All @@ -212,6 +221,6 @@ var jCFSM;
}
jCFSM.Create = Create;
})(jCFSM || (jCFSM = {}));
if ('object' === typeof (exports)) {
if ('object' === typeof exports) {
exports.Create = jCFSM.Create;
}
2 changes: 1 addition & 1 deletion jCFSM.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 23b4004

Please sign in to comment.