@peter.naydenov/fsm-hub 1.0.6 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/package.json +10 -8
- package/src/{index.js → main.js} +8 -8
- package/src/methods/_callback.js +24 -18
- package/src/methods/_debugger.js +1 -1
- package/src/methods/_setTransitions.js +1 -1
- package/src/methods/addFsm.js +7 -8
- package/src/methods/addFunctions.js +1 -1
- package/src/methods/index.js +6 -8
- package/src/msg.js +1 -1
- package/test/01-basics.js +6 -9
- package/test/02-multipleSystem.js +3 -5
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peter.naydenov/fsm-hub",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "FSM orchestration and state-managment tool",
|
|
5
|
-
"main": "src/
|
|
5
|
+
"main": "src/main.js",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"scripts": {
|
|
7
8
|
"test": "mocha test",
|
|
8
|
-
"cover": "
|
|
9
|
+
"cover": "c8 mocha test",
|
|
10
|
+
"build": "vite build"
|
|
9
11
|
},
|
|
10
12
|
"author": "Peter Naydenov",
|
|
11
13
|
"license": "MIT",
|
|
@@ -14,10 +16,10 @@
|
|
|
14
16
|
"ask-for-promise": "1.3.1"
|
|
15
17
|
},
|
|
16
18
|
"devDependencies": {
|
|
17
|
-
"@peter.naydenov/fsm": "
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"
|
|
19
|
+
"@peter.naydenov/fsm": "4.0.1",
|
|
20
|
+
"c8": "^7.12.0",
|
|
21
|
+
"chai": "4.3.7",
|
|
22
|
+
"mocha": "10.1.0"
|
|
21
23
|
},
|
|
22
24
|
"repository": {
|
|
23
25
|
"type": "git",
|
|
@@ -28,7 +30,7 @@
|
|
|
28
30
|
"state-managment",
|
|
29
31
|
"orchestration"
|
|
30
32
|
],
|
|
31
|
-
"
|
|
33
|
+
"c8": {
|
|
32
34
|
"include": [
|
|
33
35
|
"src/**/*.js"
|
|
34
36
|
],
|
package/src/{index.js → main.js}
RENAMED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
, stack = require ( '@peter.naydenov/stack' )
|
|
6
|
-
;
|
|
1
|
+
import methods from './methods/index.js'
|
|
2
|
+
import msg from './msg.js'
|
|
3
|
+
import askForPromise from 'ask-for-promise'
|
|
4
|
+
import stack from '@peter.naydenov/stack'
|
|
7
5
|
|
|
8
6
|
function FsmHub ( machine, transformerLib ) {
|
|
9
7
|
const
|
|
@@ -21,7 +19,9 @@ function FsmHub ( machine, transformerLib ) {
|
|
|
21
19
|
hub.reactivityTask = false
|
|
22
20
|
hub.askForPromise = askForPromise
|
|
23
21
|
|
|
24
|
-
fnKeys.forEach ( k =>
|
|
22
|
+
fnKeys.forEach ( k => { // Attach methods to fsmHub
|
|
23
|
+
hub[k] = methods[k]( hub, msg )
|
|
24
|
+
})
|
|
25
25
|
|
|
26
26
|
const { transformers, subscribers, actions, callbacks } = hub._setTransitions ( machine, transformerLib );
|
|
27
27
|
hub.transformers = transformers // Data transformation function with format 'fsm/fsmListener' or 'fsm/function'
|
|
@@ -32,6 +32,6 @@ function FsmHub ( machine, transformerLib ) {
|
|
|
32
32
|
|
|
33
33
|
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
export default FsmHub
|
|
36
36
|
|
|
37
37
|
|
package/src/methods/_callback.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
|
|
1
|
+
import askForPromise from "ask-for-promise"
|
|
2
2
|
|
|
3
3
|
function _callback ( hub, msg ) {
|
|
4
4
|
return function _callback ( task, fsmName, state, response ) {
|
|
5
5
|
const
|
|
6
6
|
itemKey = `${fsmName}/${state}`
|
|
7
|
-
, fsmSubscriber = hub.subscribers[itemKey] || false
|
|
8
|
-
, callbackNames = hub.callbacks[itemKey] || false
|
|
9
|
-
, fnCallbacks = hub.fnCallbacks
|
|
10
|
-
, act = hub.actions
|
|
11
|
-
, wait = []
|
|
7
|
+
, fsmSubscriber = hub.subscribers[itemKey] || false // names of subscribed FSM list
|
|
8
|
+
, callbackNames = hub.callbacks[itemKey] || false // names of subscribed functions list
|
|
9
|
+
, fnCallbacks = hub.fnCallbacks // List of available functions
|
|
10
|
+
, act = hub.actions // Collection of action names
|
|
11
|
+
, wait = [] // List of promises
|
|
12
12
|
;
|
|
13
13
|
let data;
|
|
14
|
-
|
|
15
|
-
if ( !fsmSubscriber && !callbackNames ) {
|
|
14
|
+
|
|
15
|
+
if ( !fsmSubscriber && !callbackNames ) { // Finish with callback if no subscribed FSM or callback functions
|
|
16
16
|
task.done ()
|
|
17
17
|
return
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
if ( fsmSubscriber ) {
|
|
20
|
+
if ( fsmSubscriber ) { // Execute fsm rules
|
|
21
21
|
fsmSubscriber.forEach ( subscriberName => {
|
|
22
|
-
if ( !hub.fsm[ subscriberName ] )
|
|
22
|
+
if ( !hub.fsm[ subscriberName ] ) {
|
|
23
23
|
hub._debugger ( msg.MISSING_FSM, fsmSubscriber )
|
|
24
24
|
return
|
|
25
25
|
}
|
|
@@ -29,17 +29,21 @@ return function _callback ( task, fsmName, state, response ) {
|
|
|
29
29
|
, transformerKey = `${fsmName}/${subscriberName}`
|
|
30
30
|
, transformFn = hub.transformers [transformerKey]
|
|
31
31
|
;
|
|
32
|
+
|
|
32
33
|
if ( typeof transformFn == 'function' ) data = transformFn (state, response )
|
|
33
34
|
else data = response
|
|
34
|
-
|
|
35
|
-
if ( !data )
|
|
36
|
-
|
|
35
|
+
|
|
36
|
+
if ( !data ) {
|
|
37
|
+
data = {}
|
|
38
|
+
data.___internalFlag = true
|
|
39
|
+
}
|
|
40
|
+
|
|
37
41
|
const updatePromise = hub.fsm [ subscriberName ].update ( action, data );
|
|
38
42
|
wait.push ( updatePromise )
|
|
39
43
|
})
|
|
40
44
|
} // if fsmSubscriber
|
|
41
|
-
|
|
42
|
-
if ( callbackNames ) {
|
|
45
|
+
|
|
46
|
+
if ( callbackNames ) { // Execute functions rules
|
|
43
47
|
let startFunctions = askForPromise ();
|
|
44
48
|
wait.push ( startFunctions.promise )
|
|
45
49
|
|
|
@@ -49,8 +53,12 @@ return function _callback ( task, fsmName, state, response ) {
|
|
|
49
53
|
, transformerKey = `${fsmName}/${cbName}`
|
|
50
54
|
, transformFn = hub.transformers [ transformerKey ]
|
|
51
55
|
;
|
|
56
|
+
|
|
52
57
|
if ( typeof transformFn == 'function' ) data = transformFn (state, response )
|
|
53
58
|
else data = response
|
|
59
|
+
|
|
60
|
+
if ( data.answer && data.answer.hasOwnProperty ( '___internalFlag') ) data.answer = undefined
|
|
61
|
+
|
|
54
62
|
if ( typeof fn == 'function' ) fn(data)
|
|
55
63
|
else hub._debugger ( msg.MISSING_FN, cbName )
|
|
56
64
|
|
|
@@ -62,8 +70,6 @@ return function _callback ( task, fsmName, state, response ) {
|
|
|
62
70
|
|
|
63
71
|
|
|
64
72
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
module.exports = _callback
|
|
73
|
+
export default _callback
|
|
68
74
|
|
|
69
75
|
|
package/src/methods/_debugger.js
CHANGED
package/src/methods/addFsm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import askForPromise from "ask-for-promise"
|
|
2
2
|
|
|
3
3
|
function addFsm ( hub, msg ) {
|
|
4
4
|
return function ( ins ) {
|
|
@@ -28,10 +28,9 @@ return function ( ins ) {
|
|
|
28
28
|
hubUpdateWatcher ( name, state, response )
|
|
29
29
|
} // reactivityComplete func.
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
function hubUpdateWatcher (name,state,response) {
|
|
31
|
+
function hubUpdateWatcher ( name, state, response ) {
|
|
34
32
|
let executeReactivity;
|
|
33
|
+
|
|
35
34
|
if ( !hub.lock ) {
|
|
36
35
|
hub.lock = true
|
|
37
36
|
executeReactivity = askForPromise ()
|
|
@@ -40,17 +39,17 @@ return function ( ins ) {
|
|
|
40
39
|
return
|
|
41
40
|
}
|
|
42
41
|
|
|
43
|
-
if ( response
|
|
42
|
+
if ( response ) {
|
|
44
43
|
if ( hub.haveInternalRequest ) {
|
|
45
44
|
hub.cacheInternal.push ( [[ name, state, response ]])
|
|
46
45
|
return
|
|
47
|
-
|
|
46
|
+
}
|
|
48
47
|
let reactivityTask = askForPromise ();
|
|
49
48
|
reactivityTask.onComplete ( z => reactivityComplete ( true ) ) // complete of internal reaction
|
|
50
49
|
hub.haveInternalRequest = true;
|
|
51
50
|
hub._callback ( reactivityTask, name, state, response )
|
|
52
51
|
return
|
|
53
|
-
|
|
52
|
+
}
|
|
54
53
|
hub.cache.push ( [[ name, state, response ]])
|
|
55
54
|
} // hubUpdateWatcher func.
|
|
56
55
|
|
|
@@ -63,6 +62,6 @@ return function ( ins ) {
|
|
|
63
62
|
|
|
64
63
|
|
|
65
64
|
|
|
66
|
-
|
|
65
|
+
export default addFsm
|
|
67
66
|
|
|
68
67
|
|
package/src/methods/index.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
, _callback = require ( './_callback' )
|
|
1
|
+
import _setTransitions from './_setTransitions.js'
|
|
2
|
+
import _debugger from './_debugger.js'
|
|
3
|
+
import _callback from './_callback.js'
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
;
|
|
5
|
+
import addFsm from './addFsm.js'
|
|
6
|
+
import addFunctions from './addFunctions.js'
|
|
9
7
|
|
|
10
8
|
const fn = {
|
|
11
9
|
_setTransitions
|
|
@@ -18,6 +16,6 @@ const fn = {
|
|
|
18
16
|
|
|
19
17
|
|
|
20
18
|
|
|
21
|
-
|
|
19
|
+
export default fn
|
|
22
20
|
|
|
23
21
|
|
package/src/msg.js
CHANGED
package/test/01-basics.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
, expect = require ( 'chai' ).expect
|
|
5
|
-
;
|
|
6
|
-
|
|
1
|
+
import FsmHub from '../src/main.js'
|
|
2
|
+
import Fsm from '@peter.naydenov/fsm'
|
|
3
|
+
import { expect } from 'chai'
|
|
7
4
|
|
|
8
5
|
const
|
|
9
6
|
WRONG_REACTIVITY_RECORD = 'Error: Wrong reactivity record on row %s.'
|
|
@@ -140,7 +137,7 @@ it ( 'Add a fsm', () => {
|
|
|
140
137
|
|
|
141
138
|
|
|
142
139
|
|
|
143
|
-
it ( 'Use hub-
|
|
140
|
+
it ( 'Use hub-transformer', done => {
|
|
144
141
|
// Define Fsm machines
|
|
145
142
|
const
|
|
146
143
|
miniOne = {
|
|
@@ -152,10 +149,10 @@ it ( 'Use hub-tranformer', done => {
|
|
|
152
149
|
|
|
153
150
|
// Setup fsm transition libraries
|
|
154
151
|
const transitionOne = {
|
|
155
|
-
switchOn ( task, dependencies, stateObj,
|
|
152
|
+
switchOn ( task, dependencies, stateObj, data ) {
|
|
156
153
|
task.done ({
|
|
157
154
|
success : true
|
|
158
|
-
, response :
|
|
155
|
+
, response : data
|
|
159
156
|
})
|
|
160
157
|
}
|
|
161
158
|
};
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
, expect = require ( 'chai' ).expect
|
|
5
|
-
;
|
|
1
|
+
import FsmHub from '../src/main.js'
|
|
2
|
+
import Fsm from '@peter.naydenov/fsm'
|
|
3
|
+
import { expect } from 'chai'
|
|
6
4
|
|
|
7
5
|
describe ( 'Fsm Hub', () => {
|
|
8
6
|
|