Skip to content
Lucent
SEARCH LUCENT

Search guides, APIs, and examples.

GitHub

Very early and experimental. The language, the generated native code and every package API change without notice. Do not use Lucent in production.

Send events to JavaScript

Take a JS callback, call it from async code, and return an id that stops it.

ticker.lucent.ts
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);
}
  • JavaScript passes a function, and Lucent keeps it while tick runs. Each call from async code is posted to the JS thread.
  • start returns an id and stop takes it back. Stopping is what lets go of the function.
  • An event handler called from async code must return void or a Promise.

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.