Skip to content

Commit

Permalink
updated the state property to call the setState method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brec Carson committed Apr 27, 2012
1 parent 53415c4 commit 3b238f5
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/statechart.coffee
Expand Up @@ -9,7 +9,7 @@ class Stateful extends Emitter
@define 'stateName', get: -> @__state.name
@define 'state',
get: -> @__state
set: (obj) -> @__state = obj
set: (obj) -> @setState obj

@Success: false
@Failure: true
Expand All @@ -32,7 +32,11 @@ class Stateful extends Emitter

addPaths chart, @::__statechart

# TODO: confirm integrity of chart, makes sure all entry / exit points are accounted for
#
# TODO: confirm integrity of chart:
# - makes sure all entry / exit points are accounted for
# - make sure paths are valid
#

constructor: (config={}) ->
return unless @statechart?
Expand All @@ -45,7 +49,9 @@ class Stateful extends Emitter

dispose: -> @removeAllListeners()

setState: (stateObj) ->
setState: (nameOrObj) ->
stateObj = if typeof nameOrObj is 'string' then @pathResolver nameOrObj else nameOrObj

# TODO validate state change

if @state
Expand All @@ -54,7 +60,7 @@ class Stateful extends Emitter
if @isDescendantState stateObj
@removeMethods oldState.methods

@state = stateObj
@__state = stateObj
@addMethods @state.methods
@buildTransitions @state.transitions

Expand Down Expand Up @@ -88,10 +94,7 @@ class Stateful extends Emitter
chgMethod = @[t.action]
@[t.action] = =>
dontTransition = (chgMethod.apply @, arguments)

unless dontTransition
@setState destination

unless dontTransition then @setState destination
return dontTransition

pathResolver: (path) ->
Expand All @@ -101,8 +104,7 @@ class Stateful extends Emitter
# start from top
steps = path.split '/'
target = paths: @statechart
for step in steps
target = target.paths[step]
target = target.paths[step] for step in steps

return target

Expand Down

0 comments on commit 3b238f5

Please sign in to comment.