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

useSyncSharedValue #5662

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 44 additions & 0 deletions src/hooks/useSyncSharedValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { useAnimatedReaction, runOnJS } from 'react-native-reanimated';
import { deepEqualWorklet, shallowEqualWorklet } from '@/worklets/comparisons';

type SetStateType<T, D> = D extends 'sharedToState' ? (value: T) => void : never;

interface SyncParams<T, D extends 'sharedToState' | 'stateToShared'> {
sharedValue: { value: T };
state: T;
setState: SetStateType<T, D>;
syncDirection: D;
compareDepth?: 'shallow' | 'deep';
}

export function useSyncSharedValue<T, D extends 'sharedToState' | 'stateToShared'>({
sharedValue,
state,
setState,
christianbaroni marked this conversation as resolved.
Show resolved Hide resolved
syncDirection,
compareDepth = 'shallow',
}: SyncParams<T, D>) {
useAnimatedReaction(
() => {
if (typeof sharedValue.value === 'object' && sharedValue.value !== null && typeof state === 'object' && state !== null) {
const isEqual =
compareDepth === 'deep'
? deepEqualWorklet(sharedValue.value as Record<string, any>, state as Record<string, any>)
: shallowEqualWorklet(sharedValue.value as Record<string, any>, state as Record<string, any>);
return !isEqual;
}
return sharedValue.value !== state;
},
shouldSync => {
if (shouldSync) {
if (syncDirection === 'sharedToState') {
runOnJS(setState)(sharedValue.value);
} else {
sharedValue.value = state;
}
}
}
);
}
36 changes: 36 additions & 0 deletions src/worklets/comparisons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
export function deepEqualWorklet(obj1: Record<string, any>, obj2: Record<string, any>): boolean {
'worklet';
if (Object.is(obj1, obj2)) {
return true;
}
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
if (keys1.length !== keys2.length) {
return false;
}
for (const key of keys1) {
if (!(key in obj2) || !deepEqualWorklet(obj1[key], obj2[key])) {
return false;
}
}
return true;
}

/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
export function shallowEqualWorklet(obj1: Record<string, any>, obj2: Record<string, any>): boolean {
'worklet';
if (Object.is(obj1, obj2)) {
return true;
}
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
if (keys1.length !== keys2.length) return false;
for (const key of keys1) {
if (obj1[key] !== obj2[key]) {
return false;
}
}
return true;
}

Loading