react-native-reanimated
Version:
More powerful alternative to Animated library for React Native.
135 lines (129 loc) • 4.43 kB
JavaScript
;
import { scheduleOnRN, scheduleOnUI } from 'react-native-worklets';
import { processColorsInProps, processStylePropInPlace, processTransform, processTransformOrigin, stylePropsBuilder } from "../common/index.js";
import jsPropsUpdater from '../createAnimatedComponent/JSPropsUpdater';
import { getStaticFeatureFlag } from "../featureFlags/index.js";
import { requestSettledPropsSync } from '../PropsRegistryGarbageCollector';
const USE_ANIMATION_BACKEND = getStaticFeatureFlag('USE_ANIMATION_BACKEND');
const updateProps = (viewDescriptors, updates, isAnimatedProps = false) => {
'worklet';
// TODO: Remove this if once we have SVG props builder implemented
// We need to keep it for now to prevent regression in SVG props processing
if (isAnimatedProps) {
processColorsInProps(updates);
if ('transformOrigin' in updates) {
processStylePropInPlace(updates, 'transformOrigin', processTransformOrigin);
}
if ('transform' in updates) {
processStylePropInPlace(updates, 'transform', processTransform);
}
}
global.UpdatePropsManager.update(viewDescriptors,
// Use props builder only for style updaters, since animated props
// can contain any properties of different types, depending on the
// component, which we cannot process properly with the props builder.
isAnimatedProps ? updates : stylePropsBuilder.build(updates), isAnimatedProps);
};
export default updateProps;
function updateJSProps(operations) {
jsPropsUpdater.updateProps(operations);
}
function hasAnyProp(object) {
'worklet';
for (const _ in object) {
return true;
}
return false;
}
function createUpdatePropsManager() {
'worklet';
const nativeOperations = [];
let jsOperations = [];
let flushPending = false;
const processViewUpdates = (tag, updates, isAnimatedProps) => {
const jsPropNames = global._tagToJSPropNamesMapping[tag];
if (!jsPropNames) {
// Fast path for the common case of a view without JS props: hand the
// updates over as they are instead of copying them every frame. Native
// reads them without modifying them, except with USE_ANIMATION_BACKEND,
// which rounds text metrics in place. Animated props are the updater's
// own object, so in that case they are copied to keep its last values.
if (!hasAnyProp(updates)) {
return {};
}
return {
nativePropUpdates: isAnimatedProps && USE_ANIMATION_BACKEND ? {
...updates
} : updates
};
}
let nativePropUpdates;
let jsPropUpdates;
for (const propName in updates) {
if (jsPropNames[propName]) {
jsPropUpdates ??= {};
jsPropUpdates[propName] = updates[propName];
} else {
nativePropUpdates ??= {};
nativePropUpdates[propName] = updates[propName];
}
}
return {
nativePropUpdates,
jsPropUpdates
};
};
return {
update(viewDescriptors, updates, isAnimatedProps) {
viewDescriptors.value.forEach(({
tag,
shadowNodeWrapper
}) => {
const viewTag = tag;
const {
nativePropUpdates,
jsPropUpdates
} = processViewUpdates(viewTag, updates, isAnimatedProps);
if (nativePropUpdates) {
nativeOperations.push({
shadowNodeWrapper,
updates: nativePropUpdates,
isAnimatedProps
});
}
if (jsPropUpdates) {
jsOperations.push({
tag: viewTag,
updates: jsPropUpdates
});
}
if (!flushPending && (nativePropUpdates || jsPropUpdates)) {
global.__requestMapperRunFinalizer(this.flush);
flushPending = true;
}
});
},
flush() {
if (nativeOperations.length) {
global._updateProps(nativeOperations);
nativeOperations.length = 0;
requestSettledPropsSync();
}
if (jsOperations.length) {
// Fresh array each flush: scheduleOnRN caches serialized args by identity.
scheduleOnRN(updateJSProps, jsOperations);
jsOperations = [];
}
flushPending = false;
if (!USE_ANIMATION_BACKEND) {
global._maybeFlushUIUpdatesQueue();
}
}
};
}
// is-tree-shakable-suppress
scheduleOnUI(() => {
'worklet';
global.UpdatePropsManager = createUpdatePropsManager();
});
//# sourceMappingURL=updateProps.native.js.map