@rn-org/react-native-thread 0.2.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
 
@@ -27,11 +27,8 @@ class ReactNativeThreadModule(reactContext: ReactApplicationContext) :
27
27
  private val threads = ConcurrentHashMap<Long, ThreadEntry>()
28
28
  private val nextId = AtomicLong(1L)
29
29
 
30
- // ─── NativeEventEmitter boilerplate ────────────────────────────────────────
31
- override fun addListener(eventName: String?) { /* no-op: handled by JS */ }
32
- override fun removeListeners(count: Double) { /* no-op */ }
33
-
34
- // ─── Thread lifecycle ───────────────────────────────────────────────────────
30
+ override fun addListener(eventName: String?) {}
31
+ override fun removeListeners(count: Double) {}
35
32
 
36
33
  override fun createThread(): Double {
37
34
  val id = nextId.getAndIncrement()
@@ -46,7 +43,7 @@ class ReactNativeThreadModule(reactContext: ReactApplicationContext) :
46
43
  cx.optimizationLevel = -1
47
44
  val scope = cx.initStandardObjects()
48
45
  injectConsole(scope, id)
49
- injectPostMessage(scope, id)
46
+ injectResolveThreadMessage(scope, id)
50
47
  scopeFuture.complete(scope)
51
48
  } catch (e: Exception) {
52
49
  scopeFuture.completeExceptionally(e)
@@ -84,13 +81,11 @@ class ReactNativeThreadModule(reactContext: ReactApplicationContext) :
84
81
  entry.executor.shutdown()
85
82
  }
86
83
 
