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.

2. Keep the trip in native code

A Trip class that holds its fixes natively, and a summary returned as a copied object.

src/trip.lucent.ts
const seconds = (b.time - a.time) / 1000;
return seconds > 0 ? distance(a, b) / seconds : 0;
}
export type TripSummary = { fixes: number; meters: number; seconds: number; topSpeed: number };
/** A trip keeps its fixes in native code; JavaScript holds the Trip. */
export class Trip {
private fixes: Fix[] = [];
add(fix: Fix): void {
this.fixes.push(fix);
}
get count(): number {
return this.fixes.length;
}
summary(): TripSummary {
let meters = 0;
let topSpeed = 0;
for (let i = 1; i < this.fixes.length; i++) {
const a = this.fixes[i - 1]!;
const b = this.fixes[i]!;
meters += distance(a, b);
topSpeed = Math.max(topSpeed, speed(a, b));
}
const first = this.fixes[0];
const last = this.fixes[this.fixes.length - 1];
const seconds = first && last ? (last.time - first.time) / 1000 : 0;
return { fixes: this.fixes.length, meters, seconds, topSpeed };
}
}

Trip is a class, so JavaScript gets a handle on one native object rather than a copy. Its fixes stay in native code: JavaScript sends each fix once, with add.

  • trip.add(fix) copies the fix. Changing fix in JavaScript afterwards doesn't change the trip.
  • trip.summary() returns a new object on each call: a copy of a TripSummary struct.
  • trip itself crosses by reference. Passing it back to Lucent later gives the same object, with its fixes.
  • trip.count is a getter: reading it calls into native code.

This is the shape Design the boundary first recommends: the data stays native, and small values cross.

The screen shows 5 fixes, 3.55 km, top speed 1.6 m/s.