Created
August 22, 2015 00:06
-
-
Save bingomanatee/b09f3c7353a14ebacde4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
define(['angular', 'javascript-state-machine'], function (angular, StateMachine) { | |
angular.module('AwaitingActivationCtrl', []) | |
.controller('AwaitingActivationCtrl', ['$scope', '$timeout', function ($scope, $timeout) { | |
var results = ['parking', 'validating', 'activating', 'finished', 'finished'] | |
var rIndex = 0; | |
$scope.delay = 1500; | |
$scope.stateMessage = '~#0'; | |
$scope.calls = 0; | |
$scope.freqChange = 25; | |
$scope.log = []; | |
$scope.indexValue = function(){ | |
return rIndex; | |
} | |
function log(a, b, c, d) { | |
$scope.log.push([a, b, c, d].join(' ')); | |
while ($scope.log.length >10) $scope.log.shift(); | |
} | |
var time = new Date().getTime(); | |
var state = StateMachine.create({ | |
events: [ | |
{name: 'park', from: '*', to: 'parked'}, | |
{name: 'validating', from: '*', to: 'valid'}, | |
{name: 'activating', from: '*', to: 'active'}, | |
{name: 'restart', from: '*', to: 'start'}, | |
{name: 'finish', from: '*', to: 'finished'} | |
], | |
initial: 'start', | |
callbacks: { | |
onenterstart: function (event, from, to) { | |
$scope.log=[];; | |
log('STATE ' + from + '... ' + to + ' at ', new Date().getTime() - time); | |
$scope.eventName = to; | |
rIndex = 0; | |
$scope.stateMessage = 'LittleRedCorvette'; | |
}, | |
onafterrestart: function () { | |
log('restarted EVENT at ', new Date().getTime() - time); | |
API(_onAPI); | |
}, | |
onenterparked: function (event, from, to) { | |
log('STATE ' + from + '... ' + to + ' at ', new Date().getTime() - time); | |
$scope.eventName = to; | |
$scope.stateMessage = "I guess I should of knownBy the way you parked your car sideways\n" + | |
"That it wouldn't last\n" + | |
"See you're the kinda personThat believes in makin' out once\n" + | |
"Love 'em and leave 'em fast"; | |
}, | |
onafterpark: function () { | |
log('park EVENT at ', new Date().getTime() - time); | |
API(_onAPI); | |
}, | |
onentervalid: function (event, from, to) { | |
log('STATE ' + from + '... ' + to + ' at ', new Date().getTime() - time); | |
$scope.eventName = to; | |
$scope.stateMessage = "I guess I must be dumb\n" + | |
"'Cause you had a pocket full of horses\n" + | |
"Trojan and some of them used\n" + | |
"But it was Saturday night\n" + | |
"I guess that makes it all right\n" + | |
"And you say what have I got to lose?\n" + | |
"And honey I say"; | |
}, | |
onaftervalidating: function () { | |
log('validating EVENT at ', new Date().getTime() - time); | |
API(_onAPI); | |
}, | |
onenteractive: function (event, from, to) { | |
log('STATE ' + from + '... ' + to + ' at ', new Date().getTime() - time); | |
$scope.eventName = to; | |
$scope.stateMessage = "I guess I should of closed my eyes\n" + | |
"When you drove me to the place\n" + | |
"Where your horses run free" + | |
"'Cause I felt a little ill\n" + | |
"When I saw all the pictures\n" + | |
"Of the jockeys that were there before me"; | |
}, | |
onafteractivating: function () { | |
log('activating EVENT at ', new Date().getTime() - time); | |
API(_onAPI); | |
}, | |
onenterfinished: function (event, from, to) { | |
log('STATE ' + from + '... ' + to + ' at ', new Date().getTime() - time); | |
$scope.eventName = to; | |
$scope.stateMessage = | |
"Believe it or not \n" + | |
"I started to worry \n" + | |
"I wondered if I had enough class \n" + | |
"But it was Saturday night \n" + | |
"I guess that makes it all right \n" + | |
"And you say, \"Baby, have you got enough gas?\" \n" + | |
"Oh yeah" | |
}, | |
onafterfinish: function () { | |
log('finish EVENT at ', new Date().getTime() - time); | |
log('finished: not restarting API'); | |
} | |
} | |
}); | |
function API(onDone) { | |
++$scope.calls; | |
var result = results[rIndex]; | |
if (rIndex < results.length - 1) { | |
if (Math.random() < $scope.freqChange / 100) { | |
++rIndex; | |
} | |
} | |
rIndex = Math.min(results.length - 1, rIndex); | |
$timeout(function () { | |
onDone(result); | |
}, $scope.delay); | |
} | |
function _onAPI(result) { | |
switch (result) { | |
case 'parking': | |
state.park(); | |
break; | |
case 'validating': | |
state.validating(); | |
break; | |
case 'activating': | |
state.activating(); | |
break; | |
case 'finished': | |
state.finish(); | |
break; | |
default: | |
throw new Error('unhandled result ' + result); | |
} | |
} | |
$scope.onCheck = function () { | |
API(_onAPI); | |
} | |
$scope.restart = function () { | |
state.restart(); | |
$scope.onCheck(); | |
} | |
}]); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment