Permalink
Browse files

Fixing stateSwitch in case of instant: true

  • Loading branch information...
nvh committed Oct 10, 2016
1 parent 641be2d commit 27d0b00265579986dadfce92fe75eaa01a380f8c
Showing with 30 additions and 7 deletions.
  1. +11 −3 framer/LayerStateMachine.coffee
  2. +19 −4 test/tests/LayerStatesTest.coffee
@@ -33,7 +33,6 @@ class exports.LayerStateMachine extends BaseClass
@switchTo(stateName, {instant: true})
switchTo: (stateName, options={}) ->
-
# Check if the state exists, if not this is a pretty serious error
throw Error "No such state: '#{stateName}'" unless @states[stateName]
@@ -54,8 +53,18 @@ class exports.LayerStateMachine extends BaseClass
options.start = false
animation = @layer.animate(properties, options)
+ # In the case of instant: true, onStart and onStop are called from within animation.start()
+ # This function is called once after animation.start() or in onStart, whichEver comes first.
+ stateSwitched = false
+ switchState = =>
+ return if stateSwitched
+ stateSwitched = true
+ @_previousNames.push(stateNameA)
+ @_currentName = stateNameB
+
onStart = =>
@emit(Events.StateSwitchStart, stateNameA, stateNameB, @)
+ switchState()
onStop = =>
@emit(Events.StateSwitchStop, stateNameA, stateNameB, @)
@@ -76,8 +85,7 @@ class exports.LayerStateMachine extends BaseClass
animation.start() if startAnimation
- @_previousNames.push(stateNameA)
- @_currentName = stateNameB
+ switchState()
return animation
@@ -15,28 +15,43 @@ describe "LayerStates", ->
@layer.states.a = {x:100, y:100}
@layer.states.b = {x:200, y:200}
- it "should emit willSwitch when switching", (done) ->
+ it "should emit StateSwitchStart when switching", (done) ->
test = (previous, current, states) =>
previous.should.equal initialStateName
current.should.equal "a"
@layer.states.current.name.should.equal initialStateName
+ @layer.states.machine._previousNames.should.eql []
stateWithoutName(@layer.states.current).should.eql @layer.states[initialStateName]
done()
- @layer.on Events.StateWillSwitch, test
+ @layer.on Events.StateSwitchStart, test
@layer.animate "a", instant: true
- it "should emit didSwitch when switching", (done) ->
+ it "should emit StateSwitchStop when switching", (done) ->
test = (previous, current, states) =>
previous.should.equal initialStateName
current.should.equal "a"
@layer.states.current.name.should.equal "a"
+ @layer.states.machine._previousNames.should.eql ["default"]
stateWithoutName(@layer.states.current).should.eql @layer.states.a
done()
- @layer.on Events.StateDidSwitch, test
+ @layer.on Events.StateSwitchStop, test
+ @layer.animate "a", time: 0.01
+
+ it "should emit StateSwitchStop when switching instant", (done) ->
+
+ test = (previous, current, states) =>
+ previous.should.equal initialStateName
+ current.should.equal "a"
+ @layer.states.current.name.should.equal "a"
+ @layer.states.machine._previousNames.should.eql ["default"]
+ stateWithoutName(@layer.states.current).should.eql @layer.states.a
+ done()
+
+ @layer.on Events.StateSwitchStop, test
@layer.animate "a", instant: true
describe "Special states", ->

0 comments on commit 27d0b00

Please sign in to comment.