Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove makeRemote #5518

Merged
merged 5 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/createAnimatedComponent/PropsFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class PropsFilter implements IPropsFilter {
const processedStyle: StyleProps = styles.map((style) => {
if (style && style.viewDescriptors) {
// this is how we recognize styles returned by useAnimatedStyle
style.viewsRef.add(component);
style.viewsRef?.add(component);
tjzel marked this conversation as resolved.
Show resolved Hide resolved
if (component._isFirstRender) {
this._initialStyle = {
...style.initial.value,
Expand Down
4 changes: 1 addition & 3 deletions src/createAnimatedComponent/createAnimatedComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,7 @@ export function createAnimatedComponent(
_detachStyles() {
if (IS_WEB && this._styles !== null) {
for (const style of this._styles) {
if (style?.viewsRef) {
style.viewsRef.remove(this);
}
style.viewsRef.delete(this);
tjzel marked this conversation as resolved.
Show resolved Hide resolved
}
} else if (this._viewTag !== -1 && this._styles !== null) {
for (const style of this._styles) {
Expand Down
14 changes: 13 additions & 1 deletion src/reanimated2/ViewDescriptorsSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useRef } from 'react';
import { makeMutable } from './core';
import type { SharedValue } from './commonTypes';
import type { Descriptor } from './hook/commonTypes';
import { shouldBeUseWeb } from './PlatformChecker';

export interface ViewRefSet<T> {
items: Set<T>;
Expand Down Expand Up @@ -51,7 +52,18 @@ export function makeViewDescriptorsSet(): ViewDescriptorsSet {
return data;
}

export function useViewRefSet<T>(): ViewRefSet<T> {
const SHOULD_BE_USE_WEB = shouldBeUseWeb();

export const useViewRefSet = SHOULD_BE_USE_WEB
? useViewRefSetJS
: useViewRefSetNative;

function useViewRefSetNative() {
// Stub native implementation.
return undefined;
}

function useViewRefSetJS<T>(): ViewRefSet<T> {
const ref = useRef<ViewRefSet<T> | null>(null);
if (ref.current === null) {
const data: ViewRefSet<T> = {
Expand Down
2 changes: 1 addition & 1 deletion src/reanimated2/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export { runOnJS, runOnUI } from './threads';
export { createWorkletRuntime, runOnRuntime } from './runtimes';
export type { WorkletRuntime } from './runtimes';
export { makeShareable, makeShareableCloneRecursive } from './shareables';
export { makeMutable, makeRemote } from './mutables';
export { makeMutable } from './mutables';

const IS_FABRIC = isFabric();

Expand Down
57 changes: 32 additions & 25 deletions src/reanimated2/hook/useAnimatedStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type { MutableRefObject } from 'react';
import { useEffect, useRef } from 'react';

import { startMapper, stopMapper, makeRemote } from '../core';
import { startMapper, stopMapper } from '../core';
import updateProps, { updatePropsJestWrapper } from '../UpdateProps';
import { initialUpdaterRun } from '../animation';
import { useSharedValue } from './useSharedValue';
Expand Down Expand Up @@ -37,7 +37,7 @@ interface AnimatedState {
isAnimationCancelled: boolean;
}

interface AnimationRef {
interface AnimatedUpdaterData {
initial: {
value: AnimatedStyle<any>;
updater: () => AnimatedStyle<any>;
Expand Down Expand Up @@ -410,17 +410,17 @@ export function useAnimatedStyle<Style extends DefaultStyle>(
adapters?: WorkletFunction | WorkletFunction[],
isAnimatedProps = false
) {
const viewsRef: ViewRefSet<unknown> = useViewRefSet();
const initRef = useRef<AnimationRef>();
const viewsRef = useViewRefSet();
const animatedUpdaterData = useRef<AnimatedUpdaterData>();
let inputs = Object.values(updater.__closure ?? {});
if (SHOULD_BE_USE_WEB) {
if (!inputs.length && dependencies?.length) {
// let web work without a Babel/SWC plugin
// let web work without a Babel plugin
inputs = dependencies;
}
if (__DEV__ && !inputs.length && !dependencies && !updater.__workletHash) {
throw new Error(
`[Reanimated] \`useAnimatedStyle\` was used without a dependency array or Babel plugin. Please explicitly pass a dependency array, or enable the Babel/SWC plugin.
`[Reanimated] \`useAnimatedStyle\` was used without a dependency array or Babel plugin. Please explicitly pass a dependency array, or enable the Babel plugin.
For more, see the docs: \`https://docs.swmansion.com/react-native-reanimated/docs/guides/web-support#web-without-the-babel-plugin\`.`
);
}
Expand All @@ -431,8 +431,8 @@ For more, see the docs: \`https://docs.swmansion.com/react-native-reanimated/doc
: [adapters]
: [];
const adaptersHash = adapters ? buildWorkletsHash(adaptersArray) : null;
const animationsActive = useSharedValue<boolean>(true);
const animatedStyle: MutableRefObject<Style> = useRef<Style>({} as Style);
const areAnimationsActive = useSharedValue<boolean>(true);
const jestAnimatedStyle = useRef<Style>({} as Style);

// build dependencies
if (!dependencies) {
Expand All @@ -442,27 +442,28 @@ For more, see the docs: \`https://docs.swmansion.com/react-native-reanimated/doc
}
adaptersHash && dependencies.push(adaptersHash);

if (!initRef.current) {
if (!animatedUpdaterData.current) {
const initialStyle = initialUpdaterRun(updater);
validateAnimatedStyles(initialStyle);
initRef.current = {
if (__DEV__) {
validateAnimatedStyles(initialStyle);
}
animatedUpdaterData.current = {
initial: {
value: initialStyle,
updater: updater,
updater,
},
remoteState: makeRemote<AnimatedState>({
remoteState: {
tjzel marked this conversation as resolved.
Show resolved Hide resolved
last: initialStyle,
animations: {},
isAnimationCancelled: false,
isAnimationRunning: false,
}),
},
viewDescriptors: makeViewDescriptorsSet(),
};
}

const { initial, remoteState, viewDescriptors } = initRef.current;
const { initial, remoteState, viewDescriptors } = animatedUpdaterData.current;
const shareableViewDescriptors = viewDescriptors.shareableViewDescriptors;
const maybeViewRef = SHOULD_BE_USE_WEB ? viewsRef : undefined;

dependencies.push(shareableViewDescriptors);

Expand All @@ -487,9 +488,9 @@ For more, see the docs: \`https://docs.swmansion.com/react-native-reanimated/doc
shareableViewDescriptors,
updater,
remoteState,
maybeViewRef,
animationsActive,
animatedStyle,
viewsRef,
areAnimationsActive,
jestAnimatedStyle,
adaptersArray
);
};
Expand All @@ -500,8 +501,8 @@ For more, see the docs: \`https://docs.swmansion.com/react-native-reanimated/doc
shareableViewDescriptors,
updaterFn,
remoteState,
maybeViewRef,
animationsActive,
viewsRef,
areAnimationsActive,
isAnimatedProps
);
};
Expand All @@ -510,19 +511,25 @@ For more, see the docs: \`https://docs.swmansion.com/react-native-reanimated/doc
return () => {
stopMapper(mapperId);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, dependencies);

useEffect(() => {
animationsActive.value = true;
areAnimationsActive.value = true;
return () => {
animationsActive.value = false;
areAnimationsActive.value = false;
};
}, []);
}, [areAnimationsActive]);
tjzel marked this conversation as resolved.
Show resolved Hide resolved

checkSharedValueUsage(initial.value);

if (isJest()) {
return { viewDescriptors, initial, viewsRef, animatedStyle };
return {
viewDescriptors,
initial,
viewsRef,
animatedStyle: jestAnimatedStyle,
};
} else {
return { viewDescriptors, initial, viewsRef };
}
Expand Down
3 changes: 1 addition & 2 deletions src/reanimated2/hook/useHandler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
import { useEffect, useRef } from 'react';
import type { WorkletFunction } from '../commonTypes';
import { makeRemote } from '../core';
import { isWeb, isJest } from '../PlatformChecker';
import type { DependencyList, ReanimatedEvent } from './commonTypes';
import { areDependenciesEqual, buildDependencies } from './utils';
Expand Down Expand Up @@ -66,7 +65,7 @@ export function useHandler<
const initRef = useRef<ContextWithDependencies<Context> | null>(null);
if (initRef.current === null) {
initRef.current = {
context: makeRemote<Context>({} as Context),
context: {} as Context,
tjzel marked this conversation as resolved.
Show resolved Hide resolved
savedDependencies: [],
};
}
Expand Down
11 changes: 0 additions & 11 deletions src/reanimated2/mutables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,3 @@ export function makeMutable<T>(
registerShareableMapping(mutable, handle);
return mutable;
}

export function makeRemote<T extends object>(initial: T = {} as T): T {
const handle = makeShareableCloneRecursive({
__init: () => {
'worklet';
return initial;
},
});
registerShareableMapping(initial, handle);
return initial;
}