UNPKG

3.05 kBJavaScriptView Raw
1'use strict';
2
3export function valueSetter(mutable, value, forceUpdate = false) {
4 'worklet';
5
6 const previousAnimation = mutable._animation;
7 if (previousAnimation) {
8 previousAnimation.cancelled = true;
9 mutable._animation = null;
10 }
11 if (typeof value === 'function' || value !== null && typeof value === 'object' &&
12 // TODO TYPESCRIPT fix this after fixing AnimationObject type
13 value.onFrame !== undefined) {
14 const animation = typeof value === 'function' ?
15 // TODO TYPESCRIPT fix this after fixing AnimationObject type
16 value() :
17 // TODO TYPESCRIPT fix this after fixing AnimationObject type
18 value;
19 // prevent setting again to the same value
20 // and triggering the mappers that treat this value as an input
21 // this happens when the animation's target value(stored in animation.current until animation.onStart is called) is set to the same value as a current one(this._value)
22 // built in animations that are not higher order(withTiming, withSpring) hold target value in .current
23 if (mutable._value === animation.current && !animation.isHigherOrder && !forceUpdate) {
24 animation.callback && animation.callback(true);
25 return;
26 }
27 // animated set
28 const initializeAnimation = timestamp => {
29 animation.onStart(animation, mutable.value, timestamp, previousAnimation);
30 };
31 const currentTimestamp = global.__frameTimestamp || global._getAnimationTimestamp();
32 initializeAnimation(currentTimestamp);
33 const step = newTimestamp => {
34 // Function `requestAnimationFrame` adds callback to an array, all the callbacks are flushed with function `__flushAnimationFrame`
35 // Usually we flush them inside function `nativeRequestAnimationFrame` and then the given timestamp is the timestamp of end of the current frame.
36 // However function `__flushAnimationFrame` may also be called inside `registerEventHandler` - then we get actual timestamp which is earlier than the end of the frame.
37
38 const timestamp = newTimestamp < (animation.timestamp || 0) ? animation.timestamp : newTimestamp;
39 if (animation.cancelled) {
40 animation.callback && animation.callback(false /* finished */);
41 return;
42 }
43 const finished = animation.onFrame(animation, timestamp);
44 animation.finished = true;
45 animation.timestamp = timestamp;
46 // TODO TYPESCRIPT
47 // For now I'll assume that `animation.current` is always defined
48 // but actually need to dive into animations to understand it
49 mutable._value = animation.current;
50 if (finished) {
51 animation.callback && animation.callback(true /* finished */);
52 } else {
53 requestAnimationFrame(step);
54 }
55 };
56 mutable._animation = animation;
57 step(currentTimestamp);
58 } else {
59 // prevent setting again to the same value
60 // and triggering the mappers that treat this value as an input
61 if (mutable._value === value && !forceUpdate) {
62 return;
63 }
64 mutable._value = value;
65 }
66}
67//# sourceMappingURL=valueSetter.js.map
\No newline at end of file