Skip to content

Commit 2b8c683

Browse files
committed
animated spring cleaning and MIT license
1 parent fef3b19 commit 2b8c683

17 files changed

+104
-229
lines changed

src/animated/AnimatedArray.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,51 @@ import guid from './guid'
66
class AnimatedArray extends AnimatedWithChildren {
77
constructor(array) {
88
super()
9-
this.values = array.map(n => new AnimatedValue(n))
9+
this._values = array.map(n => new AnimatedValue(n))
1010
this._listeners = {}
1111
}
1212

1313
setValue(values) {
14-
values.forEach((n, i) => this.values[i].setValue(n))
14+
values.forEach((n, i) => this._values[i].setValue(n))
1515
}
1616

1717
setOffset(values) {
18-
values.forEach((n, i) => this.values[i].setOffset(n))
18+
values.forEach((n, i) => this._values[i].setOffset(n))
1919
}
2020

2121
flattenOffset() {
22-
this.values.forEach(v => v.flattenOffset())
22+
this._values.forEach(v => v.flattenOffset())
2323
}
2424

2525
__getValue() {
26-
return this.values.map(v => v.__getValue())
26+
return this._values.map(v => v.__getValue())
2727
}
2828

2929
stopAnimation(callback) {
30-
this.values.forEach(v => v.stopAnimation())
30+
this._values.forEach(v => v.stopAnimation())
3131
callback && callback(this.__getValue())
3232
}
3333

3434
addListener(callback) {
3535
const id = guid()
3636
const jointCallback = ({ value: number }) => callback(this.__getValue())
37-
this._listeners[id] = this.values.map(v => v.addListener(jointCallback))
37+
this._listeners[id] = this._values.map(v => v.addListener(jointCallback))
3838
return id
3939
}
4040

4141
removeListener(id) {
42-
this.values.forEach(v => v.removeListener(id))
42+
this._values.forEach(v => v.removeListener(id))
4343
delete this._listeners[id]
4444
}
4545

4646
__attach() {
47-
for (let i = 0; i < this.values.length; ++i) {
48-
if (this.values[i] instanceof Animated) {
49-
this.values[i].__addChild(this)
50-
}
51-
}
47+
for (let i = 0; i < this._values.length; ++i)
48+
if (this._values[i] instanceof Animated) this._values[i].__addChild(this)
5249
}
5350

5451
__detach() {
55-
for (let i = 0; i < this.values.length; ++i) {
56-
if (this.values[i] instanceof Animated) {
57-
this.values[i].__removeChild(this)
58-
}
59-
}
52+
for (let i = 0; i < this._values.length; ++i)
53+
if (this._values[i] instanceof Animated) this._values[i].__removeChild(this)
6054
}
6155
}
6256

src/animated/AnimatedInterpolation.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class AnimatedInterpolation extends AnimatedWithChildren {
2626
}
2727
})
2828
}
29-
3029
const id = guid()
3130
this._listeners[id] = callback
3231
return id
@@ -46,7 +45,6 @@ class AnimatedInterpolation extends AnimatedWithChildren {
4645

4746
__detach() {
4847
this._parent.__removeChild(this)
49-
5048
this._parentListener = this._parent.removeListener(this._parentListener)
5149
}
5250
}

src/animated/AnimatedProps.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class AnimatedProps extends Animated {
2222
if (value instanceof Animated) props[key] = value.__getValue()
2323
else props[key] = value
2424
}
25-
2625
return props
2726
}
2827

src/animated/AnimatedTemplate.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,19 @@ class AnimatedTemplate extends AnimatedWithChildren {
1515

1616
__getValue() {
1717
let value = this._strings[0]
18-
for (let i = 0; i < this._values.length; ++i) {
18+
for (let i = 0; i < this._values.length; ++i)
1919
value += this.__transformValue(this._values[i]) + this._strings[1 + i]
20-
}
2120
return value
2221
}
2322

2423
__attach() {
25-
for (let i = 0; i < this._values.length; ++i) {
24+
for (let i = 0; i < this._values.length; ++i)
2625
if (this._values[i] instanceof Animated) this._values[i].__addChild(this)
27-
}
2826
}
2927

3028
__detach() {
31-
for (let i = 0; i < this._values.length; ++i) {
29+
for (let i = 0; i < this._values.length; ++i)
3230
if (this._values[i] instanceof Animated) this._values[i].__removeChild(this)
33-
}
3431
}
3532
}
3633

