-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappState.js
121 lines (113 loc) · 2.83 KB
/
appState.js
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
112
113
114
115
116
117
118
119
120
121
const debug = require('debug')('appState')
/**
* Application state handler.
*/
// start state is INIT
var appState = 'INIT'
/**
* Callback has two params: cb(appState, newAppState)
* - appState: application state actually
* - newAppState: new application state
* Tipical use case is when cb contains a logger function.
*/
module.exports = cb => {
// RUNNING - application is running
// STOPPED - application is running, but programmatically stopped
// ERROR - application is running, but has a critical error (e.g.: DB connection error): app doesn't serves requests
// INIT - application is starting, initialization: starting phase (app doesn't handle request)
const state = {
INIT: 'INIT',
RUNNING: 'RUNNING',
STOPPED: 'STOPPED',
ERROR: 'ERROR',
FATAL: 'FATAL',
}
const stateMachine = {
INIT: [state.INIT, state.RUNNING, state.STOPPED, state.ERROR, state.FATAL],
RUNNING: [state.INIT, state.RUNNING, state.STOPPED, state.ERROR, state.FATAL],
STOPPED: [state.INIT, state.RUNNING, state.STOPPED, state.ERROR, state.FATAL],
ERROR: [state.INIT, state.RUNNING, state.STOPPED, state.ERROR, state.FATAL],
FATAL: [state.FATAL],
}
/**
* Set application state to new state and loging it,
* if state has changed.
* @param {string} newAppState
*/
function changeAppState(newAppState) {
if (state[newAppState]) {
if (stateMachine[appState].includes(newAppState)) {
// valid state changes
if (appState !== newAppState) {
if (cb) cb(appState, newAppState)
appState = newAppState
} else {
debug('info: appState has already set: ' + newAppState)
}
} else {
debug('warn: invalid state changes from ' + appState + ' to ' + newAppState)
}
} else {
debug('warn: unknow appState: ' + newAppState)
}
}
// changing appState
function init() {
changeAppState(state.INIT)
}
function running() {
changeAppState(state.RUNNING)
}
function error() {
changeAppState(state.ERROR)
}
function stopped() {
changeAppState(state.STOPPED)
}
function fatal() {
changeAppState(state.FATAL)
}
function isInit() {
return appState === state.INIT
}
function isRunning() {
return appState === state.RUNNING
}
function isError() {
return appState === state.ERROR
}
function isStopped() {
return appState === state.STOPPED
}
function isFatal() {
return appState === state.FATAL
}
function getStateMachine() {
return stateMachine
}
function get() {
return appState
}
function list() {
return state
}
function reset() {
appState = state.INIT
}
return {
init,
running,
error,
stopped,
fatal,
get,
getStateMachine,
list,
isInit,
isRunning,
isError,
isStopped,
isFatal,
reset,
}
}