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

[v3][Web] setImmediate is not defined #4140

Closed
nandorojo opened this issue Mar 1, 2023 · 17 comments · Fixed by #4276
Closed

[v3][Web] setImmediate is not defined #4140

nandorojo opened this issue Mar 1, 2023 · 17 comments · Fixed by #4276
Assignees
Labels
Missing repro This issue need minimum repro scenario Needs review Issue is ready to be reviewed by a maintainer Platform: Web This issue is specific to web

Comments

@nandorojo
Copy link
Contributor

Description

This bug has appeared over the years many times. The solution has been to use requestAnimationFrame instead of setImmediate on Web.

The regression came from #3970

See: #2622 #152.

Steps to reproduce

Use Reanimated on Next.js.

Snack or a link to a repository

https://stackblitz.com/edit/nextjs-pxczpl?file=pages%2Findex.js,next.config.js,package.json

Reanimated version

3.0.0

React Native version

0.71

Platforms

Web

JavaScript runtime

None

Workflow

None

Architecture

None

Build type

None

Device

None

Device model

No response

Acknowledgements

Yes

@nandorojo nandorojo added the Needs review Issue is ready to be reviewed by a maintainer label Mar 1, 2023
@github-actions github-actions bot added Missing repro This issue need minimum repro scenario Platform: Web This issue is specific to web labels Mar 1, 2023
@github-actions
Copy link

github-actions bot commented Mar 1, 2023

Hey! 👋

The issue doesn't seem to contain a minimal reproduction.

Could you provide a snack or a link to a GitHub repository under your username that reproduces the problem?

@hdwatts
Copy link

hdwatts commented Mar 5, 2023

Im encountering this as well on 3.0.1

@nandorojo
Copy link
Contributor Author

there is indeed a reproduction.

@hdwatts
Copy link

hdwatts commented Mar 5, 2023

I solved this by:

npm i setimmediate
and before importing react-native-reanimated:

import "setimmediate"
import "react-native-reanimated"

However I'd prefer not to have this shim necessary

@nandorojo
Copy link
Contributor Author

yeah same

@wcandillon
Copy link
Contributor

I can confirm that RN Skia is affected by this issue as well.

@edkimmel
Copy link

Finally found the same issue. Webpack was swallowing the exception during module initialization -_-.

setImmediate is being called when undefined in both a minimal VM environment on node for SSR and in the browser.

@deggertsen
Copy link

deggertsen commented Mar 21, 2023

I have reproduced this as well. Not liking the workaround... Anyone have a patch in progress? Seems somewhat related to software-mansion/react-native-gesture-handler#2402

@deggertsen
Copy link

Looks like there is a PR here? #4189
I've been able to solve it short term with this patch.

diff --git a/node_modules/react-native-reanimated/lib/module/reanimated2/js-reanimated/index.js b/node_modules/react-native-reanimated/lib/module/reanimated2/js-reanimated/index.js
index 4f2ddfe..99cb340 100644
--- a/node_modules/react-native-reanimated/lib/module/reanimated2/js-reanimated/index.js
+++ b/node_modules/react-native-reanimated/lib/module/reanimated2/js-reanimated/index.js
@@ -1,9 +1,10 @@
 import JSReanimated from './JSReanimated';
+import { Platform } from 'react-native';
 const reanimatedJS = new JSReanimated();
 
 global._makeShareableClone = c => c;
 