src/animated/AnimatedValue.js

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,24 @@ import guid from './guid'
2727
* this two-phases process is to deal with composite props such as
2828
* transform which can receive values from multiple parents.
2929
*/
30-
function _flush(rootNode) {
31-
var animatedStyles = new Set()
3230

33-
function findAnimatedStyles(node) {
34-
if (typeof node.update === 'function') {
35-
animatedStyles.add(node)
36-
} else {
37-
node.__getChildren().forEach(findAnimatedStyles)
38-
}
39-
}
31+
function findAnimatedStyles(node, styles) {
32+
if (typeof node.update === 'function') styles.add(node)
33+
else node.__getChildren().forEach(findAnimatedStyles, styles)
34+
}
4035

41-
findAnimatedStyles(rootNode)
36+
function _flush(rootNode) {
37+
var animatedStyles = new Set()
38+
findAnimatedStyles(rootNode, animatedStyles)
4239
animatedStyles.forEach(animatedStyle => animatedStyle.update())
4340
}
41+
4442
/**
4543
* Standard value for driving animations. One `Animated.Value` can drive
4644
* multiple properties in a synchronized fashion, but can only be driven by one
4745
* mechanism at a time. Using a new mechanism (e.g. starting a new animation,
4846
* or calling `setValue`) will stop any previous ones.
4947
*/
50-
5148
class AnimatedValue extends AnimatedWithChildren {
5249
constructor(value) {
5350
super()
@@ -64,44 +61,42 @@ class AnimatedValue extends AnimatedWithChildren {
6461
__getValue() {
6562
return this._value + this._offset
6663
}
64+
6765
/**
6866
* Directly set the value. This will stop any animations running on the value
6967
* and update all the bound properties.
7068
*/
71-
7269
setValue(value) {
7370
if (this._animation) {
7471
this._animation.stop()
75-
7672
this._animation = null
7773
}
78-
7974
this._updateValue(value)
8075
}
76+
8177
/**
8278
* Sets an offset that is applied on top of whatever value is set, whether via
8379
* `setValue`, an animation, or `Animated.event`. Useful for compensating
8480
* things like the start of a pan gesture.
8581
*/
86-
8782
setOffset(offset) {
8883
this._offset = offset
8984
}
85+
9086
/**
9187
* Merges the offset value into the base value and resets the offset to zero.
9288
* The final output of the value is unchanged.
9389
*/
94-
9590
flattenOffset() {
9691
this._value += this._offset
9792
this._offset = 0
9893
}
94+
9995
/**
10096
* Adds an asynchronous listener to the value so you can observe updates from
10197
* animations. This is useful because there is no way to
10298
* synchronously read the value because it might be driven natively.
10399
*/
104-
105100
addListener(callback) {
106101
var id = guid()
107102
this._listeners[id] = callback
@@ -115,31 +110,31 @@ class AnimatedValue extends AnimatedWithChildren {
115110
removeAllListeners() {
116111
this._listeners = {}
117112
}
113+
118114
/**
119115
* Stops any running animation or tracking. `callback` is invoked with the
120116
* final value after stopping the animation, which is useful for updating
121117
* state to match the animation position with layout.
122118
*/
123-
124119
stopAnimation(callback) {
125120
this.stopTracking()
126121
this._animation && this._animation.stop()
127122
this._animation = null
128123
callback && callback(this.__getValue())
129124
}
125+
130126
/**
131127
* Interpolates the value before updating the property, e.g. mapping 0-1 to
132128
* 0-10.
133129
*/
134-
135130
interpolate(config) {
136131
return new AnimatedInterpolation(this, Interpolation.create(config))
137132
}
133+
138134
/**
139135
* Typically only used internally, but could be used by a custom Animation
140136
* class.
141137
*/
142-
143138
animate(animation, callback) {
144139
var handle = null
145140
if (animation.__isInteraction) handle = InteractionManager.current.createInteractionHandle()
@@ -157,18 +152,18 @@ class AnimatedValue extends AnimatedWithChildren {
157152
previousAnimation,
158153
)
159154
}
155+
160156
/**
161157
* Typically only used internally.
162158
*/
163-
164159
stopTracking() {
165160
this._tracking && this._tracking.__detach()
166161
this._tracking = null
167162
}
163+
168164
/**
169165
* Typically only used internally.
170166
*/
171-
172167
track(tracking) {
173168
this.stopTracking()
174169
this._tracking = tracking
@@ -177,11 +172,8 @@ class AnimatedValue extends AnimatedWithChildren {
177172
_updateValue(value) {
178173
this._value = value
179174
_flush(this)
180-
for (var key in this._listeners) {
181-
this._listeners[key]({
182-
value: this.__getValue(),
183-
})
184-
}
175+
for (var key in this._listeners)
176+
this._listeners[key]({ value: this.__getValue() })
185177
}
186178
}
187179

src/animated/Interpolation.js

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import normalizeColor from 'normalize-css-color'
22

33
var linear = t => t
4+
45
/**
56
* Very handy helper to map input ranges to output ranges with an easing
67
* function and custom behavior outside of the ranges.
78
*/
8-
99
class Interpolation {
1010
static create(config) {
1111
if (config.outputRange && typeof config.outputRange[0] === 'string') {
@@ -47,8 +47,9 @@ class Interpolation {
4747
}
4848

4949
function interpolate(input, inputMin, inputMax, outputMin, outputMax, easing, extrapolateLeft, extrapolateRight) {
50-
var result = input // Extrapolate
51-
50+
var result = input
51+
52+
// Extrapolate
5253
if (result < inputMin) {
5354
if (extrapolateLeft === 'identity') {
5455
return result
@@ -77,7 +78,6 @@ function interpolate(input, inputMin, inputMax, outputMin, outputMax, easing, ex
7778
if (input <= inputMin) {
7879
return outputMin
7980
}
80-
8181
return outputMax
8282
} // Input Range
8383

@@ -105,9 +105,7 @@ function interpolate(input, inputMin, inputMax, outputMin, outputMax, easing, ex
105105
function colorToRgba(input) {
106106
var int32Color = normalizeColor(input)
107107

108-
if (int32Color === null) {
109-
return input
110-
}
108+
if (int32Color === null) return input
111109

112110
int32Color = int32Color || 0 // $FlowIssue
113111

@@ -119,6 +117,7 @@ function colorToRgba(input) {
119117
}
120118

121119
var stringShapeRegex = /[0-9\.-]+/g
120+
122121
/**
123122
* Supports string shapes by extracting numbers so new values can be computed,
124123
* and recombines those values into new strings of the same shape. Supports
@@ -127,10 +126,10 @@ var stringShapeRegex = /[0-9\.-]+/g
127126
* rgba(123, 42, 99, 0.36) // colors
128127
* -45deg // values with units
129128
*/
130-
131129
function createInterpolationFromStringOutputRange(config) {
132130
var outputRange = config.outputRange
133131
outputRange = outputRange.map(colorToRgba)
132+
134133
// ->
135134
// [
136135
// [0, 50],
@@ -142,7 +141,6 @@ function createInterpolationFromStringOutputRange(config) {
142141
/* $FlowFixMe(>=0.18.0): `outputRange[0].match()` can return `null`. Need to
143142
* guard against this possibility.
144143
*/
145-
146144
var outputRanges = outputRange[0].match(stringShapeRegex).map(() => [])
147145
outputRange.forEach(value => {
148146
/* $FlowFixMe(>=0.18.0): `value.match()` can return `null`. Need to guard
@@ -152,18 +150,16 @@ function createInterpolationFromStringOutputRange(config) {
152150
outputRanges[i].push(+number)
153151
})
154152
})
155-
/* $FlowFixMe(>=0.18.0): `outputRange[0].match()` can return `null`. Need to
156-
* guard against this possibility.
157-
*/
158153

154+
/* $FlowFixMe(>=0.18.0): `outputRange[0].match()` can return `null`. Need to
155+
* guard against this possibility.
156+
*/
159157
var interpolations = outputRange[0].match(stringShapeRegex).map((value, i) => {
160-
return Interpolation.create({
161-
...config,
162-
outputRange: outputRanges[i],
163-
})
164-
}) // rgba requires that the r,g,b are integers.... so we want to round them, but we *dont* want to
165-
// round the opacity (4th column).
158+
return Interpolation.create({ ...config, outputRange: outputRanges[i] })
159+
})
166160

161+
// rgba requires that the r,g,b are integers.... so we want to round them, but we *dont* want to
162+
// round the opacity (4th column).
167163
const shouldRound = /^rgb/.test(outputRange[0])
168164
return input => {
169165
var i = 0 // 'rgba(0, 100, 200, 0)'
@@ -177,9 +173,7 @@ function createInterpolationFromStringOutputRange(config) {
177173
}
178174

179175
function findRange(input, inputRange) {
180-
for (var i = 1; i < inputRange.length - 1; ++i) {
181-
if (inputRange[i] >= input) break
182-
}
176+
for (var i = 1; i < inputRange.length - 1; ++i) if (inputRange[i] >= input) break
183177
return i - 1
184178
}
185179

0 commit comments

Comments
 (0)