Permalink
Browse files

Added backward compatibility support for deprecated methods

  • Loading branch information...
nvh committed Sep 13, 2016
1 parent b94edd2 commit 44253ef8a3ecda1ff4710e340f2ddd01e45db868
Showing with 157 additions and 0 deletions.
  1. +2 −0 framer/LayerStateMachine.coffee
  2. +56 −0 framer/LayerStates.coffee
  3. +99 −0 test/tests/LayerStatesTest.coffee
@@ -48,3 +48,5 @@ class exports.LayerStateMachine extends BaseClass
super
# Also emit this to the layer with self as argument
@_layer.emit args...
properties: {}
View
@@ -14,6 +14,26 @@ readOnlyProperty = (object, name, enumerable, getter) ->
set: ->
throw new Error "You can't override special state '#{name}'"
deprecatedProperty = (object, name, replacementSuggestion, stateMachine, getter, setter=null) ->
Object.defineProperty object, name,
configurable: true
enumerable: false
get: ->
if stateMachine.properties[name]?
return stateMachine.properties[name]
message = "states.#{name} is deprecated"
if replacementSuggestion?
message += ", use '#{replacementSuggestion}' instead."
console.warn message
getter(stateMachine._layer)
set: (value) ->
if setter?
console.warn "states.#{name} a reserved state name and might not work as expected"
setter(stateMachine._layer, value)
else
console.warn "states.#{name} is a deprecated method, using it as a state name may cause unexpected behaviour in old projects"
stateMachine.properties[name] = value
class exports.LayerStates
constructor: (stateMachine) ->
readOnlyProperty @, "initial", true, -> stateMachine.initial
@@ -22,6 +42,42 @@ class exports.LayerStates
readOnlyProperty @, "previousName", false, -> stateMachine.previousName
readOnlyProperty @, "currentName", false, -> stateMachine.currentName
## Deprecated methods
deprecatedProperty @, "add", "layer.states = ", stateMachine, (layer) ->
(states) ->
layer.states = states
deprecatedProperty @, "remove", "delete layer.states.a", stateMachine, (layer) ->
(stateName) ->
delete layer.states[stateName]
deprecatedProperty @, "switch", "layer.animate \"a\"", stateMachine, (layer) ->
(stateName) ->
layer.animate stateName
deprecatedProperty @, "switchInstant", "layer.switchInstant \"a\"", stateMachine, (layer) ->
(stateName) ->
layer.switchInstant stateName
deprecatedProperty @, "all", "layer.stateNames", stateMachine, (layer) ->
layer.stateNames
deprecatedProperty @, "states", "layer.stateNames", stateMachine, (layer) ->
layer.stateNames
deprecatedProperty @, "next", "layer.animateToNextState", stateMachine, (layer) ->
(options) ->
layer.animateToNextState(options)
deprecatedProperty @, "last", null, stateMachine, (layer) ->
(options) ->
last = _.last(stateMachine._previousNames)
layer.animate(last, options)
deprecatedProperty @, "animationOptions", null, stateMachine
, (layer) ->
layer.options
, (layer, value) ->
layer.options = value
deprecatedProperty @, "animatingKeys", null, stateMachine, (layer) ->
->
keys = []
for name, state of layer.states
keys = _.union(keys, _.keys(state))
keys
@filterStateProperties: (properties) ->
stateProperties = {}
@@ -388,3 +388,102 @@ describe "LayerStates", ->
animation = layer.animate
x: 100
animation.options.time.should.equal 4
describe "Backwards compatibility", ->
it "should still support layer.states.add", ->
layer = new Layer
layer.states.add
stateA: x: 200
stateB: scale: 0.5
assert.deepEqual layer.stateNames, ['initial','stateA','stateB']
assert.deepEqual layer.states.stateA, x: 200
assert.deepEqual layer.states.stateB, scale: 0.5
it "should still support layer.states.remove", ->
layer = new Layer
layer.states =
stateA: x: 200
stateB: scale: 0.5
assert.deepEqual layer.stateNames, ['initial','stateA','stateB']
layer.states.remove 'stateA'
assert.deepEqual layer.stateNames, ['initial','stateB']
it "should still support layer.states.switch", (done) ->
layer = new Layer
layer.states =
stateA: x: 200
stateB: scale: 0.5
layer.onStateDidSwitch ->
assert.equal layer.states.currentName, 'stateA'
done()
layer.states.switch 'stateA'
it "should still support layer.states.switchInstant", ->
layer = new Layer
layer.states =
stateA: x: 200
stateB: scale: 0.5
layer.states.switchInstant 'stateB'
assert.equal layer.states.currentName, 'stateB'
it "should still support layer.states.all", ->
layer = new Layer
layer.states =
stateA: x: 200
stateB: scale: 0.5
assert.deepEqual layer.states.all, ['initial','stateA','stateB']
it "should still support layer.states.states", ->
layer = new Layer
layer.states =
stateA: x: 200
stateB: scale: 0.5
assert.deepEqual layer.states.states, ['initial','stateA','stateB']
it "should still support layer.states.animatingKeys", ->
layer = new Layer
layer.states =
stateA: x: 200, y: 300
stateB: scale: 0.5
assert.deepEqual layer.states.animatingKeys(), ["width", "height", "visible", "opacity", "clip", "scrollHorizontal", "scrollVertical", "x", "y", "z", "scaleX", "scaleY", "scaleZ", "scale", "skewX", "skewY", "skew", "originX", "originY", "originZ", "perspective", "perspectiveOriginX", "perspectiveOriginY", "rotationX", "rotationY", "rotationZ", "rotation", "blur", "brightness", "saturate", "hueRotate", "contrast", "invert", "grayscale", "sepia", "shadowX", "shadowY", "shadowBlur", "shadowSpread", "shadowColor", "backgroundColor", "color", "borderColor", "borderWidth", "force2d", "flat", "backfaceVisible", "name", "borderRadius", "html", "image", "scrollX", "scrollY", "mouseWheelSpeedMultiplier", "velocityThreshold", "constrained"]
delete layer.states.initial
assert.deepEqual layer.states.animatingKeys(), ["x","y","scale"]
it "should still support layer.states.next", (done) ->
layer = new Layer
layer.states =
stateA: x: 200
stateB: scale: 0.5
layer.onStateDidSwitch ->
assert.equal layer.states.currentName, 'stateA'
done()
layer.states.next()
it "should still support layer.states.last", (done) ->
layer = new Layer
layer.states =
stateA: x: 200
stateB: scale: 0.5
layer.switchInstant 'stateB'
layer.switchInstant 'stateA'
layer.switchInstant 'stateB'
layer.onStateDidSwitch ->
assert.equal layer.states.currentName, 'stateA'
done()
layer.states.last()
it "should still support layer.states.animationOptions", ->
layer = new Layer
layer.states =
stateA: x: 200
layer.states.animationOptions =
time: 4
animation = layer.animate "stateA"
animation.options.time.should.equal 4
it "should work when using one of the deprecated methods as statename", ->
layer = new Layer
layer.states =
add: x: 200
layer.animate "add", instant: true
assert.equal layer.states.add.x, 200
assert.equal layer.x, 200

0 comments on commit 44253ef

Please sign in to comment.