Last active
February 8, 2017 15:37
-
-
Save heycarsten/7136a752d42aee13c1072c96480494f4 to your computer and use it in GitHub Desktop.
ember-fsm
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
import Ember from 'ember'; | |
import FSM from 'ember-fsm'; | |
const WAIT_TIMES = { | |
'red': 5000, | |
'amber': 3500, | |
'green': 6500 | |
}; | |
export default Ember.Controller.extend(FSM.Stateful, { | |
fsmStates: { | |
initialState: 'off', | |
off: { | |
didEnter: ['didPowerOff', 'notifyPowerDown'], | |
willExit: 'notifyPowerUp' | |
} | |
}, | |
fsmEvents: { | |
cycle: { | |
after: 'colorChanged', | |
transitions: [ | |
{ off: 'red' }, | |
{ red: 'green' }, | |
{ green: 'amber' }, | |
{ amber: 'red' } | |
] | |
}, | |
powerDown: { | |
transition: { | |
from: ['red', 'green', 'amber'], to: 'off' | |
} | |
} | |
}, | |
actions: { | |
powerUp() { | |
this.sendStateEvent('cycle'); | |
}, | |
powerDown() { | |
if (this.get('isInOff')) { | |
return; | |
} | |
this.set('doPowerDown', true); | |
} | |
}, | |
didPowerOff() { | |
this.set('doPowerDown', false); | |
}, | |
notifyPowerUp() { | |
this.log('info', 'Requesting to take traffic signal online.'); | |
return this.sleep(3000).then(() => { | |
this.log('info', 'Notified central office of power up.'); | |
}); | |
}, | |
notifyPowerDown() { | |
this.log('info', 'Notified central office of power down.'); | |
}, | |
colorChanged(transition) { | |
let ms = WAIT_TIMES[this.get('fsmCurrentState')]; | |
this.log('change', `${transition.fromState} -> ${transition.toState}`); | |
return this.sleep(ms).then(() => { | |
if (this.get('doPowerDown')) { | |
this.sendStateEvent('powerDown'); | |
} else { | |
this.sendStateEvent('cycle'); | |
} | |
}); | |
}, | |
sleep(ms) { | |
return new Ember.RSVP.Promise((resolve) => { | |
Ember.run.later(this, () => { | |
resolve(); | |
}, ms); | |
}); | |
}, | |
log(level, message) { | |
this.get('model.messages').pushObject({ | |
level: level, | |
message: message | |
}); | |
} | |
}); |
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
import Ember from 'ember'; | |
export default Ember.Route.extend({ | |
model() { | |
return { messages: Ember.A([]) }; | |
} | |
}); |
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
body { | |
margin: 12px 16px; | |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
font-size: 12pt; | |
} | |
.signal { | |
text-align: center | |
} | |
.signal ol { | |
display: inline-block; | |
font-family: "wingdings"; | |
background: #ec2; | |
line-height: 0.85; | |
border-radius: 4px; | |
margin: 0; | |
padding: 0; | |
padding: 14px 42px; | |
list-style: none; | |
font-size: 110px; | |
color: #aaa; | |
} | |
.signal li { | |
margin: 0; | |
padding: 0; | |
} | |
.red { | |
color: #f22; | |
} | |
.green { | |
color: #1e2; | |
} | |
.amber { | |
color: #ed3; | |
} | |
.log ol { | |
list-style: none; | |
margin: 22px 0; | |
padding: 0; | |
} |
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
{ | |
"version": "0.11.0", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": true, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.10.2", | |
"ember-data": "2.11.0", | |
"ember-template-compiler": "2.10.2", | |
"ember-testing": "2.10.2" | |
}, | |
"addons": { | |
"ember-fsm": "1.0.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment