English 中文(简体)
Multithreading in React Native with node:worker_threads
原标题:

I have a WebGL ES application that renders at 60 FPS and has expensive functions that take a few milliseconds.

Is it safe to use node:worker_threads in the root App.js file like so?

import {
  Worker, isMainThread, parentPort, workerData,
} from  node:worker_threads ;

export default function App() {
  return <div>my app</div>;
}

if (!isMainThread) {
  // Run expensive task here
}
问题回答

No it is not, not directly. You need to either "Nodeify" (such as https://www.npmjs.com/package/rn-nodeify) the React-Native app or you need to provide a stub that emulates the worker_thread functionality of node.

In web browsers, there is the Web Worker API. RN uses JavaScriptCore which is Apple s JS engine, which is wrapped by React-Native s C libraries that emulate some APIs that a Web Browser provides. Sadly it does not provide the same APIs as a Web Browser. I suggest you try some of the libraries on NPM. Many look pretty old, and its possible newer RN versions provide some similar functionality.

Consider https://reactnative.dev/docs/interactionmanager.html using RN s interaction manager to schedule your heavy process work.

I perused a few libraries on NPM, I d suggest trying this library first https://github.com/joltup/react-native-threads





相关问题
How to make Sequelize use singular table names

I have an model called User but Sequelize looks for the table USERS whenever I am trying to save in the DB. Does anyone know how to set Sequelize to use singular table names? Thanks.

What is Node.js? [closed]

I don t fully get what Node.js is all about. Maybe it s because I am mainly a web based business application developer. What is it and what is the use of it? My understanding so far is that: The ...

Clientside going serverside with node.js

I`ve been looking for a serverside language for some time, and python got my attention somewhat. But as I already know and love javascript, I now want learn to code on the server with js and node.js. ...

Can I use jQuery with Node.js?

Is it possible to use jQuery selectors/DOM manipulation on the server-side using Node.js?

How do I escape a string for a shell command in node?

In nodejs, the only way to execute external commands is via sys.exec(cmd). I d like to call an external command and give it data via stdin. In nodejs there does yet not appear to be a way to open a ...

热门标签