Send events to JavaScript
Take a JS callback, call it from async code, and return an id that stops it.
import { delay } from "lucent:core";
const running = new Set<number>();let nextId = 1;
/** Calls `onTick` every `ms` milliseconds, until `stop(id)`. */export async function start(ms: number, onTick: (count: number) => void): Promise<number> { const id = nextId++; running.add(id); void tick(id, ms, onTick); return id;}
async function tick(id: number, ms: number, onTick: (count: number) => void): Promise<void> { let count = 0; while (running.has(id)) { await delay(ms); if (running.has(id)) onTick(++count); }}
export function stop(id: number): void { running.delete(id);}import { useEffect, useState } from "react";import { Text } from "react-native";import { start, stop } from "./src/ticker.lucent";
export function Clock() { const [count, setCount] = useState(0); useEffect(() => { const started = start(1000, setCount); return () => void started.then(stop); }, []); return <Text>{count}</Text>;}- JavaScript passes a function, and Lucent keeps it while
tickruns. Each call from async code is posted to the JS thread. startreturns an id andstoptakes it back. Stopping is what lets go of the function.- An event handler called from async code must return
voidor aPromise.
For events that come from the SDK, keep the delegate or listener in a Map by id instead of the Set. Implement a delegate or listener shows one, and the network state example the whole pattern.