Skip to content

πŸ› Update shared value from JS to useFrameProcessor workletΒ #3701

@jgatto1

Description

@jgatto1

What's happening?

Hey guys! I need to update a shared value from JS to the worklet code running on the useFrameProcessor function but it never updates the code on the worklet side but only on the JS side. How can I properly update the shared value to keep it in sync? I've tried using the useSharedValue from react-native-worklets-core but sometimes crashes somehow and i is also slower. Sorry guys I'm getting too confused working with worklets.
Note: If I add on the useFrameProcessor deep a reactive value like useState it works and I'm able to read the updated value. Just wondering, is it a faster way?

Appreciate any help!

Reproduceable Code

import { useSharedValue } from "react-native-reanimated";

const [update, setUpdate] = useState(false);
const groupOfLinesShared = useSharedValue<number[][] | null>(null);

  useEffect(() => {
    const process = async () => {
      groupOfLinesShared.value = [...groupOfLinesShared.value || [], [2]];
      setUpdate(a => !a);
    };
    process();

    setInterval(() => {
      process();
    }, 3000);
  }, []);

const frameProcessor = useFrameProcessor(
    async (frame) => {
      "worklet";
      console.log('[WORKLET] groupOfLinesShared.value', groupOfLinesShared.value);
      // Here I pretend to get the updated groupOfLinesShared value but the only way is to update the deep of this processor.
      // If I remove the `update` it won't refresh.
}, [update]);

  return (
    <View style={StyleSheet.absoluteFill}>
      <Camera
        style={StyleSheet.absoluteFill}
        video
        device={device}
        ref={camera}
        // isActive={isCameraActive}
        isActive
        frameProcessor={frameProcessor}
        format={compatibleFormat}
        zoom={params.zoom || 1}
        fps={FPS_VALUE}
        outputOrientation="device"
      />
</View>

Relevant log output

LOG  [WORKLET] groupOfLinesShared.value null

Camera Device

{
  "neutralZoom": 1,
  "supportsRawCapture": false,
  "formats": [],
  "supportsLowLightBoost": false,
  "hardwareLevel": "full",
  "minExposure": -8,
  "position": "back",
  "hasFlash": true,
  "minZoom": 1,
  "physicalDevices": [
    "wide-angle-camera"
  ],
  "id": "com.apple.avfoundation.avcapturedevice.built-in_video:0",
  "hasTorch": true,
  "maxExposure": 8,
  "sensorOrientation": "portrait",
  "minFocusDistance": 20,
  "maxZoom": 123.75,
  "isMultiCam": false,
  "supportsFocus": true,
  "name": "Back Camera"
}

Device

Iphone 15 Pro

VisionCamera Version

4.7.2

Can you reproduce this issue in the VisionCamera Example app?

Yes, I can reproduce the same issue in the Example app here

Additional information

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions