@rn-org/react-native-thread 0.3.0 → 0.4.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 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
- - [postMessage](#postmessage)
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 `postMessage(data)` from any thread, receive it on the main thread with `onMessage` (callback or `await`).
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
- postMessage({ sum: result });
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 `postMessage(data)`. Prefer `ThreadHandle.onMessage` if you want messages scoped to a single thread.
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 `postMessage` output from **this thread only**. Returns an unsubscribe function. |
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
- ### `postMessage`
311
+ ### `resolveThreadMessage`
312
312
 
313
313
  ```ts
314
- declare function postMessage(data: unknown): void
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
- postMessage({ status: 'done', value: args.multiply * 2 });
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
- postMessage('done');
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
- postMessage('done');
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++) {} postMessage("done")',
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
- postMessage(args.greeting);
406
+ resolveThreadMessage(args.greeting);
407
407
  }, { greeting: 'hello' });
408
408
 
409
409
  // Output (what Hermes compiles)
410
- thread.run("((args) => {\n postMessage(args.greeting);\n})(__params__)", { greeting: 'hello' });
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 `postMessage`.
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
- | `postMessage` payload must be JSON-serialisable | Transported as a JSON string over the bridge |
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
- injectPostMessage(scope, id)
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 injectPostMessage(scope: Scriptable, threadId: Long) {
84
+ private fun injectResolveThreadMessage(scope: Scriptable, threadId: Long) {
85
85
  val ctx = reactApplicationContext
86
86
  ScriptableObject.putProperty(
87
87
  scope as ScriptableObject,
88
- "postMessage",
88
+ "resolveThreadMessage",
89
89
  object : BaseFunction() {
90
90
  override fun call(
91
91
  cx: RhinoContext,
@@ -79,7 +79,7 @@
79
79
 
80
80
  NSUInteger capturedTid = tidForLog;
81
81
  __weak ReactNativeThread *weakSelf = self;
82
- t.context[@"postMessage"] = ^(JSValue *data) {
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rn-org/react-native-thread",
3
- "version": "0.3.0",
3
+ "version": "0.4.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
- "postmessage"
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
- * postMessage({ result });
12
+ * resolveThreadMessage({ result });
13
13
  * });
14
14
  */
15
- declare function postMessage(data: unknown): void;
15
+ declare function resolveThreadMessage(data: unknown): void;
16
16
 
17
17
  /**
18
18
  * Parameters passed by the caller via the optional second argument of