1- // RUNNING - application is running
2- // STOPPED - application is running, but programmatically stopped
3- // ERROR - application is running, but has a critical error (e.g.: DB connection error): app doesn't serves requests
4- // INIT - application is starting, initialization: starting phase (app doesn't handle request)
1+ /**
2+ * Application state handler.
3+ */
54
6- // start state
5+ // start state is INIT
76var appState = 'INIT'
87
98/**
@@ -13,6 +12,16 @@ var appState = 'INIT'
1312 * Tipical use case is when cb contains a logger function.
1413 */
1514module . exports = ( cb ) => {
15+ // RUNNING - application is running
16+ // STOPPED - application is running, but programmatically stopped
17+ // ERROR - application is running, but has a critical error (e.g.: DB connection error): app doesn't serves requests
18+ // INIT - application is starting, initialization: starting phase (app doesn't handle request)
19+ const state = {
20+ INIT : 'INIT' ,
21+ RUNNING : 'RUNNING' ,
22+ ERROR : 'ERROR' ,
23+ STOPPED : 'STOPPED'
24+ }
1625 /**
1726 * Set application state to new state and loging it,
1827 * if state has changed.
@@ -27,21 +36,37 @@ module.exports = (cb) => {
2736
2837 // changing appState
2938 function init ( ) {
30- changeAppState ( ' INIT' )
39+ changeAppState ( state . INIT )
3140 }
3241 function running ( ) {
33- changeAppState ( ' RUNNING' )
42+ changeAppState ( state . RUNNING )
3443 }
3544 function error ( ) {
36- changeAppState ( ' ERROR' )
45+ changeAppState ( state . ERROR )
3746 }
3847 function stopped ( ) {
39- changeAppState ( 'STOPPED' )
48+ changeAppState ( state . STOPPED )
49+ }
50+
51+ function isInit ( ) {
52+ return appState === state . INIT
53+ }
54+ function isRunning ( ) {
55+ return appState === state . RUNNING
56+ }
57+ function isError ( ) {
58+ return appState === state . ERROR
59+ }
60+ function isStopped ( ) {
61+ return appState === state . STOPPED
4062 }
4163
42- // reading appState
4364 function get ( ) {
4465 return appState
4566 }
46- return { init, running, error, stopped, get}
67+
68+ function list ( ) {
69+ return state
70+ }
71+ return { init, running, error, stopped, get, list, isInit, isRunning, isError, isStopped}
4772}
0 commit comments