Skip to content

Commit

Permalink
Add client session state machine
Browse files Browse the repository at this point in the history
  • Loading branch information
jden committed Dec 4, 2015
1 parent 9fd5285 commit ead0915
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
7 changes: 7 additions & 0 deletions client/client_session.js
@@ -1,8 +1,11 @@
var log = require('minilog')('radar:client')
var EventEmitter = require('events').EventEmitter
var inherits = require('util').inherits
var ClientSessionStateMachine = require('./client_session_state_machine')

function ClientSession (name, id, accountName, version, transport) {
this.state = ClientSessionStateMachine.create(this)

this.createdAt = Date.now()
this.lastModified = Date.now()
this.name = name
Expand All @@ -17,6 +20,10 @@ function ClientSession (name, id, accountName, version, transport) {
}
inherits(ClientSession, EventEmitter)

ClientSession.prototype._initialize = function (set) {

}

// Instance methods

// ClientSession Message API:
Expand Down
30 changes: 30 additions & 0 deletions client/client_session_state_machine.js
@@ -0,0 +1,30 @@
var StateMachine = require('javascript-state-machine')
// var bind = require('lodash.bind')

var bind = function (fn, context) {
return function () {
// parameters: event, from, to, ...args
var args = Array.prototype.slice.call(arguments, 3)
return fn.apply(context, args)
}
}

module.exports.create = function createClientSessionStateMachine (clientSession) {
return StateMachine.create({
initial: 'initializing',
events: [
{name: 'initialize', from: 'initializing', to: 'ready'},
{name: 'leave', from: 'ready', to: 'not ready'},
{name: 'comeback', from: 'not ready', to: 'ready'},
{name: 'end', from: ['ready', 'not ready'], to: 'ended'},
{name: 'abort', from: 'initializing', to: 'ended'}
],
callbacks: {
oninitialize: bind(oninitialize, clientSession)
}
})
}

function oninitialize () {
console.log('oninitialize', arguments)
}
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -36,6 +36,7 @@
"async": "^1.5.0",
"callback_tracker": "0.1.0",
"engine.io": "1.4.2",
"lodash.bind": "^3.1.0",
"miniee": "0.0.5",
"minilog": "2.0.8",
"nomnom": "^1.8.1",
Expand Down Expand Up @@ -73,6 +74,8 @@
"test-debug": "./node_modules/.bin/mocha debug --ui exports --reporter spec --slow 4000ms --bail \"$TEST\""
},
"standard": {
"ignore": ["sample/"]
"ignore": [
"sample/"
]
}
}

0 comments on commit ead0915

Please sign in to comment.