Skip to content

Commit 035b4a3

Browse files
committed
introducing isXXX() methods
1 parent 5708ae1 commit 035b4a3

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

appState.js

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
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
76
var appState = 'INIT'
87

98
/**
@@ -13,6 +12,16 @@ var appState = 'INIT'
1312
* Tipical use case is when cb contains a logger function.
1413
*/
1514
module.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
}

appState.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,28 @@ describe('appState', () => {
6565
appState.error()
6666
appState.get().should.be.equal('ERROR')
6767
})
68+
69+
it('isInit ok', () => {
70+
const appState = require('./appState')()
71+
appState.init()
72+
appState.isInit().should.be.true()
73+
})
74+
75+
it('isRunning ok', () => {
76+
const appState = require('./appState')()
77+
appState.running()
78+
appState.isRunning().should.be.true()
79+
})
80+
81+
it('isStopped ok', () => {
82+
const appState = require('./appState')()
83+
appState.stopped()
84+
appState.isStopped().should.be.true()
85+
})
86+
87+
it('isError ok', () => {
88+
const appState = require('./appState')()
89+
appState.error()
90+
appState.isError().should.be.true()
91+
})
6892
})

0 commit comments

Comments
 (0)