1212'use strict' ;
1313
1414const AnimatedNode = require ( './AnimatedNode' ) ;
15+ const AnimatedValue = require ( './AnimatedValue' ) ;
1516const AnimatedWithChildren = require ( './AnimatedWithChildren' ) ;
1617const NativeAnimatedHelper = require ( '../NativeAnimatedHelper' ) ;
1718
@@ -26,7 +27,7 @@ export type InterpolationConfigType = {
2627 * detected during the deployment of v0.38.0. To see the error, remove this
2728 * comment and run flow
2829 */
29- outputRange : Array < number > | Array < string > ,
30+ outputRange : Array < number > | Array < string > | Array < AnimatedNode > ,
3031 easing ?: ( input : number ) => number ,
3132 extrapolate ?: ExtrapolateType ,
3233 extrapolateLeft ?: ExtrapolateType ,
@@ -46,7 +47,7 @@ function createInterpolation(
4647 return createInterpolationFromStringOutputRange ( config ) ;
4748 }
4849
49- const outputRange : Array < number > = ( config.outputRange: any) ;
50+ const outputRange : Array < number > | Array < AnimatedNode > = config.outputRange;
5051 checkInfiniteRange('outputRange', outputRange);
5152
5253 const inputRange = config.inputRange;
@@ -85,12 +86,20 @@ function createInterpolation(
8586 ) ;
8687
8788 const range = findRange ( input , inputRange ) ;
89+ const outputStart : number | AnimatedNode = outputRange [ range ] ;
90+ const outputEnd : number | AnimatedNode = outputRange [ range + 1 ] ;
91+ const outputStartValue =
92+ outputStart instanceof AnimatedNode
93+ ? outputStart . __getValue ( )
94+ : outputStart ;
95+ const outputEndValue =
96+ outputEnd instanceof AnimatedNode ? outputEnd . __getValue ( ) : outputEnd ;
8897 return interpolate (
8998 input ,
9099 inputRange [ range ] ,
91100 inputRange [ range + 1 ] ,
92- outputRange [ range ] ,
93- outputRange [ range + 1 ] ,
101+ outputStartValue ,
102+ outputEndValue ,
94103 easing ,
95104 extrapolateLeft ,
96105 extrapolateRight ,
@@ -291,7 +300,7 @@ function checkValidInputRange(arr: Array<number>) {
291300 }
292301}
293302
294- function checkInfiniteRange ( name : string , arr : Array < number > ) {
303+ function checkInfiniteRange ( name : string , arr : Array < any > ) {
295304 invariant ( arr . length >= 2 , name + ' must have at least 2 elements' ) ;
296305 invariant (
297306 arr . length !== 2 || arr [ 0 ] !== - Infinity || arr [ 1 ] !== Infinity ,
@@ -311,17 +320,24 @@ class AnimatedInterpolation extends AnimatedWithChildren {
311320
312321 _parent : AnimatedNode ;
313322 _config : InterpolationConfigType ;
323+ _transformedOutputRange : Array < AnimatedNode > ;
314324 _interpolation : ( input : number ) = > number | string ;
315325
316326 constructor ( parent : AnimatedNode , config : InterpolationConfigType ) {
317327 super ( ) ;
318328 this . _parent = parent ;
319329 this . _config = config ;
330+ this . _transformedOutputRange = this . __transformOutputRangeToAnimatedValues (
331+ config . outputRange ,
332+ ) ;
320333 this . _interpolation = createInterpolation ( config ) ;
321334 }
322335
323- __makeNative() {
336+ __makeNative(): void {
324337 this . _parent . __makeNative ( ) ;
338+ this . _transformedOutputRange . forEach ( function ( value ) {
339+ value . __makeNative ( ) ;
340+ } ) ;
325341 super . __makeNative ( ) ;
326342 }
327343
@@ -334,37 +350,54 @@ class AnimatedInterpolation extends AnimatedWithChildren {
334350 return this . _interpolation ( parentValue ) ;
335351 }
336352
337- interpolate(config: InterpolationConfigType): AnimatedInterpolation {
338- return new AnimatedInterpolation ( this , config ) ;
339- }
340-
341353 __attach(): void {
342- this . _parent . __addChild ( this ) ;
354+ const that = this ;
355+ this . _parent . __addChild ( that ) ;
356+ this . _transformedOutputRange . forEach ( function ( value ) {
357+ value . __addChild ( that ) ;
358+ } ) ;
343359 }
344360
345361 __detach(): void {
346- this . _parent . __removeChild ( this ) ;
362+ const that = this ;
363+ this . _parent . __removeChild ( that ) ;
364+ this . _transformedOutputRange . forEach ( function ( value ) {
365+ value . __removeChild ( that ) ;
366+ } ) ;
347367 super . __detach ( ) ;
348368 }
349369
350- __transformDataType(range: Array< any > ) {
351- // Change the string array type to number array
352- // So we can reuse the same logic in iOS and Android platform
353- /* $FlowFixMe(>=0.70.0 site=react_native_fb) This comment suppresses an
354- * error found when Flow v0.70 was deployed. To see the error delete this
355- * comment and run Flow. */
370+ __transformOutputRangeToAnimatedValues(
371+ range: Array< number | string | AnimatedNode > ,
372+ ): Array< AnimatedNode > {
356373 return range . map ( function ( value ) {
357- if ( typeof value !== 'string' ) {
358- return value ;
359- }
360- if ( / d e g $ / . test ( value ) ) {
374+ if ( typeof value === 'string' && / d e g $ / . test ( value ) ) {
361375 const degrees = parseFloat ( value ) || 0 ;
376+ // Radians.
362377 const radians = degrees * Math . PI / 180.0 ;
363- return radians ;
364- } else {
365- // Assume radians
366- return parseFloat ( value ) || 0 ;
378+ return new AnimatedValue ( radians ) ;
379+ }
380+ if ( typeof value === 'string' ) {
381+ // Assume radians.
382+ const radians = parseFloat ( value ) || 0 ;
383+ return new AnimatedValue ( radians ) ;
367384 }
385+ if ( typeof value === 'number' ) {
386+ // Just a plain number value.
387+ return new AnimatedValue ( value ) ;
388+ }
389+ if ( value instanceof AnimatedNode ) {
390+ return value ;
391+ }
392+ throw new Error ( 'Incompatible type passed to outputRange.' ) ;
393+ } ) ;
394+ }
395+
396+ __outputRangeToTags(range: Array< AnimatedNode > ): Array< number > {
397+ return range . map ( function ( value ) {
398+ const tag = value . __getNativeTag ( ) ;
399+ invariant ( tag , 'There must be a native tag for this value.' ) ;
400+ return tag ;
368401 } ) ;
369402 }
370403
@@ -374,14 +407,14 @@ class AnimatedInterpolation extends AnimatedWithChildren {
374407 }
375408
376409 return {
410+ type : 'interpolation' ,
411+ parent : this . _parent . __getNativeTag ( ) ,
377412 inputRange : this . _config . inputRange ,
378- // Only the `outputRange` can contain strings so we don't need to transform `inputRange` here
379- outputRange : this . __transformDataType ( this . _config . outputRange ) ,
413+ outputRange : this . __outputRangeToTags ( this . _transformedOutputRange ) ,
380414 extrapolateLeft :
381415 this . _config . extrapolateLeft || this . _config . extrapolate || 'extend' ,
382416 extrapolateRight :
383417 this . _config . extrapolateRight || this . _config . extrapolate || 'extend' ,
384- type : 'interpolation' ,
385418 } ;
386419 }
387420}
0 commit comments