-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFluxEcoJsStateMonad.mjs
More file actions
111 lines (96 loc) · 3.37 KB
/
FluxEcoJsStateMonad.mjs
File metadata and controls
111 lines (96 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
export class FluxEcoJsStateMonad {
/**
* @property {object}
*/
#stateValues;
/**
* @param {object} stateValues
*/
constructor(stateValues) {
this.#stateValues = stateValues;
}
/**
* @typedef {object} FluxEcoStateValues
* @property {string} currentStateName
* @property {string} nextStateName
* @property {string} finalStateName
* @property {array} completedStateNames
* @property {array} uncompletedStateNames
* @property {Object} data
*
* @param {FluxEcoStateValues} stateValues
*/
static of(stateValues) {
return new this(stateValues);
}
/**
* @return {FluxEcoStateValues}
*/
get stateValues() {
return this.#stateValues;
}
bind(fn) {
const [newState, newFn] = fn(this.#stateValues);
if (newFn === null) {
//final transition of chain
return FluxEcoJsStateMonad.of(newState);
}
if (newFn && typeof newFn !== "function") {
throw new TypeError("Invalid function provided.");
}
return FluxEcoJsStateMonad.of(newState).bind(newFn);
}
/**
* todo think about the static - but in functional thinking it could/should be ok.
* it should work non static by returning the FluxEcoJsStateMonad with the new state.
*
* bind(fn, stateName) ?
*/
/**
* @param {FluxEcoStateValues} stateValues
* @param {string} stateName
* @return {FluxEcoStateValues}
*/
static markStateAsCompleted(stateValues, stateName) {
if (stateValues.currentStateName !== stateName) {
console.log("the current stateName differs from the initalized steps: current stateValues: " + JSON.stringify(stateValues) + " try to mark as completed: " + stateName) //todo
}
let index = stateValues.completedStateNames.indexOf(stateName);
if (index !== -1) {
stateValues.uncompletedStateNames.splice(index, 1);
stateValues.completedStateNames.push(stateName)
stateValues.nextStateName = stateValues.uncompletedStateNames[0];
}
return stateValues;
}
static putStateNameAsNextInFront(stateValues, stateName) {
if (stateValues.uncompletedStateNames.includes(stateName) === false) {
stateValues.uncompletedStateNames.unshift(stateName);
}
stateValues.nextStateName = stateName;
return stateValues;
}
/**
* @param {FluxEcoStateValues} stateValues
* @param {string} currentStateName
* @return {FluxEcoStateValues}
*/
static changeCurrentStateName(stateValues, currentStateName) {
if (stateValues.nextStateName !== currentStateName && stateValues.currentStateName !== currentStateName) {
console.log("the current transition stateName differs from the initalized transition steps " + stateValues.nextStateName + currentStateName) //todo
}
stateValues.currentStateName = currentStateName;
stateValues.nextStateName = stateValues.uncompletedStateNames[1];
return stateValues;
}
/**
* @param {FluxEcoStateValues} stateValues
* @param {object} data
* @return {FluxEcoStateValues}
*/
static setStateData(stateValues, data) {
stateValues = {...stateValues};
stateValues.data = {...data};
return stateValues;
}
}