@@ -230,6 +230,48 @@ function running on a chosen runtime. Arguments and return values are serialized
230230as JSON. This is the request/response API for work that should execute on
231231another 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
234276import { call , runtimeFunction } from ' @react-native-runtimes/core' ;
235277
@@ -261,40 +303,11 @@ transformer rewrites it before the app runs:
261303await 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
296308Use 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
300313async 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
0 commit comments