@rn-org/react-native-thread 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -25
- package/package.json +1 -1
- package/src/globals.d.ts +5 -4
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ Run JavaScript on real background threads in React Native — no Workers, no Wor
|
|
|
13
13
|
- [Quick start](#quick-start)
|
|
14
14
|
- [API reference](#api-reference)
|
|
15
15
|
- [runOnJS](#runonjs)
|
|
16
|
-
- [runOnNewJS](#
|
|
16
|
+
- [runOnNewJS](#runonewjs)
|
|
17
17
|
- [createThread](#createthread)
|
|
18
18
|
- [getThreads](#getthreads)
|
|
19
19
|
- [destroyThread](#destroythread)
|
|
@@ -24,6 +24,7 @@ Run JavaScript on real background threads in React Native — no Workers, no Wor
|
|
|
24
24
|
- [Thread globals](#thread-globals)
|
|
25
25
|
- [postMessage](#postmessage)
|
|
26
26
|
- [console](#console)
|
|
27
|
+
- [`__params__`](#__params__)
|
|
27
28
|
- [Hermes & Babel plugin](#hermes--babel-plugin-deep-dive)
|
|
28
29
|
- [Constraints](#constraints)
|
|
29
30
|
- [Contributing](#contributing)
|
|
@@ -106,10 +107,10 @@ runOnJS((args) => {
|
|
|
106
107
|
}, { limit: 42 });
|
|
107
108
|
|
|
108
109
|
// ── 2. Create a named, persistent thread ────────────────────────────────────
|
|
109
|
-
const
|
|
110
|
+
const thread = createThread('MyThread');
|
|
110
111
|
|
|
111
112
|
// Send work + params
|
|
112
|
-
|
|
113
|
+
thread.run(
|
|
113
114
|
(args) => {
|
|
114
115
|
var result = 0;
|
|
115
116
|
for (var i = 0; i < args.limit; i++) result += i;
|
|
@@ -119,22 +120,22 @@ worker.run(
|
|
|
119
120
|
);
|
|
120
121
|
|
|
121
122
|
// Option A — callback
|
|
122
|
-
const unsubscribe =
|
|
123
|
-
console.log('Result from
|
|
123
|
+
const unsubscribe = thread.onMessage((data) => {
|
|
124
|
+
console.log('Result from thread:', data);
|
|
124
125
|
});
|
|
125
126
|
|
|
126
127
|
// Option B — promise (resolves on the next message)
|
|
127
|
-
const data = await
|
|
128
|
-
console.log('Result from
|
|
128
|
+
const data = await thread.onMessage();
|
|
129
|
+
console.log('Result from thread:', data);
|
|
129
130
|
|
|
130
131
|
// ── 3. List all running threads ─────────────────────────────────────────────
|
|
131
132
|
console.log(getThreads());
|
|
132
|
-
// [{ id: 1, name: 'RNOrgThread' }, { id: 2, name: '
|
|
133
|
+
// [{ id: 1, name: 'RNOrgThread' }, { id: 2, name: 'MyThread' }]
|
|
133
134
|
|
|
134
135
|
// ── 4. Clean up ─────────────────────────────────────────────────────────────
|
|
135
136
|
unsubscribe();
|
|
136
|
-
|
|
137
|
-
destroyThread('
|
|
137
|
+
thread.destroy(); // by handle
|
|
138
|
+
destroyThread('MyThread'); // by name — same effect
|
|
138
139
|
destroyThread(2); // by id — same effect
|
|
139
140
|
```
|
|
140
141
|
|
|
@@ -199,7 +200,7 @@ Returns a snapshot of every live thread currently managed by the library, includ
|
|
|
199
200
|
const threads = getThreads();
|
|
200
201
|
// [
|
|
201
202
|
// { id: 1, name: 'RNOrgThread' },
|
|
202
|
-
// { id: 2, name: '
|
|
203
|
+
// { id: 2, name: 'MyThread' },
|
|
203
204
|
// ]
|
|
204
205
|
```
|
|
205
206
|
|
|
@@ -216,7 +217,7 @@ Destroys a thread and frees its resources. Accepts either the numeric thread ID
|
|
|
216
217
|
This is the same function called by `ThreadHandle.destroy()`.
|
|
217
218
|
|
|
218
219
|
```ts
|
|
219
|
-
destroyThread('
|
|
220
|
+
destroyThread('MyThread'); // by name
|
|
220
221
|
destroyThread(2); // by id
|
|
221
222
|
```
|
|
222
223
|
|
|
@@ -316,17 +317,17 @@ declare function postMessage(data: unknown): void
|
|
|
316
317
|
Sends `data` back to the main JS thread. The value is JSON-serialised in the background thread and JSON-parsed before reaching the `onMessage` handler. Must be JSON-serialisable (`object`, `array`, `string`, `number`, `boolean`, or `null`).
|
|
317
318
|
|
|
318
319
|
```ts
|
|
319
|
-
|
|
320
|
+
thread.run((args) => {
|
|
320
321
|
postMessage({ status: 'done', value: args.multiply * 2 });
|
|
321
322
|
}, { multiply: 21 });
|
|
322
323
|
|
|
323
324
|
// Callback
|
|
324
|
-
|
|
325
|
+
thread.onMessage((data) => {
|
|
325
326
|
console.log(data); // { status: 'done', value: 42 }
|
|
326
327
|
});
|
|
327
328
|
|
|
328
329
|
// Promise
|
|
329
|
-
const data = await
|
|
330
|
+
const data = await thread.onMessage();
|
|
330
331
|
console.log(data); // { status: 'done', value: 42 }
|
|
331
332
|
```
|
|
332
333
|
|
|
@@ -347,16 +348,24 @@ console.log(data); // { status: 'done', value: 42 }
|
|
|
347
348
|
declare const __params__: any
|
|
348
349
|
```
|
|
349
350
|
|
|
350
|
-
Injected by the library when you pass a second argument to `run()`, `runOnJS()`, or `runOnNewJS()`. The value is JSON-serialised on the main thread and prepended to the code string as
|
|
351
|
+
Injected by the library when you pass a second argument to `run()`, `runOnJS()`, or `runOnNewJS()`. The value is JSON-serialised on the main thread and prepended to the code string as `var __params__ = <JSON>;`.
|
|
351
352
|
|
|
352
|
-
|
|
353
|
-
var __params__ = <JSON>;
|
|
354
|
-
```
|
|
355
|
-
|
|
356
|
-
Must be JSON-serialisable. If you access `__params__` without passing a value, it will be `undefined`.
|
|
353
|
+
You can access the params value in two ways:
|
|
357
354
|
|
|
358
355
|
```ts
|
|
359
|
-
|
|
356
|
+
// Option A — args callback parameter
|
|
357
|
+
thread.run(
|
|
358
|
+
(args) => {
|
|
359
|
+
for (var i = 0; i < args.iterations; i++) {
|
|
360
|
+
// ...
|
|
361
|
+
}
|
|
362
|
+
postMessage('done');
|
|
363
|
+
},
|
|
364
|
+
{ iterations: 50_000 }
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
// Option B — __params__ global (works in both functions and raw code strings)
|
|
368
|
+
thread.run(
|
|
360
369
|
() => {
|
|
361
370
|
for (var i = 0; i < __params__.iterations; i++) {
|
|
362
371
|
// ...
|
|
@@ -365,6 +374,12 @@ worker.run(
|
|
|
365
374
|
},
|
|
366
375
|
{ iterations: 50_000 }
|
|
367
376
|
);
|
|
377
|
+
|
|
378
|
+
// Option C — raw code string (only __params__ is available)
|
|
379
|
+
thread.run(
|
|
380
|
+
'for (var i = 0; i < __params__.iterations; i++) {} postMessage("done")',
|
|
381
|
+
{ iterations: 50_000 }
|
|
382
|
+
);
|
|
368
383
|
```
|
|
369
384
|
|
|
370
385
|
---
|
|
@@ -387,12 +402,12 @@ The included Babel plugin runs at **compile time** — before Hermes touches the
|
|
|
387
402
|
|
|
388
403
|
```js
|
|
389
404
|
// Input (your source)
|
|
390
|
-
|
|
405
|
+
thread.run((args) => {
|
|
391
406
|
postMessage(args.greeting);
|
|
392
407
|
}, { greeting: 'hello' });
|
|
393
408
|
|
|
394
409
|
// Output (what Hermes compiles)
|
|
395
|
-
|
|
410
|
+
thread.run("((args) => {\n postMessage(args.greeting);\n})(__params__)", { greeting: 'hello' });
|
|
396
411
|
```
|
|
397
412
|
|
|
398
413
|
The second `params` argument is left untouched; only the first (function) argument is transformed.
|
|
@@ -409,7 +424,7 @@ The second `params` argument is left untouched; only the first (function) argume
|
|
|
409
424
|
### Limitations
|
|
410
425
|
|
|
411
426
|
- Thread functions are **self-contained**: they run in an isolated JS engine with no access to the outer closure, imported modules, or the React tree.
|
|
412
|
-
- All values must be passed explicitly via the `args` parameter or `postMessage`.
|
|
427
|
+
- All values must be passed explicitly via the `args` parameter, `__params__`, or `postMessage`.
|
|
413
428
|
- Thread function bodies must be **ASCII-safe**: Rhino (Android) does not support non-ASCII identifier characters in source mode.
|
|
414
429
|
- The `params` value must be **JSON-serialisable**: functions, `undefined`, `Map`, `Set`, etc. are not supported.
|
|
415
430
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rn-org/react-native-thread",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Run JavaScript on real background threads in React Native — no Workers, no Worklets. Uses JavaScriptCore on iOS and Mozilla Rhino on Android. Built as a New Architecture TurboModule.",
|
|
5
5
|
"main": "./lib/module/index.js",
|
|
6
6
|
"types": "./lib/typescript/src/index.d.ts",
|
package/src/globals.d.ts
CHANGED
|
@@ -23,12 +23,13 @@ declare function postMessage(data: unknown): void;
|
|
|
23
23
|
* JSON-compatible value.
|
|
24
24
|
*
|
|
25
25
|
* @example
|
|
26
|
-
*
|
|
26
|
+
* thread.run((args) => {
|
|
27
27
|
* console.log(args.name); // 'hello'
|
|
28
28
|
* }, { name: 'hello' });
|
|
29
29
|
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
30
|
+
* // or via __params__
|
|
31
|
+
* thread.run(() => {
|
|
32
|
+
* console.log(__params__.name); // 'hello'
|
|
33
|
+
* }, { name: 'hello' });
|
|
33
34
|
*/
|
|
34
35
|
declare const __params__: any;
|