LynxMainThread
A service that calls one of your main-thread scripts from Angular code, and gives you its return value. import { LynxMainThread } from '@blotch/angular-lynx';
Info
Template handlers such as (bindtap) already run on the main thread. Main-thread scripts explains when you need this API instead.
Usage
src/app/app.ts
LynxMainThread is provided in root, so inject() it anywhere.
API
runOnMainThread(handle, ...args)
handle: a function frommainThreadFn().args: its arguments, checked against its parameter types.- Returns a promise of the function's return value. It rejects if the function throws.
Where you call it changes what happens:
Constraints
- Code that runs on both threads calls it twice. A constructor,
ngOnInitoreffect()runs in both copies of your app (see Threads), so the function runs once from each. Call it from an event handler instead. - From the background thread, define the function at module scope. The call finds the function by an ID, which matches on both threads only for module-level functions.
- From the background thread, use JSON-safe arguments and return values. A development build throws on arguments that can't be serialized. A returned promise isn't awaited.
Related
- Main-thread scripts guide
mainThreadFnbackgroundFn: the other direction
