@rn-org/react-native-thread 0.3.0 → 0.5.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 +15 -15
- package/android/src/main/java/com/rnorg/reactnativethread/ReactNativeThreadModule.kt +3 -3
- package/ios/ReactNativeThread.mm +1 -1
- package/lib/module/index.js +18 -0
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/globals.d.ts +2 -2
- package/src/index.tsx +19 -0
package/README.md
CHANGED
|
@@ -22,7 +22,7 @@ Run JavaScript on real background threads in React Native — no Workers, no Wor
|
|
|
22
22
|
- [ThreadInfo](#threadinfo)
|
|
23
23
|
- [ThreadTask](#threadtask)
|
|
24
24
|
- [Thread globals](#thread-globals)
|
|
25
|
-
- [
|
|
25
|
+
- [resolveThreadMessage](#resolvethreadmessage)
|
|
26
26
|
- [console](#console)
|
|
27
27
|
- [`__params__`](#__params__)
|
|
28
28
|
- [Hermes & Babel plugin](#hermes--babel-plugin-deep-dive)
|
|
@@ -37,7 +37,7 @@ Run JavaScript on real background threads in React Native — no Workers, no Wor
|
|
|
37
37
|
- **True background threads** — each thread runs its own JS engine on an OS-level thread; the main Hermes/JSC runtime is never blocked.
|
|
38
38
|
- **Unlimited threads** — create as many threads as you need; each is isolated.
|
|
39
39
|
- **Shared thread** (`runOnJS`) — fire-and-forget tasks on a single persistent background thread; no teardown required.
|
|
40
|
-
- **Per-thread message passing** — call `
|
|
40
|
+
- **Per-thread message passing** — call `resolveThreadMessage(data)` from any thread, receive it on the main thread with `onMessage` (callback or `await`).
|
|
41
41
|
- **Parameter injection** — pass a JSON-serialisable value from the main thread into the background function as a typed argument `(args) => { ... }`.
|
|
42
42
|
- **Named threads** — give threads friendly names; list or destroy them by name.
|
|
43
43
|
- **Full `console` support** — `console.log/info/warn/error/debug` work inside threads and appear in Logcat / Xcode logs.
|
|
@@ -114,7 +114,7 @@ thread.run(
|
|
|
114
114
|
(args) => {
|
|
115
115
|
var result = 0;
|
|
116
116
|
for (var i = 0; i < args.limit; i++) result += i;
|
|
117
|
-
|
|
117
|
+
resolveThreadMessage({ sum: result });
|
|
118
118
|
},
|
|
119
119
|
{ limit: 1_000_000 }
|
|
120
120
|
);
|
|
@@ -237,7 +237,7 @@ function onMessage(
|
|
|
237
237
|
function onMessage(): Promise<{ data: unknown; threadId: number }>
|
|
238
238
|
```
|
|
239
239
|
|
|
240
|
-
Global listener that fires whenever **any** thread calls `
|
|
240
|
+
Global listener that fires whenever **any** thread calls `resolveThreadMessage(data)`. Prefer `ThreadHandle.onMessage` if you want messages scoped to a single thread.
|
|
241
241
|
|
|
242
242
|
```ts
|
|
243
243
|
// Callback variant
|
|
@@ -275,7 +275,7 @@ type ThreadHandle = {
|
|
|
275
275
|
| `id` | Numeric ID assigned by the native layer. |
|
|
276
276
|
| `name` | Display name provided at creation (or the default `RNThread-<id>`). |
|
|
277
277
|
| `run(task, params?)` | Execute `task` on this thread. `params` is passed as the first argument to the function. Can be called multiple times. |
|
|
278
|
-
| `onMessage(handler)` | Subscribe to `
|
|
278
|
+
| `onMessage(handler)` | Subscribe to `resolveThreadMessage` output from **this thread only**. Returns an unsubscribe function. |
|
|
279
279
|
| `onMessage()` | Returns a `Promise` that resolves with the next message from **this thread only**, then auto-unsubscribes. |
|
|
280
280
|
| `destroy()` | Shut down the thread and remove it from the registry. Equivalent to calling `destroyThread(handle.id)`. |
|
|
281
281
|
|
|
@@ -308,17 +308,17 @@ Either an arrow function / function expression (transformed by the Babel plugin)
|
|
|
308
308
|
|
|
309
309
|
These globals are available inside every thread function:
|
|
310
310
|
|
|
311
|
-
### `
|
|
311
|
+
### `resolveThreadMessage`
|
|
312
312
|
|
|
313
313
|
```ts
|
|
314
|
-
declare function
|
|
314
|
+
declare function resolveThreadMessage(data: unknown): void
|
|
315
315
|
```
|
|
316
316
|
|
|
317
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`).
|
|
318
318
|
|
|
319
319
|
```ts
|
|
320
320
|
thread.run((args) => {
|
|
321
|
-
|
|
321
|
+
resolveThreadMessage({ status: 'done', value: args.multiply * 2 });
|
|
322
322
|
}, { multiply: 21 });
|
|
323
323
|
|
|
324
324
|
// Callback
|
|
@@ -359,7 +359,7 @@ thread.run(
|
|
|
359
359
|
for (var i = 0; i < args.iterations; i++) {
|
|
360
360
|
// ...
|
|
361
361
|
}
|
|
362
|
-
|
|
362
|
+
resolveThreadMessage('done');
|
|
363
363
|
},
|
|
364
364
|
{ iterations: 50_000 }
|
|
365
365
|
);
|
|
@@ -370,14 +370,14 @@ thread.run(
|
|
|
370
370
|
for (var i = 0; i < __params__.iterations; i++) {
|
|
371
371
|
// ...
|
|
372
372
|
}
|
|
373
|
-
|
|
373
|
+
resolveThreadMessage('done');
|
|
374
374
|
},
|
|
375
375
|
{ iterations: 50_000 }
|
|
376
376
|
);
|
|
377
377
|
|
|
378
378
|
// Option C — raw code string (only __params__ is available)
|
|
379
379
|
thread.run(
|
|
380
|
-
'for (var i = 0; i < __params__.iterations; i++) {}
|
|
380
|
+
'for (var i = 0; i < __params__.iterations; i++) {} resolveThreadMessage("done")',
|
|
381
381
|
{ iterations: 50_000 }
|
|
382
382
|
);
|
|
383
383
|
```
|
|
@@ -403,11 +403,11 @@ The included Babel plugin runs at **compile time** — before Hermes touches the
|
|
|
403
403
|
```js
|
|
404
404
|
// Input (your source)
|
|
405
405
|
thread.run((args) => {
|
|
406
|
-
|
|
406
|
+
resolveThreadMessage(args.greeting);
|
|
407
407
|
}, { greeting: 'hello' });
|
|
408
408
|
|
|
409
409
|
// Output (what Hermes compiles)
|
|
410
|
-
thread.run("((args) => {\n
|
|
410
|
+
thread.run("((args) => {\n resolveThreadMessage(args.greeting);\n})(__params__)", { greeting: 'hello' });
|
|
411
411
|
```
|
|
412
412
|
|
|
413
413
|
The second `params` argument is left untouched; only the first (function) argument is transformed.
|
|
@@ -424,7 +424,7 @@ The second `params` argument is left untouched; only the first (function) argume
|
|
|
424
424
|
### Limitations
|
|
425
425
|
|
|
426
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.
|
|
427
|
-
- All values must be passed explicitly via the `args` parameter, `__params__`, or `
|
|
427
|
+
- All values must be passed explicitly via the `args` parameter, `__params__`, or `resolveThreadMessage`.
|
|
428
428
|
- Thread function bodies must be **ASCII-safe**: Rhino (Android) does not support non-ASCII identifier characters in source mode.
|
|
429
429
|
- The `params` value must be **JSON-serialisable**: functions, `undefined`, `Map`, `Set`, etc. are not supported.
|
|
430
430
|
|
|
@@ -436,7 +436,7 @@ The second `params` argument is left untouched; only the first (function) argume
|
|
|
436
436
|
|---|---|
|
|
437
437
|
| Functions are closure-isolated | They run in a completely separate JS engine |
|
|
438
438
|
| `params` must be JSON-serialisable | Serialised and passed as a function argument |
|
|
439
|
-
| `
|
|
439
|
+
| `resolveThreadMessage` payload must be JSON-serialisable | Transported as a JSON string over the bridge |
|
|
440
440
|
| Function body must be ASCII-safe | Rhino parser limitation |
|
|
441
441
|
| New Architecture required | TurboModule / Codegen only; no bridge fallback |
|
|
442
442
|
|
|
@@ -43,7 +43,7 @@ class ReactNativeThreadModule(reactContext: ReactApplicationContext) :
|
|
|
43
43
|
cx.optimizationLevel = -1
|
|
44
44
|
val scope = cx.initStandardObjects()
|
|
45
45
|
injectConsole(scope, id)
|
|
46
|
-
|
|
46
|
+
injectResolveThreadMessage(scope, id)
|
|
47
47
|
scopeFuture.complete(scope)
|
|
48
48
|
} catch (e: Exception) {
|
|
49
49
|
scopeFuture.completeExceptionally(e)
|
|
@@ -81,11 +81,11 @@ class ReactNativeThreadModule(reactContext: ReactApplicationContext) :
|
|
|
81
81
|
entry.executor.shutdown()
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
private fun
|
|
84
|
+
private fun injectResolveThreadMessage(scope: Scriptable, threadId: Long) {
|
|
85
85
|
val ctx = reactApplicationContext
|
|
86
86
|
ScriptableObject.putProperty(
|
|
87
87
|
scope as ScriptableObject,
|
|
88
|
-
"
|
|
88
|
+
"resolveThreadMessage",
|
|
89
89
|
object : BaseFunction() {
|
|
90
90
|
override fun call(
|
|
91
91
|
cx: RhinoContext,
|
package/ios/ReactNativeThread.mm
CHANGED
|
@@ -79,7 +79,7 @@
|
|
|
79
79
|
|
|
80
80
|
NSUInteger capturedTid = tidForLog;
|
|
81
81
|
__weak ReactNativeThread *weakSelf = self;
|
|
82
|
-
t.context[@"
|
|
82
|
+
t.context[@"resolveThreadMessage"] = ^(JSValue *data) {
|
|
83
83
|
JSValue *jsonStr = [data.context[@"JSON"] invokeMethod:@"stringify"
|
|
84
84
|
withArguments:@[data]];
|
|
85
85
|
NSString *serialised = [jsonStr toString];
|
package/lib/module/index.js
CHANGED
|
@@ -5,6 +5,12 @@ import ReactNativeThread from "./NativeReactNativeThread.js";
|
|
|
5
5
|
const _emitter = new NativeEventEmitter(ReactNativeThread);
|
|
6
6
|
const RN_THREAD_MESSAGE_EVENT = 'RNThreadMessage';
|
|
7
7
|
const _registry = new Map();
|
|
8
|
+
function _findByName(name) {
|
|
9
|
+
for (const info of _registry.values()) {
|
|
10
|
+
if (info.name === name) return info;
|
|
11
|
+
}
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
8
14
|
function _register(id, name) {
|
|
9
15
|
_registry.set(id, {
|
|
10
16
|
id,
|
|
@@ -42,6 +48,12 @@ export function runOnJS(task, params) {
|
|
|
42
48
|
ReactNativeThread.runOnThread(getSharedThread(), toCode(task, params));
|
|
43
49
|
}
|
|
44
50
|
export function runOnNewJS(task, params, name) {
|
|
51
|
+
if (name != null) {
|
|
52
|
+
const existing = _findByName(name);
|
|
53
|
+
if (existing) {
|
|
54
|
+
return createThreadHandle(existing.id, existing.name);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
45
57
|
const id = ReactNativeThread.createThread();
|
|
46
58
|
const resolvedName = name ?? `RNThread-${id}`;
|
|
47
59
|
_register(id, resolvedName);
|
|
@@ -49,6 +61,12 @@ export function runOnNewJS(task, params, name) {
|
|
|
49
61
|
return createThreadHandle(id, resolvedName);
|
|
50
62
|
}
|
|
51
63
|
export function createThread(name) {
|
|
64
|
+
if (name != null) {
|
|
65
|
+
const existing = _findByName(name);
|
|
66
|
+
if (existing) {
|
|
67
|
+
return createThreadHandle(existing.id, existing.name);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
52
70
|
const id = ReactNativeThread.createThread();
|
|
53
71
|
const resolvedName = name ?? `RNThread-${id}`;
|
|
54
72
|
_register(id, resolvedName);
|
package/lib/module/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["NativeEventEmitter","ReactNativeThread","_emitter","RN_THREAD_MESSAGE_EVENT","_registry","Map","
|
|
1
|
+
{"version":3,"names":["NativeEventEmitter","ReactNativeThread","_emitter","RN_THREAD_MESSAGE_EVENT","_registry","Map","_findByName","name","info","values","undefined","_register","id","set","_unregister","delete","toCode","task","params","paramsJson","JSON","stringify","src","toString","includes","test","__DEV__","console","warn","argsStr","SHARED_THREAD_NAME","_sharedThreadId","getSharedThread","createThread","runOnJS","runOnThread","runOnNewJS","existing","createThreadHandle","resolvedName","getThreads","Array","from","destroyThread","idOrName","targetId","has","onMessage","handler","sub","addListener","event","parsed","data","parse","threadId","remove","Promise","resolve","run","unsub","destroy"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;AAAA,SAASA,kBAAkB,QAAQ,cAAc;AACjD,OAAOC,iBAAiB,MAAM,8BAA2B;AAkBzD,MAAMC,QAAQ,GAAG,IAAIF,kBAAkB,CAACC,iBAAiB,CAAC;AAC1D,MAAME,uBAAuB,GAAG,iBAAiB;AAEjD,MAAMC,SAAS,GAAG,IAAIC,GAAG,CAAqB,CAAC;AAE/C,SAASC,WAAWA,CAACC,IAAY,EAA0B;EACzD,KAAK,MAAM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EAAE,EAAE;YACnBgD,KAAK,CAAC,CAAC;YACPF,OAAO,CAACL,IAAI,CAAC;UACf;QACF,CAAC,CAAC;MACJ,CAAC,CAAC;IACJ,CAA8B;IAC9BQ,OAAOA,CAAA,EAAG;MACRlB,aAAa,CAAC/B,EAAE,CAAC;IACnB;EACF,CAAC;AACH","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAGA,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,GAAG,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC;AAEzD,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,GAAG,CAAC,IAAI,EAAE,UAAU,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IAC9C,SAAS,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;IACxD,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAC9B,OAAO,IAAI,IAAI,CAAC;CACjB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAGA,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,GAAG,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC;AAEzD,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,GAAG,CAAC,IAAI,EAAE,UAAU,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IAC9C,SAAS,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;IACxD,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAC9B,OAAO,IAAI,IAAI,CAAC;CACjB,CAAC;AA0DF,wBAAgB,OAAO,CAAC,IAAI,EAAE,UAAU,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,IAAI,CAEhE;AAED,wBAAgB,UAAU,CACxB,IAAI,EAAE,UAAU,EAChB,MAAM,CAAC,EAAE,OAAO,EAChB,IAAI,CAAC,EAAE,MAAM,GACZ,YAAY,CAYd;AAED,wBAAgB,YAAY,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,YAAY,CAWxD;AAED,wBAAgB,UAAU,IAAI,UAAU,EAAE,CAEzC;AAED,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CA0B7D;AAED,wBAAgB,SAAS,CACvB,OAAO,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,KAAK,IAAI,GACjD,MAAM,IAAI,CAAC;AACd,wBAAgB,SAAS,IAAI,OAAO,CAAC;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rn-org/react-native-thread",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.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",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"turbo-module",
|
|
54
54
|
"new-architecture",
|
|
55
55
|
"worker",
|
|
56
|
-
"
|
|
56
|
+
"resolveThreadMessage"
|
|
57
57
|
],
|
|
58
58
|
"repository": {
|
|
59
59
|
"type": "git",
|
package/src/globals.d.ts
CHANGED
|
@@ -9,10 +9,10 @@
|
|
|
9
9
|
* @example
|
|
10
10
|
* thread.run(() => {
|
|
11
11
|
* const result = heavyWork();
|
|
12
|
-
*
|
|
12
|
+
* resolveThreadMessage({ result });
|
|
13
13
|
* });
|
|
14
14
|
*/
|
|
15
|
-
declare function
|
|
15
|
+
declare function resolveThreadMessage(data: unknown): void;
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* Parameters passed by the caller via the optional second argument of
|
package/src/index.tsx
CHANGED
|
@@ -22,6 +22,13 @@ const RN_THREAD_MESSAGE_EVENT = 'RNThreadMessage';
|
|
|
22
22
|
|
|
23
23
|
const _registry = new Map<number, ThreadInfo>();
|
|
24
24
|
|
|
25
|
+
function _findByName(name: string): ThreadInfo | undefined {
|
|
26
|
+
for (const info of _registry.values()) {
|
|
27
|
+
if (info.name === name) return info;
|
|
28
|
+
}
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
25
32
|
function _register(id: number, name: string): void {
|
|
26
33
|
_registry.set(id, { id, name });
|
|
27
34
|
}
|
|
@@ -75,6 +82,12 @@ export function runOnNewJS(
|
|
|
75
82
|
params?: unknown,
|
|
76
83
|
name?: string
|
|
77
84
|
): ThreadHandle {
|
|
85
|
+
if (name != null) {
|
|
86
|
+
const existing = _findByName(name);
|
|
87
|
+
if (existing) {
|
|
88
|
+
return createThreadHandle(existing.id, existing.name);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
78
91
|
const id = ReactNativeThread.createThread();
|
|
79
92
|
const resolvedName = name ?? `RNThread-${id}`;
|
|
80
93
|
_register(id, resolvedName);
|
|
@@ -83,6 +96,12 @@ export function runOnNewJS(
|
|
|
83
96
|
}
|
|
84
97
|
|
|
85
98
|
export function createThread(name?: string): ThreadHandle {
|
|
99
|
+
if (name != null) {
|
|
100
|
+
const existing = _findByName(name);
|
|
101
|
+
if (existing) {
|
|
102
|
+
return createThreadHandle(existing.id, existing.name);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
86
105
|
const id = ReactNativeThread.createThread();
|
|
87
106
|
const resolvedName = name ?? `RNThread-${id}`;
|
|
88
107
|
_register(id, resolvedName);
|