Skip to content

Commit ccf78ee

Browse files
committed
Clarify single-thread runtime function docs
1 parent 88c1f70 commit ccf78ee

2 files changed

Lines changed: 92 additions & 64 deletions

File tree

packages/core/README.md

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,48 @@ function running on a chosen runtime. Arguments and return values are serialized
230230
as JSON. This is the request/response API for work that should execute on
231231
another runtime and return a value to the caller.
232232

233+
### Function Used Only On A Single Thread
234+
235+
When a function should always run on the same runtime, define it in module/global
236+
scope and put that runtime name as the first string directive in the function
237+
body:
238+
239+
```tsx
240+
async function sum(a: number, b: number) {
241+
'background';
242+
return a + b;
243+
}
244+
245+
const result = await sum(5, 1);
246+
```
247+
248+
Metro turns that into a registered runtime function and replaces the original
249+
function with a scheduled alias:
250+
251+
```tsx
252+
export const sum_ = runtimeFunction.withId(
253+
'src/math.sum_',
254+
async function sum(a: number, b: number) {
255+
'background';
256+
return a + b;
257+
},
258+
);
259+
260+
const sum = call(sum_).on('background');
261+
const result = await sum(5, 1);
262+
```
263+
264+
The generated `sum_` export is intentionally private-looking, but it must exist
265+
so other runtimes can load the function through `require(file).sum_`.
266+
267+
Use this shortcut for fixed-runtime helpers. Use `call(fn).on(runtimeName)` when
268+
the caller should choose the runtime.
269+
270+
### Function Used On Different Runtimes
271+
272+
When the caller should choose the runtime, export a runtime function and schedule
273+
it with `call(fn).on(runtimeName)(...args)`:
274+
233275
```tsx
234276
import { call, runtimeFunction } from '@react-native-runtimes/core';
235277

@@ -261,40 +303,11 @@ transformer rewrites it before the app runs:
261303
await fibonacci.runOn('fibonacci-worker-runtime', 38);
262304
```
263305

264-
For a local top-level function that should always run on one runtime, put the
265-
runtime name as the first string directive in the function body:
266-
267-
```tsx
268-
async function sum(a: number, b: number) {
269-
'background';
270-
return a + b;
271-
}
272-
273-
const result = await sum(5, 1);
274-
```
275-
276-
Metro turns that into the same registered runtime function shape:
277-
278-
```tsx
279-
export const sum_ = runtimeFunction.withId(
280-
'src/math.sum_',
281-
async function sum(a: number, b: number) {
282-
'background';
283-
return a + b;
284-
},
285-
);
286-
287-
const sum = call(sum_).on('background');
288-
const result = await sum(5, 1);
289-
```
290-
291-
The generated `sum_` export is intentionally private-looking, but it must exist
292-
so other runtimes can load the function through `require(file).sum_`.
293-
294-
### Fixed Runtime Shortcut
306+
### Function Directive Details
295307

296308
Use a function directive when the function always belongs on the same runtime.
297-
The directive must be the first statement in a top-level function declaration:
309+
The function must be declared in module/global scope, and the directive must be
310+
the first statement in the function body:
298311

299312
```tsx
300313
async function refreshCache(key: string) {
@@ -393,6 +406,7 @@ Current constraints:
393406
- arguments and return values must be JSON-serializable
394407
- the scheduled function must be exported and registered with `runtimeFunction`,
395408
or use the top-level function directive shortcut
409+
- directive shortcut functions must be declared in module/global scope
396410
- inline lambdas and non-exported functions are not scheduled across runtimes
397411
- closures are not captured; pass all inputs as arguments
398412
- directive shortcut functions are rewritten to `const` aliases, so define them

website/docs/threaded-runtime/scheduling-functions.md

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,48 @@ title: Scheduling Functions on Another Runtime
66
Use awaitable runtime functions when one runtime needs a return value from code
77
executed on another named runtime.
88

9+
## Function Used Only On A Single Thread
10+
11+
When a function should always run on the same runtime, define it in module/global
12+
scope and put that runtime name as the first string directive in the function
13+
body:
14+
15+
```tsx
16+
async function sum(a: number, b: number) {
17+
'background';
18+
return a + b;
19+
}
20+
21+
const result = await sum(5, 1);
22+
```
23+
24+
Metro turns that into a registered runtime function and replaces the original
25+
function with a scheduled alias:
26+
27+
```tsx
28+
export const sum_ = runtimeFunction.withId(
29+
'src/math.sum_',
30+
async function sum(a: number, b: number) {
31+
'background';
32+
return a + b;
33+
},
34+
);
35+
36+
const sum = call(sum_).on('background');
37+
const result = await sum(5, 1);
38+
```
39+
40+
The generated `sum_` export is intentionally private-looking, but it must exist
41+
so other runtimes can load the function through `require(file).sum_`.
42+
43+
Use this shortcut for fixed-runtime helpers. Use `call(fn).on(runtimeName)` when
44+
the caller should choose the runtime.
45+
46+
## Function Used On Different Runtimes
47+
48+
When the caller should choose the runtime, export a runtime function and schedule
49+
it with `call(fn).on(runtimeName)(...args)`:
50+
951
```tsx
1052
import { call, runtimeFunction } from '@react-native-runtimes/core';
1153

@@ -37,40 +79,11 @@ It is rewritten to a direct call on the registered runtime function:
3779
const result = await fibonacci.runOn('fibonacci-worker-runtime', 38);
3880
```
3981

40-
For a local top-level function that should always run on one runtime, put the
41-
runtime name as the first string directive in the function body:
42-
43-
```tsx
44-
async function sum(a: number, b: number) {
45-
'background';
46-
return a + b;
47-
}
48-
49-
const result = await sum(5, 1);
50-
```
51-
52-
Metro turns that into the same registered runtime function shape:
53-
54-
```tsx
55-
export const sum_ = runtimeFunction.withId(
56-
'src/math.sum_',
57-
async function sum(a: number, b: number) {
58-
'background';
59-
return a + b;
60-
},
61-
);
62-
63-
const sum = call(sum_).on('background');
64-
const result = await sum(5, 1);
65-
```
66-
67-
The generated `sum_` export is intentionally private-looking, but it must exist
68-
so other runtimes can load the function through `require(file).sum_`.
69-
70-
## Fixed Runtime Shortcut
82+
## Function Directive Details
7183

7284
Use a function directive when the function always belongs on the same runtime.
73-
The directive must be the first statement in a top-level function declaration:
85+
The function must be declared in module/global scope, and the directive must be
86+
the first statement in the function body:
7487

7588
```tsx
7689
async function refreshCache(key: string) {
@@ -192,6 +205,7 @@ Current constraints:
192205
- arguments and return values must be JSON-serializable
193206
- scheduled functions must be exported and wrapped in `runtimeFunction`, or use
194207
the top-level function directive shortcut
208+
- directive shortcut functions must be declared in module/global scope
195209
- inline lambdas and non-exported functions are not supported
196210
- closures are not captured; pass all inputs as arguments
197211
- directive shortcut functions are rewritten to `const` aliases, so define them

0 commit comments

Comments
 (0)