-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🙋 basic implementation of hierarchical machines (#7)
* basic implementation of hierarchical machines - move tests to test folder - split test file in parts - add test for hierarchical based on spec - add _next private method to return the next state - implement `event` method - move `move` test helper to it's own file - add test that moves a machine to a substate two levels down - add `_unregister` that unregisters submachines recursively - define prototype constructor so class is called a Nanostate, not a Nanobus - ignore package-lock * fmt test fn
- Loading branch information
1 parent
3839653
commit e35f060
Showing
8 changed files
with
196 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ dist/ | |
npm-debug.log* | ||
.DS_Store | ||
.nyc_output | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
var tape = require('tape') | ||
|
||
var nanostate = require('../') | ||
var move = require('./move') | ||
|
||
tape('change to substate and back', function (assert) { | ||
var machine = nanostate('green', { | ||
green: { timer: 'yellow' }, | ||
yellow: { timer: 'red' }, | ||
red: { timer: 'green' } | ||
}) | ||
|
||
machine.event('powerOutage', nanostate('flashingRed', { | ||
flashingRed: { powerRestored: 'green' } | ||
})) | ||
|
||
move(assert, machine, [ | ||
['timer', 'yellow'], | ||
['powerOutage', 'flashingRed'], | ||
['powerRestored', 'green'] | ||
]) | ||
|
||
assert.end() | ||
}) | ||
|
||
tape('move down two levels', function (assert) { | ||
var trafficLights = nanostate('green', { | ||
green: { timer: 'yellow' }, | ||
yellow: { timer: 'red' }, | ||
red: { timer: 'green' } | ||
}) | ||
|
||
var powerOutage = nanostate('flashingRed', { | ||
flashingRed: { powerRestored: 'green' } | ||
}) | ||
|
||
var apocalypse = nanostate('darkness', { | ||
darkness: { worldSaved: 'green' } | ||
}) | ||
|
||
trafficLights.event('powerOutage', powerOutage) | ||
powerOutage.event('apocalypse', apocalypse) | ||
|
||
move(assert, trafficLights, [ | ||
['powerOutage', 'flashingRed'], | ||
['apocalypse', 'darkness'], | ||
['worldSaved', 'green'] | ||
]) | ||
|
||
assert.equal(trafficLights._submachine, null, 'first level submachine is unregistered') | ||
assert.equal(powerOutage._submachine, null, 'second level submachine is unregistered') | ||
|
||
assert.end() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require('./nanostate') | ||
require('./parallel') | ||
require('./hierarchical') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module.exports = move | ||
|
||
// Move the machine a bunch of states. | ||
function move (assert, machine, states) { | ||
states.forEach(function (tuple) { | ||
var initial = machine.state | ||
var expected = tuple[1] | ||
machine.emit(tuple[0]) | ||
assert.equal(machine.state, expected, `from ${initial} to ${expected}`) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
var tape = require('tape') | ||
|
||
var nanostate = require('../') | ||
var move = require('./move') | ||
|
||
tape('sets an initial state', function (assert) { | ||
var machine = nanostate('green', { | ||
green: { timer: 'yellow' }, | ||
yellow: { timer: 'red' }, | ||
red: { timer: 'green' } | ||
}) | ||
assert.equal(machine.state, 'green') | ||
assert.end() | ||
}) | ||
|
||
tape('change state', function (assert) { | ||
var machine = nanostate('green', { | ||
green: { timer: 'yellow' }, | ||
yellow: { timer: 'red' }, | ||
red: { timer: 'green' } | ||
}) | ||
|
||
move(assert, machine, [ | ||
['timer', 'yellow'], | ||
['timer', 'red'], | ||
['timer', 'green'] | ||
]) | ||
assert.end() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
var tape = require('tape') | ||
|
||
var nanostate = require('../') | ||
|
||
tape('create parallel state', (assert) => { | ||
var machine = nanostate.parallel(createParallelTransitions()) | ||
|
||
machine.emit('bold:toggle') | ||
assert.deepEqual(machine.state, { | ||
bold: 'on', underline: 'off', italics: 'off', list: 'none' | ||
}) | ||
|
||
assert.end() | ||
}) | ||
|
||
tape('change states in parallel machine', (assert) => { | ||
var machine = nanostate.parallel(createParallelTransitions()) | ||
|
||
machine.emit('underline:toggle') | ||
machine.emit('list:numbers') | ||
assert.deepEqual(machine.state, { | ||
bold: 'off', underline: 'on', italics: 'off', list: 'numbers' | ||
}) | ||
|
||
machine.emit('bold:toggle') | ||
machine.emit('underline:toggle') | ||
machine.emit('italics:toggle') | ||
machine.emit('list:bullets') | ||
assert.deepEqual(machine.state, { | ||
bold: 'on', underline: 'off', italics: 'on', list: 'bullets' | ||
}) | ||
|
||
machine.emit('list:none') | ||
assert.deepEqual(machine.state, { | ||
bold: 'on', underline: 'off', italics: 'on', list: 'none' | ||
}) | ||
|
||
assert.end() | ||
}) | ||
|
||
function createParallelTransitions () { | ||
return { | ||
bold: nanostate('off', { | ||
on: { 'toggle': 'off' }, | ||
off: { 'toggle': 'on' } | ||
}), | ||
underline: nanostate('off', { | ||
on: { 'toggle': 'off' }, | ||
off: { 'toggle': 'on' } | ||
}), | ||
italics: nanostate('off', { | ||
on: { 'toggle': 'off' }, | ||
off: { 'toggle': 'on' } | ||
}), | ||
list: nanostate('none', { | ||
none: { bullets: 'bullets', numbers: 'numbers' }, | ||
bullets: { none: 'none', numbers: 'numbers' }, | ||
numbers: { bullets: 'bullets', none: 'none' } | ||
}) | ||
} | ||
} |