Skip to content

Commit b914c09

Browse files
committed
feat: add "isAnimated" helper
1 parent 8872241 commit b914c09

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
lines changed

src/animated/Animated.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
export function isAnimated(val: unknown): val is Animated {
2+
return val instanceof Animated
3+
}
4+
15
export abstract class Animated<Payload = unknown> {
26
public abstract getValue(): any
37
public getAnimatedValue() {
@@ -37,11 +41,11 @@ export abstract class AnimatedArray<
3741
protected payload!: Payload
3842

3943
public attach() {
40-
this.payload.forEach(p => p instanceof Animated && p.addChild(this))
44+
this.payload.forEach(p => isAnimated(p) && p.addChild(this))
4145
}
4246

4347
public detach() {
44-
this.payload.forEach(p => p instanceof Animated && p.removeChild(this))
48+
this.payload.forEach(p => isAnimated(p) && p.removeChild(this))
4549
}
4650
}
4751

@@ -56,11 +60,10 @@ export class AnimatedObject<
5660
const payload: { [key: string]: any } = {}
5761
for (const key in this.payload) {
5862
const value = this.payload[key]
59-
if (animated && !(value instanceof Animated)) continue
60-
payload[key] =
61-
value instanceof Animated
62-
? value[animated ? 'getAnimatedValue' : 'getValue']()
63-
: value
63+
if (animated && !isAnimated(value)) continue
64+
payload[key] = isAnimated(value)
65+
? value[animated ? 'getAnimatedValue' : 'getValue']()
66+
: value
6467
}
6568
return payload
6669
}
@@ -70,14 +73,12 @@ export class AnimatedObject<
7073
}
7174

7275
public attach() {
73-
Object.values(this.payload).forEach(
74-
s => s instanceof Animated && s.addChild(this)
75-
)
76+
Object.values(this.payload).forEach(s => isAnimated(s) && s.addChild(this))
7677
}
7778

7879
public detach() {
7980
Object.values(this.payload).forEach(
80-
s => s instanceof Animated && s.removeChild(this)
81+
s => isAnimated(s) && s.removeChild(this)
8182
)
8283
}
8384
}

src/animated/AnimatedValue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Animated } from './Animated'
1+
import { Animated, isAnimated } from './Animated'
22
import { AnimatedProps } from './AnimatedProps'
33
import { Animatable, SpringValue } from '../types/animated'
44
import { InterpolatorArgs } from '../types/interpolation'
@@ -53,7 +53,7 @@ export class AnimatedValue<T = number> extends Animated
5353
public done = false
5454

5555
static from(value: any) {
56-
return value instanceof AnimatedValue ? value : new AnimatedValue(value)
56+
return isAnimated(value) ? value : new AnimatedValue(value)
5757
}
5858

5959
constructor(value: T) {

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ export { useTransition } from './useTransition'
1717
export { config } from './shared/constants'
1818
export { interpolate } from './interpolate'
1919
export { Controller } from './animated/Controller'
20+
export { isAnimated } from './animated/Animated'
2021

2122
export * from './legacy'

src/targets/native/AnimatedTransform.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Animated } from '../../animated/Animated'
1+
import { Animated, isAnimated } from '../../animated/Animated'
22

33
type Transform = { [key: string]: string | number | Animated }
44

@@ -15,7 +15,7 @@ export class AnimatedTransform extends Animated {
1515
let result: { [key: string]: unknown } = {}
1616
for (var key in transform) {
1717
var value = transform[key]
18-
result[key] = value instanceof Animated ? value.getValue() : value
18+
result[key] = isAnimated(value) ? value.getValue() : value
1919
}
2020
return result
2121
})
@@ -26,8 +26,7 @@ export class AnimatedTransform extends Animated {
2626
let result: { [key: string]: unknown } = {}
2727
for (var key in transform) {
2828
var value = transform[key]
29-
result[key] =
30-
value instanceof Animated ? value.getAnimatedValue() : value
29+
result[key] = isAnimated(value) ? value.getAnimatedValue() : value
3130
}
3231
return result
3332
})
@@ -37,7 +36,7 @@ export class AnimatedTransform extends Animated {
3736
this._transforms.forEach(transform => {
3837
for (var key in transform) {
3938
var value = transform[key]
40-
if (value instanceof Animated) value.addChild(this)
39+
if (isAnimated(value)) value.addChild(this)
4140
}
4241
})
4342
}
@@ -46,7 +45,7 @@ export class AnimatedTransform extends Animated {
4645
this._transforms.forEach(transform => {
4746
for (var key in transform) {
4847
var value = transform[key]
49-
if (value instanceof Animated) value.removeChild(this)
48+
if (isAnimated(value)) value.removeChild(this)
5049
}
5150
})
5251
}

0 commit comments

Comments
 (0)