-global._scheduleOnJS = setImmediate;
+global._scheduleOnJS = Platform.OS == 'web' ? requestAnimationFrame : setImmediate;
 export const _updatePropsJS = (updates, viewRef) => {
   if (viewRef._component) {
     const component = viewRef._component;
diff --git a/node_modules/react-native-reanimated/lib/module/reanimated2/mappers.js b/node_modules/react-native-reanimated/lib/module/reanimated2/mappers.js
index a0583c1..0d7f583 100644
--- a/node_modules/react-native-reanimated/lib/module/reanimated2/mappers.js
+++ b/node_modules/react-native-reanimated/lib/module/reanimated2/mappers.js
@@ -1,3 +1,4 @@
+import { Platform } from 'react-native';
 import { isJest } from './PlatformChecker';
 import { runOnUI } from './threads';
 const IS_JEST = isJest();
@@ -98,7 +99,8 @@ export function createMapperRegistry() {
       // if they want to make any assertions on the effects of animations being run.
       mapperRun();
     } else if (!runRequested) {
-      setImmediate(mapperRun);
+      const scheduleUpdate = Platform.OS == 'web' ? requestAnimationFrame : setImmediate;
+      scheduleUpdate(mapperRun);
       runRequested = true;
     }
   }
diff --git a/node_modules/react-native-reanimated/lib/module/reanimated2/threads.js b/node_modules/react-native-reanimated/lib/module/reanimated2/threads.js
index b6b81f0..bee6741 100644
--- a/node_modules/react-native-reanimated/lib/module/reanimated2/threads.js
+++ b/node_modules/react-native-reanimated/lib/module/reanimated2/threads.js
@@ -1,4 +1,5 @@
 import NativeReanimatedModule from './NativeReanimated';
+import { Platform } from 'react-native';
 import { isJest, shouldBeUseWeb } from './PlatformChecker';
 import { makeShareableCloneOnUIRecursive, makeShareableCloneRecursive } from './shareables';
 const IS_JEST = isJest();
@@ -69,7 +70,8 @@ export function runOnUI(worklet) {
     _runOnUIQueue.push([worklet, args]);
 
     if (_runOnUIQueue.length === 1) {
-      setImmediate(() => {
+      const scheduleUpdate = Platform.OS == 'web' ? requestAnimationFrame : setImmediate;
+      scheduleUpdate(() => {
         const queue = _runOnUIQueue;
         _runOnUIQueue = [];
         NativeReanimatedModule.scheduleOnUI(makeShareableCloneRecursive(() => {

@kmagiera
Copy link
Member

I opened that stackblitz link but it works alright for me, it looks like setImmediate gets somehow installed/polyfilled. What should I do to see that error?

I even tried downloading the project and starting it locally, but again, it all works fine

@nandorojo
Copy link
Contributor Author

adding import ‘setimmediate’ (after installing it) appears to help

@kmagiera
Copy link
Member

ok, I see it now. So the repro repository actually had setImmediate installed as dependency and imported in main project file hence it wasn't crashing. I remove that and now can see that error

@seyaobey-dev
Copy link

I'm still facing the same issue with latest reanimated 3.02 even with import setImmediate:
"react-native-reanimated": "^3.0.2",

image

@costaDZ
Copy link

costaDZ commented Apr 25, 2023

Adding

import "setimmediate"

in App.tsx root file solve my problem

fluiddot pushed a commit to wordpress-mobile/react-native-reanimated that referenced this issue Jun 5, 2023
## Summary

This PR removes uses of setImmediate which is not a standard API across
web browsers. Instead, we use queueMicrotask API which is available both
in the browser world, in React Native and in Node.

Fixes software-mansion#4140

## Test plan

Run example app, test WebExample, test repro app from software-mansion#4140 with `import
setImmediate` removed
@sleaper
Copy link

sleaper commented Jun 23, 2023

Adding

import "setimmediate"

in App.tsx root file solve my problem

I had the same issue in React native. I had to put import 'setimmediate' in the top of index.js file.

But thanks @costaDZ, you saved me.

@piaskowyk
Copy link
Member

3.3.0 version should resolve this problem.

@sleaper
Copy link

sleaper commented Jun 26, 2023

3.3.0 version should resolve this problem.

Then with 3.3.0 I get this error:
#4559 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Missing repro This issue need minimum repro scenario Needs review Issue is ready to be reviewed by a maintainer Platform: Web This issue is specific to web
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants