react-native-reanimated
Version:
More powerful alternative to Animated library for React Native.
165 lines (163 loc) • 5.13 kB
JavaScript
;
import { createSynchronizable, scheduleOnRN } from 'react-native-worklets';
import { IS_JEST } from "./common/index.js";
import { unprocessColor, unprocessColorsInProps } from "./common/style/processors/colors.js";
import { unprocessObjectFit } from "./common/style/processors/image.js";
import { unprocessVerticalAlign } from "./common/style/processors/text.js";
import { getStaticFeatureFlag } from "./featureFlags/index.js";
import { ReanimatedModule } from './ReanimatedModule';
const FLUSH_INTERVAL_MS = 500;
const SYNC_SETTLED_PROPS = getStaticFeatureFlag('FORCE_REACT_RENDER_FOR_SETTLED_ANIMATIONS') && !getStaticFeatureFlag('USE_ANIMATION_BACKEND');
const syncRequested = SYNC_SETTLED_PROPS && !IS_JEST ? createSynchronizable(false, {
fixedType: true
}) : null;
export function requestSettledPropsSync() {
'worklet';
if (!syncRequested) {
return;
}
let shouldStartSync = false;
syncRequested.setBlocking(requested => {
shouldStartSync = !requested;
return true;
});
if (shouldStartSync) {
scheduleOnRN(startSettledPropsSync);
}
}
function startSettledPropsSync() {
PropsRegistryGarbageCollector.startSync();
}
export const PropsRegistryGarbageCollector = {
viewsMap: new Map(),
intervalId: null,
registerView(viewTag, component) {
if (this.viewsMap.has(viewTag)) {
// In case of nested AnimatedComponents (like <GestureDetector> with <Animated.View> inside),
// `registerView` method is called first for the inner component (e.g. <Animated.View>)
// and then second time for the outer component (e.g. <GestureDetector>).
// Both of these components have the same viewTag so the inner component will be overwritten
// with the outer one. That's why we need to skip the logic during any subsequent calls.
return;
}
this.viewsMap.set(viewTag, component);
this.startSync();
},
unregisterView(viewTag) {
const deleted = this.viewsMap.delete(viewTag);
if (deleted && this.viewsMap.size === 0) {
this.unregisterInterval();
syncRequested?.setBlocking(false);
}
},
startSync() {
if (this.viewsMap.size === 0) {
return;
}
if (this.intervalId === null) {
this.registerInterval();
}
},
isViewRegistered: viewTag => PropsRegistryGarbageCollector.viewsMap.has(viewTag),
syncPropsBackToReact() {
const settledUpdates = ReanimatedModule.getSettledUpdates(this.isViewRegistered);
for (const settledUpdate of settledUpdates) {
const {
viewTag,
props,
style
} = settledUpdate;
const component = this.viewsMap.get(viewTag);
unprocessSettledUpdate(settledUpdate);
component?._syncStylePropsBackToReact(props, style);
}
syncRequested?.setBlocking(false);
if (!ReanimatedModule.hasPendingSettledUpdates(this.isViewRegistered)) {
this.unregisterInterval();
}
},
registerInterval() {
this.intervalId = setInterval(this.syncPropsBackToReact.bind(this), FLUSH_INTERVAL_MS);
},
unregisterInterval() {
if (this.intervalId !== null) {
clearInterval(this.intervalId);
this.intervalId = null;
}
}
};
export function unprocessSettledUpdate({
props,
style,
keysLastWrittenByAnimatedStyle
}) {
unprocessColorsInProps(props);
unprocessColorsInProps(style);
for (const key of keysLastWrittenByAnimatedStyle) {
const unprocess = STYLE_BUILDER_VALUE_UNPROCESSORS[key];
if (!unprocess) {
continue;
}
for (const settledValues of [props, style]) {
if (key in settledValues) {
settledValues[key] = unprocess(settledValues[key]);
}
}
}
if (keysLastWrittenByAnimatedStyle.includes('resizeMode')) {
unprocessObjectFit(style);
}
if (keysLastWrittenByAnimatedStyle.includes('textAlignVertical')) {
unprocessVerticalAlign(style);
}
}
const STYLE_BUILDER_VALUE_UNPROCESSORS = {
boxShadow: unprocessBoxShadow,
backgroundImage: unprocessBackgroundImage,
// eslint-disable-next-line eslint-core/camelcase
experimental_backgroundImage: unprocessBackgroundImage
};
function unprocessBoxShadow(value) {
if (!Array.isArray(value)) {
return value;
}
return value.map(boxShadow => ({
...boxShadow,
color: unprocessColor(boxShadow.color)
}));
}
function unprocessBackgroundImage(value) {
if (!Array.isArray(value)) {
return value;
}
return value.map(backgroundImage => {
const colorStops = backgroundImage.colorStops.map(unprocessColorStop);
if (backgroundImage.type === 'linear-gradient') {
return {
...backgroundImage,
direction: unprocessDirection(backgroundImage.direction),
colorStops
};
}
return {
...backgroundImage,
colorStops
};
});
}
function unprocessDirection({
type,
value
}) {
return type === 'angle' ? `${value}deg` : value;
}
function unprocessColorStop({
color,
position
}) {
return {
color: color === null ? null : unprocessColor(color),
positions: position === null ? undefined : [position]
};
}
//# sourceMappingURL=PropsRegistryGarbageCollector.native.js.map