87
- // ─── Scope injection ────────────────────────────────────────────────────────
88
-
89
- private fun injectPostMessage(scope: Scriptable, threadId: Long) {
84
+ private fun injectResolveThreadMessage(scope: Scriptable, threadId: Long) {
90
85
  val ctx = reactApplicationContext
91
86
  ScriptableObject.putProperty(
92
87
  scope as ScriptableObject,
93
- "postMessage",
88
+ "resolveThreadMessage",
94
89
  object : BaseFunction() {
95
90
  override fun call(
96
91
  cx: RhinoContext,
@@ -1,23 +1,16 @@
1
1
  #import "ReactNativeThread.h"
2
2
  #import <JavaScriptCore/JavaScriptCore.h>
3
3
 
4
- // One entry per thread: a JSContext running on a dedicated serial queue.
5
- // Each thread owns its own JSVirtualMachine so execution is fully concurrent
6
- // and never contends with the main thread's VM lock.
7
4
  @interface RNThread : NSObject
8
5
  @property (nonatomic, strong) JSVirtualMachine *vm;
9
6
  @property (nonatomic, strong) JSContext *context;
10
7
  @property (nonatomic, strong) dispatch_queue_t queue;
11
- // Signals when VM+context initialisation is complete so that runOnThread
12
- // can be enqueued immediately without blocking the JS thread during init.
13
8
  @property (nonatomic) dispatch_semaphore_t ready;
14
9
  @end
15
10
 
16
11
  @implementation RNThread
17
12
  @end
18
13
 
19
- // ─────────────────────────────────────────────────────────────────────────────
20
-
21
14
  @implementation ReactNativeThread {
22
15
  NSMutableDictionary<NSNumber *, RNThread *> *_threads;
23
16
  NSLock *_lock;
@@ -33,7 +26,6 @@
33
26
  return self;
34
27
  }
35
28
 
36
- // Required by RCTEventEmitter
37
29
  - (NSArray<NSString *> *)supportedEvents {
38
30
  return @[@"RNThreadMessage"];
39
31
  }
@@ -41,16 +33,12 @@
41
33
  - (void)addListener:(NSString *)eventName { [super addListener:eventName]; }
42
34
  - (void)removeListeners:(double)count { [super removeListeners:count]; }
43
35
 
44
- // ─── createThread ─────────────────────────────────────────────────────────────
45
-
46
36
  - (NSNumber *)createThread {
47
37
  RNThread *t = [RNThread new];
48
38
 
49
39
  NSString *label = [NSString stringWithFormat:@"com.rnorg.reactnativethread.%lu",
50
40
  (unsigned long)(NSUInteger)_nextId];
51
41
 
52
- // QOS_CLASS_UTILITY keeps background JS work below the main/UI thread in
53
- // the scheduler, preventing CPU starvation when a tight loop is running.
54
42
  dispatch_queue_attr_t attr = dispatch_queue_attr_make_with_qos_class(
55
43
  DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, 0);
56
44
  t.queue = dispatch_queue_create(label.UTF8String, attr);
@@ -58,9 +46,6 @@
58
46
 
59
47
  NSUInteger tidForLog = (NSUInteger)_nextId;
60
48
 
61
- // Async init: the JS thread is never blocked waiting for VM creation.
62
- // runOnThread enqueues a semaphore-wait block first so it executes only
63
- // after init is complete, all on the background queue.
64
49
  dispatch_async(t.queue, ^{
65
50
  t.vm = [[JSVirtualMachine alloc] init];
66
51
  t.context = [[JSContext alloc] initWithVirtualMachine:t.vm];
@@ -68,7 +53,6 @@
68
53
  NSLog(@"[RNThread-%lu] JS exception: %@", (unsigned long)tidForLog, exception);
69
54
  };
70
55
 
71
- // ── console ───────────────────────────────────────────────────────────
72
56
  NSString *tag = [NSString stringWithFormat:@"RNThread-%lu", (unsigned long)tidForLog];
73
57
  NSMutableDictionary *console = [NSMutableDictionary new];
74
58
  console[@"log"] = ^{
@@ -93,10 +77,9 @@
93
77
  };
94
78
  t.context[@"console"] = console;
95
79
 
96
- // ── postMessage ───────────────────────────────────────────────────────
97
80
  NSUInteger capturedTid = tidForLog;
98
81
  __weak ReactNativeThread *weakSelf = self;
99
- t.context[@"postMessage"] = ^(JSValue *data) { // evaluated on t.queue
82
+ t.context[@"resolveThreadMessage"] = ^(JSValue *data) {
100
83
  JSValue *jsonStr = [data.context[@"JSON"] invokeMethod:@"stringify"
101
84
  withArguments:@[data]];
102
85
  NSString *serialised = [jsonStr toString];
@@ -114,7 +97,6 @@
114
97
  }];
115
98
  };
116
99
 
117
- // Signal that the thread is ready to accept work.
118
100
  dispatch_semaphore_signal(t.ready);
119
101
  });
120
102
 
@@ -127,8 +109,6 @@
127
109
  return @(tid);
128
110
  }
129
111
 
130
- // ─── runOnThread ──────────────────────────────────────────────────────────────
131
-
132
112
  - (void)runOnThread:(double)threadId code:(NSString *)code {
133
113
  [_lock lock];
134
114
  RNThread *t = _threads[@(threadId)];
@@ -142,15 +122,12 @@
142
122
  NSString *codeCopy = [code copy];
143
123
  dispatch_semaphore_t ready = t.ready;
144
124
  dispatch_async(t.queue, ^{
145
- // Wait for init to finish (no-op after first runOnThread call).
146
125
  dispatch_semaphore_wait(ready, DISPATCH_TIME_FOREVER);
147
- dispatch_semaphore_signal(ready); // re-signal for subsequent calls
126
+ dispatch_semaphore_signal(ready);
148
127
  [t.context evaluateScript:codeCopy];
149
128
  });
150
129
  }
151
130
 
152
- // ─── destroyThread ────────────────────────────────────────────────────────────
153
-
154
131
  - (void)destroyThread:(double)threadId {
155
132
  [_lock lock];
156
133
  RNThread *t = _threads[@(threadId)];
@@ -164,8 +141,6 @@
164
141
  });
165
142
  }
166
143
 
167
- // ─── TurboModule boilerplate ──────────────────────────────────────────────────
168
-
169
144
  - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
170
145
  (const facebook::react::ObjCTurboModule::InitParams &)params
171
146
  {
@@ -1,37 +1,10 @@
1
- /**
2
- * Babel plugin for @rn-org/react-native-thread
3
- *
4
- * Hermes compiles JS to bytecode at build time, so fn.toString() at runtime
5
- * returns a placeholder ("bytecode") rather than the original source.
6
- *
7
- * This plugin runs at compile time and replaces arrow-function / function-
8
- * expression arguments passed to the thread API with string literals that
9
- * contain the source wrapped as an IIFE. The background thread engine
10
- * (JSC on iOS, Rhino on Android) can then simply eval() the string.
11
- *
12
- * Transforms:
13
- * runOnJS((args) => { ... }) → runOnJS("((args) => { ... })(__params__)")
14
- * runOnNewJS((args) => { ... }) → runOnNewJS("((args) => { ... })(__params__)")
15
- * thread.run((args) => { ... }) → thread.run("((args) => { ... })(__params__)")
16
- *
17
- * Add to your app's babel.config.js:
18
- * plugins: ['@rn-org/react-native-thread/babel-plugin']
19
- */
20
-
21
1
  'use strict';
22
2
 
23
- // Top-level function names that take a fn-or-code as their first argument.
24
3
  const TOP_LEVEL_API = new Set(['runOnJS', 'runOnNewJS']);
25
-
26
- // Method names on a ThreadHandle that take a fn-or-code as their first arg.
27
4
  const METHOD_API = new Set(['run']);
28
5
  module.exports = function reactNativeThreadPlugin({
29
6
  types: t
30
7
  }) {
31
- /**
32
- * If `argNode` is an arrow function or function expression, replace the
33
- * argument at `index` with a string literal `"(<source>)()"`.
34
- */
35
8
  function maybeTransformArg(callPath, state, argNode, index) {
36
9
  if (!t.isArrowFunctionExpression(argNode) && !t.isFunctionExpression(argNode)) {
37
10
  return;
@@ -49,16 +22,12 @@ module.exports = function reactNativeThreadPlugin({
49
22
  callee,
50
23
  arguments: args
51
24
  } = path.node;
52
-
53
- // runOnJS(() => {}) / runOnNewJS(() => {})
54
25
  if (t.isIdentifier(callee) && TOP_LEVEL_API.has(callee.name)) {
55
26
  if (args.length >= 1) {
56
27
  maybeTransformArg(path, state, args[0], 0);
57
28
  }
58
29
  return;
59
30
  }
60
-
61
- // someThread.run(() => {})
62
31
  if (t.isMemberExpression(callee) && !callee.computed && t.isIdentifier(callee.property) && METHOD_API.has(callee.property.name)) {
63
32
  if (args.length >= 1) {
64
33
  maybeTransformArg(path, state, args[0], 0);
@@ -1 +1 @@
1
- {"version":3,"names":["TOP_LEVEL_API","Set","METHOD_API","module","exports","reactNativeThreadPlugin","types","t","maybeTransformArg","callPath","state","argNode","index","isArrowFunctionExpression","isFunctionExpression","originalCode","file","code","fnSource","slice","start","end","iife","node","arguments","stringLiteral","name","visitor","CallExpression","path","callee","args","isIdentifier","has","length","isMemberExpression","computed","property"],"sourceRoot":"../../src","sources":["babel-plugin.js"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,YAAY;;AAEZ;AACA,MAAMA,aAAa,GAAG,IAAIC,GAAG,CAAC,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC;;AAExD;AACA,MAAMC,UAAU,GAAG,IAAID,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;AAEnCE,MAAM,CAACC,OAAO,GAAG,SAASC,uBAAuBA,CAAC;EAAEC,KAAK,EAAEC;AAAE,CAAC,EAAE;EAC9D;AACF;AACA;AACA;EACE,SAASC,iBAAiBA,CAACC,QAAQ,EAAEC,KAAK,EAAEC,OAAO,EAAEC,KAAK,EAAE;IAC1D,IACE,CAACL,CAAC,CAACM,yBAAyB,CAACF,OAAO,CAAC,IACrC,CAACJ,CAAC,CAACO,oBAAoB,CAACH,OAAO,CAA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]}
1
+ {"version":3,"names":["TOP_LEVEL_API","Set","METHOD_API","module","exports","reactNativeThreadPlugin","types","t","maybeTransformArg","callPath","state","argNode","index","isArrowFunctionExpression","isFunctionExpression","originalCode","file","code","fnSource","slice","start","end","iife","node","arguments","stringLiteral","name","visitor","CallExpression","path","callee","args","isIdentifier","has","length","isMemberExpression","computed","property"],"sourceRoot":"../../src","sources":["babel-plugin.js"],"mappings":"AAAA,YAAY;;AAEZ,MAAMA,aAAa,GAAG,IAAIC,GAAG,CAAC,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC;AAExD,MAAMC,UAAU,GAAG,IAAID,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;AAEnCE,MAAM,CAACC,OAAO,GAAG,SAASC,uBAAuBA,CAAC;EAAEC,KAAK,EAAEC;AAAE,CAAC,EAAE;EAC9D,SAASC,iBAAiBA,CAACC,QAAQ,EAAEC,KAAK,EAAEC,OAAO,EAAEC,KAAK,EAAE;IAC1D,IACE,CAACL,CAAC,CAACM,yBAAyB,CAACF,OAAO,CAAC,IACrC,CAACJ,CAAC,CAACO,oBAAoB,CAACH,OAAO,CAAC,EAChC;MACA;IACF;IAEA,MAAMI,YAAY,GAAGL,KAAK,CAACM,IAAI,CAACC,IAAI;IACpC,MAAMC,QAAQ,GAAGH,YAAY,CAACI,KAAK,CAACR,OAAO,CAACS,KAAK,EAAET,OAAO,CAACU,GAAG,CAAC;IAC/D,MAAMC,IAAI,GAAG,IAAIJ,QAAQ,eAAe;IAExCT,QAAQ,CAACc,IAAI,CAACC,SAAS,CAACZ,KAAK,CAAC,GAAGL,CAAC,CAACkB,aAAa,CAACH,IAAI,CAAC;EACxD;EAEA,OAAO;IACLI,IAAI,EAAE,qBAAqB;IAC3BC,OAAO,EAAE;MACPC,cAAcA,CAACC,IAAI,EAAEnB,KAAK,EAAE;QAC1B,MAAM;UAAEoB,MAAM;UAAEN,SAAS,EAAEO;QAAK,CAAC,GAAGF,IAAI,CAACN,IAAI;QAE7C,IAAIhB,CAAC,CAACyB,YAAY,CAACF,MAAM,CAAC,IAAI9B,aAAa,CAACiC,GAAG,CAACH,MAAM,CAACJ,IAAI,CAAC,EAAE;UAC5D,IAAIK,IAAI,CAACG,MAAM,IAAI,CAAC,EAAE;YACpB1B,iBAAiB,CAACqB,IAAI,EAAEnB,KAAK,EAAEqB,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;UAC5C;UACA;QACF;QAEA,IACExB,CAAC,CAAC4B,kBAAkB,CAACL,MAAM,CAAC,IAC5B,CAACA,MAAM,CAACM,QAAQ,IAChB7B,CAAC,CAACyB,YAAY,CAACF,MAAM,CAACO,QAAQ,CAAC,IAC/BnC,UAAU,CAAC+B,GAAG,CAACH,MAAM,CAACO,QAAQ,CAACX,IAAI,CAAC,EACpC;UACA,IAAIK,IAAI,CAACG,MAAM,IAAI,CAAC,EAAE;YACpB1B,iBAAiB,CAACqB,IAAI,EAAEnB,KAAK,EAAEqB,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;UAC5C;QACF;MACF;IACF;EACF,CAAC;AACH,CAAC","ignoreList":[]}
@@ -2,13 +2,8 @@
2
2
 
3
3
  import { NativeEventEmitter } from 'react-native';
4
4
  import ReactNativeThread from "./NativeReactNativeThread.js";
5
-
6
- /** Snapshot of a single live thread. */
7
-
8
5
  const _emitter = new NativeEventEmitter(ReactNativeThread);
9
6
  const RN_THREAD_MESSAGE_EVENT = 'RNThreadMessage';
10
-
11
- // ─── Thread registry (JS-layer; names are a JS concept) ──────────────────────
12
7
  const _registry = new Map();
13
8
  function _register(id, name) {
14
9
  _registry.set(id, {
@@ -21,9 +16,6 @@ function _unregister(id) {
21
16
  }
22
17
  function toCode(task, params) {
23
18
  if (typeof task === 'string') {
24
- // Always declare __params__ so Babel-transformed strings can pass it as
25
- // an argument to the function, and raw code strings that reference
26
- // __params__ directly continue to work.
27
19
  const paramsJson = params !== undefined ? JSON.stringify(params) : 'undefined';
28
20
  return `var __params__ = ${paramsJson};\n${task}`;
29
21
  }
@@ -37,9 +29,6 @@ function toCode(task, params) {
37
29
  const argsStr = params !== undefined ? JSON.stringify(params) : '';
38
30
  return `(${src})(${argsStr})`;
39
31
  }
40
-
41
- // ─── Shared thread (runOnJS) ──────────────────────────────────────────────────
42
-
43
32
  const SHARED_THREAD_NAME = 'RNOrgThread';
44
33
  let _sharedThreadId = null;
45
34
  function getSharedThread() {
@@ -52,11 +41,6 @@ function getSharedThread() {
52
41
  export function runOnJS(task, params) {
53
42
  ReactNativeThread.runOnThread(getSharedThread(), toCode(task, params));
54
43
  }
55
-
56
- /**
57
- * Runs a task on a brand-new isolated thread and returns a handle to it.
58
- * @param name - Optional display name (default: `RNThread-<id>`).
59
- */
60
44
  export function runOnNewJS(task, params, name) {
61
45
  const id = ReactNativeThread.createThread();
62
46
  const resolvedName = name ?? `RNThread-${id}`;
@@ -64,31 +48,15 @@ export function runOnNewJS(task, params, name) {
64
48
  ReactNativeThread.runOnThread(id, toCode(task, params));
65
49
  return createThreadHandle(id, resolvedName);
66
50
  }
67
-
68
- /**
69
- * Creates a persistent thread. Optionally give it a name.
70
- * @param name - Display name (default: `RNThread-<id>`).
71
- */
72
51
  export function createThread(name) {
73
52
  const id = ReactNativeThread.createThread();
74
53
  const resolvedName = name ?? `RNThread-${id}`;
75
54
  _register(id, resolvedName);
76
55
  return createThreadHandle(id, resolvedName);
77
56
  }
78
-
79
- /**
80
- * Returns info about every live thread currently managed by this library,
81
- * including the shared `runOnJS` thread once it has been started.
82
- */
83
57
  export function getThreads() {
84
58
  return Array.from(_registry.values());
85
59
  }
86
-
87
- /**
88
- * Destroy a thread by its numeric ID **or** by its name.
89
- * When a name is given, the first matching thread is destroyed.
90
- * This is also what `ThreadHandle.destroy()` calls internally.
91
- */
92
60
  export function destroyThread(idOrName) {
93
61
  let targetId;
94
62
  if (typeof idOrName === 'number') {
@@ -1 +1 @@
1
- {"version":3,"names":["NativeEventEmitter","ReactNativeThread","_emitter","RN_THREAD_MESSAGE_EVENT","_registry","Map","_register","id","name","set","_unregister","delete","toCode","task","params","paramsJson","undefined","JSON","stringify","src","toString","includes","test","__DEV__","console","warn","argsStr","SHARED_THREAD_NAME","_sharedThreadId","getSharedThread","createThread","runOnJS","runOnThread","runOnNewJS","resolvedName","createThreadHandle","getThreads","Array","from","values","destroyThread","idOrName","targetId","has","info","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;;AAIzD;;AAsBA,MAAMC,QAAQ,GAAG,IAAIF,kBAAkB,CAACC,iBAAiB,CAAC;AAC1D,MAAME,uBAAuB,GAAG,iBAAiB;;AAEjD;AACA,MAAMC,SAAS,GAAG,IAAIC,GAAG,CAAqB,CAAC;AAE/C,SAASC,SAASA,CAACC,EAAU,EAAEC,IAAY,EAAQ;EACjDJ,SAAS,CAACK,GAAG,CAACF,EAAE,EAAE;IAAEA,EAAE;IAAEC;EAAK,CAAC,CAAC;AACjC;AAEA,SAASE,WAAWA,CAACH,EAAU,EAAQ;EACrCH,SAAS,CAACO,MAAM,CAACJ,EAAE,CAAC;AACtB;AAEA,SAASK,MAAMA,CAACC,IAAgB,EAAEC,MAAgB,EAAU;EAC1D,IAAI,OAAOD,IAAI,KAAK,QAAQ,EAAE;IAC5B;IACA;IACA;IACA,MAAME,UAAU,GACdD,MAAM,KAAKE,SAAS,GAAGC,IAAI,CAACC,SAAS,CAACJ,MAAM,CAAC,GAAG,WAAW;IAC7D,OAAO,oBAAoBC,UAAU,MAAMF,IAAI,EAAE;EACnD;EAEA,MAAMM,GAAG,GAAGN,IAAI,CAACO,QAAQ,CAAC,CAAC;EAC3B,IACED,GAAG,CAACE,QAAQ,CAAC,YAAY,CAAC,IAC1B,0CAA0C,CAACC,IAAI,CAACH,GAAG,CAAC,EACpD;IACA,IAAII,OAAO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AO,IAAK;QACvC,MAAME,KAAK,GAAGb,SAAS,CAAC,CAACM,IAAI,EAAEE,QAAQ,KAAK;UAC1C,IAAIA,QAAQ,KAAK9C,EAAE,EAAE;YACnBmD,KAAK,CAAC,CAAC;YACPF,OAAO,CAACL,IAAI,CAAC;UACf;QACF,CAAC,CAAC;MACJ,CAAC,CAAC;IACJ,CAA8B;IAC9BQ,OAAOA,CAAA,EAAG;MACRnB,aAAa,CAACjC,EAAE,CAAC;IACnB;EACF,CAAC;AACH","ignoreList":[]}
1
+ {"version":3,"names":["NativeEventEmitter","ReactNativeThread","_emitter","RN_THREAD_MESSAGE_EVENT","_registry","Map","_register","id","name","set","_unregister","delete","toCode","task","params","paramsJson","undefined","JSON","stringify","src","toString","includes","test","__DEV__","console","warn","argsStr","SHARED_THREAD_NAME","_sharedThreadId","getSharedThread","createThread","runOnJS","runOnThread","runOnNewJS","resolvedName","createThreadHandle","getThreads","Array","from","values","destroyThread","idOrName","targetId","has","info","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,SAASA,CAACC,EAAU,EAAEC,IAAY,EAAQ;EACjDJ,SAAS,CAACK,GAAG,CAACF,EAAE,EAAE;IAAEA,EAAE;IAAEC;EAAK,CAAC,CAAC;AACjC;AAEA,SAASE,WAAWA,CAACH,EAAU,EAAQ;EACrCH,SAAS,CAACO,MAAM,CAACJ,EAAE,CAAC;AACtB;AAEA,SAASK,MAAMA,CAACC,IAAgB,EAAEC,MAAgB,EAAU;EAC1D,IAAI,OAAOD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D,CAAC;IACD+B,SAAS,EAAE,SAAAA,CAAUC,OAAiC,EAAE;MACtD,IAAIA,OAAO,EAAE;QACX,OAAOD,SAAS,CAAC,CAACM,IAAI,EAAEE,QAAQ,KAAK;UACnC,IAAIA,QAAQ,KAAK9C,EAAE,EAAEuC,OAAO,CAACK,IAAI,CAAC;QACpC,CAAC,CAAC;MACJ;MACA,OAAO,IAAII,OAAO,CAAWC,OAAO,IAAK;QACvC,MAAME,KAAK,GAAGb,SAAS,CAAC,CAACM,IAAI,EAAEE,QAAQ,KAAK;UAC1C,IAAIA,QAAQ,KAAK9C,EAAE,EAAE;YACnBmD,KAAK,CAAC,CAAC;YACPF,OAAO,CAACL,IAAI,CAAC;UACf;QACF,CAAC,CAAC;MACJ,CAAC,CAAC;IACJ,CAA8B;IAC9BQ,OAAOA,CAAA,EAAG;MACRnB,aAAa,CAACjC,EAAE,CAAC;IACnB;EACF,CAAC;AACH","ignoreList":[]}
@@ -1,5 +1,4 @@
1
1
  export type ThreadTask = ((args?: any) => void) | string;
2
- /** Snapshot of a single live thread. */
3
2
  export type ThreadInfo = {
4
3
  readonly id: number;
5
4
  readonly name: string;
@@ -7,39 +6,15 @@ export type ThreadInfo = {
7
6
  export type ThreadHandle = {
8
7
  readonly id: number;
9
8
  readonly name: string;
10
- /**
11
- * Run a task on this thread.
12
- * @param params - Optional JSON-serialisable value passed as the first
13
- * argument to the thread function.
14
- */
15
9
  run(task: ThreadTask, params?: unknown): void;
16
- /** Subscribe to messages from this thread. Returns an unsubscribe function. */
17
10
  onMessage(handler: (data: unknown) => void): () => void;
18
- /** Returns a Promise that resolves with the next message from this thread. */
19
11
  onMessage(): Promise<unknown>;
20
12
  destroy(): void;
21
13
  };
22
14
  export declare function runOnJS(task: ThreadTask, params?: unknown): void;
23
- /**
24
- * Runs a task on a brand-new isolated thread and returns a handle to it.
25
- * @param name - Optional display name (default: `RNThread-<id>`).
26
- */
27
15
  export declare function runOnNewJS(task: ThreadTask, params?: unknown, name?: string): ThreadHandle;
28
- /**
29
- * Creates a persistent thread. Optionally give it a name.
30
- * @param name - Display name (default: `RNThread-<id>`).
31
- */
32
16
  export declare function createThread(name?: string): ThreadHandle;
33
- /**
34
- * Returns info about every live thread currently managed by this library,
35
- * including the shared `runOnJS` thread once it has been started.
36
- */
37
17
  export declare function getThreads(): ThreadInfo[];
38
- /**
39
- * Destroy a thread by its numeric ID **or** by its name.
40
- * When a name is given, the first matching thread is destroyed.
41
- * This is also what `ThreadHandle.destroy()` calls internally.
42
- */
43
18
  export declare function destroyThread(idOrName: number | string): void;
44
19
  export declare function onMessage(handler: (data: unknown, threadId: number) => void): () => void;
45
20
  export declare function onMessage(): Promise<{
@@ -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,wCAAwC;AACxC,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;;;;OAIG;IACH,GAAG,CAAC,IAAI,EAAE,UAAU,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IAC9C,+EAA+E;IAC/E,SAAS,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;IACxD,8EAA8E;IAC9E,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAC9B,OAAO,IAAI,IAAI,CAAC;CACjB,CAAC;AAyDF,wBAAgB,OAAO,CAAC,IAAI,EAAE,UAAU,EAA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M,IAAI,CAAC;IACxD,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAC9B,OAAO,IAAI,IAAI,CAAC;CACjB,CAAC;AAmDF,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,CAMd;AAED,wBAAgB,YAAY,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,YAAY,CAKxD;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.2.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",
@@ -1,36 +1,10 @@
1
- /**
2
- * Babel plugin for @rn-org/react-native-thread
3
- *
4
- * Hermes compiles JS to bytecode at build time, so fn.toString() at runtime
5
- * returns a placeholder ("bytecode") rather than the original source.
6
- *
7
- * This plugin runs at compile time and replaces arrow-function / function-
8
- * expression arguments passed to the thread API with string literals that
9
- * contain the source wrapped as an IIFE. The background thread engine
10
- * (JSC on iOS, Rhino on Android) can then simply eval() the string.
11
- *
12
- * Transforms:
13
- * runOnJS((args) => { ... }) → runOnJS("((args) => { ... })(__params__)")
14
- * runOnNewJS((args) => { ... }) → runOnNewJS("((args) => { ... })(__params__)")
15
- * thread.run((args) => { ... }) → thread.run("((args) => { ... })(__params__)")
16
- *
17
- * Add to your app's babel.config.js:
18
- * plugins: ['@rn-org/react-native-thread/babel-plugin']
19
- */
20
-
21
1
  'use strict';
22
2
 
23
- // Top-level function names that take a fn-or-code as their first argument.
24
3
  const TOP_LEVEL_API = new Set(['runOnJS', 'runOnNewJS']);
25
4
 
26
- // Method names on a ThreadHandle that take a fn-or-code as their first arg.
27
5
  const METHOD_API = new Set(['run']);
28
6
 
29
7
  module.exports = function reactNativeThreadPlugin({ types: t }) {
30
- /**
31
- * If `argNode` is an arrow function or function expression, replace the
32
- * argument at `index` with a string literal `"(<source>)()"`.
33
- */
34
8
  function maybeTransformArg(callPath, state, argNode, index) {
35
9
  if (
36
10
  !t.isArrowFunctionExpression(argNode) &&
@@ -52,7 +26,6 @@ module.exports = function reactNativeThreadPlugin({ types: t }) {
52
26
  CallExpression(path, state) {
53
27
  const { callee, arguments: args } = path.node;
54
28
 
55
- // runOnJS(() => {}) / runOnNewJS(() => {})
56
29
  if (t.isIdentifier(callee) && TOP_LEVEL_API.has(callee.name)) {
57
30
  if (args.length >= 1) {
58
31
  maybeTransformArg(path, state, args[0], 0);
@@ -60,7 +33,6 @@ module.exports = function reactNativeThreadPlugin({ types: t }) {
60
33
  return;
61
34
  }
62
35
 
63
- // someThread.run(() => {})
64
36
  if (
65
37
  t.isMemberExpression(callee) &&
66
38
  !callee.computed &&
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
package/src/index.tsx CHANGED
@@ -3,7 +3,6 @@ import ReactNativeThread from './NativeReactNativeThread';
3
3
 
4
4
  export type ThreadTask = ((args?: any) => void) | string;
5
5
 
6
- /** Snapshot of a single live thread. */
7
6
  export type ThreadInfo = {
8
7
  readonly id: number;
9
8
  readonly name: string;
@@ -12,15 +11,8 @@ export type ThreadInfo = {
12
11
  export type ThreadHandle = {
13
12
  readonly id: number;
14
13
  readonly name: string;
15
- /**
16
- * Run a task on this thread.
17
- * @param params - Optional JSON-serialisable value passed as the first
18
- * argument to the thread function.
19
- */
20
14
  run(task: ThreadTask, params?: unknown): void;
21
- /** Subscribe to messages from this thread. Returns an unsubscribe function. */
22
15
  onMessage(handler: (data: unknown) => void): () => void;
23
- /** Returns a Promise that resolves with the next message from this thread. */
24
16
  onMessage(): Promise<unknown>;
25
17
  destroy(): void;
26
18
  };
@@ -28,7 +20,6 @@ export type ThreadHandle = {
28
20
  const _emitter = new NativeEventEmitter(ReactNativeThread);
29
21
  const RN_THREAD_MESSAGE_EVENT = 'RNThreadMessage';
30
22
 
31
- // ─── Thread registry (JS-layer; names are a JS concept) ──────────────────────
32
23
  const _registry = new Map<number, ThreadInfo>();
33
24
 
34
25
  function _register(id: number, name: string): void {
@@ -41,9 +32,6 @@ function _unregister(id: number): void {
41
32
 
42
33
  function toCode(task: ThreadTask, params?: unknown): string {
43
34
  if (typeof task === 'string') {
44
- // Always declare __params__ so Babel-transformed strings can pass it as
45
- // an argument to the function, and raw code strings that reference
46
- // __params__ directly continue to work.
47
35
  const paramsJson =
48
36
  params !== undefined ? JSON.stringify(params) : 'undefined';
49
37
  return `var __params__ = ${paramsJson};\n${task}`;
@@ -67,8 +55,6 @@ function toCode(task: ThreadTask, params?: unknown): string {
67
55
  return `(${src})(${argsStr})`;
68
56
  }
69
57
 
70
- // ─── Shared thread (runOnJS) ──────────────────────────────────────────────────
71
-
72
58
  const SHARED_THREAD_NAME = 'RNOrgThread';
73
59
  let _sharedThreadId: number | null = null;
74
60
 
@@ -84,10 +70,6 @@ export function runOnJS(task: ThreadTask, params?: unknown): void {
84
70
  ReactNativeThread.runOnThread(getSharedThread(), toCode(task, params));
85
71
  }
86
72
 
87
- /**
88
- * Runs a task on a brand-new isolated thread and returns a handle to it.
89
- * @param name - Optional display name (default: `RNThread-<id>`).
90
- */
91
73
  export function runOnNewJS(
92
74
  task: ThreadTask,
93
75
  params?: unknown,
@@ -100,10 +82,6 @@ export function runOnNewJS(
100
82
  return createThreadHandle(id, resolvedName);
101
83
  }
102
84
 
103
- /**
104
- * Creates a persistent thread. Optionally give it a name.
105
- * @param name - Display name (default: `RNThread-<id>`).
106
- */
107
85
  export function createThread(name?: string): ThreadHandle {
108
86
  const id = ReactNativeThread.createThread();
109
87
  const resolvedName = name ?? `RNThread-${id}`;
@@ -111,19 +89,10 @@ export function createThread(name?: string): ThreadHandle {
111
89
  return createThreadHandle(id, resolvedName);
112
90
  }
113
91
 
114
- /**
115
- * Returns info about every live thread currently managed by this library,
116
- * including the shared `runOnJS` thread once it has been started.
117
- */
118
92
  export function getThreads(): ThreadInfo[] {
119
93
  return Array.from(_registry.values());
120
94
  }
121
95
 
122
- /**
123
- * Destroy a thread by its numeric ID **or** by its name.
124
- * When a name is given, the first matching thread is destroyed.
125
- * This is also what `ThreadHandle.destroy()` calls internally.
126
- */
127
96
  export function destroyThread(idOrName: number | string): void {
128
97
  let targetId: number | undefined;
129
98