The plugin contract

contract.d.ts is the source of truth of a plugin: the calls, the starting values and the events of each of its channels. lecodes plugin gen reads it and writes the app's wrapper and the frame of every native half. This page lists everything the file can say. For the walk through a whole plugin, see Write a native plugin.

The generator is strict on purpose. A line it does not understand is an error with its line number, never a silent guess, and every problem of the file is reported at once.

What the file holds

Type imports from lecodes-sdk/plugin, interfaces and type aliases — nothing else:

TypeScript
import type { Service, View, UIHostNode, File, Json, Entity, Scene3D, i32, f64 } from "lecodes-sdk/plugin"
  • Import a name under its own name (no as): the generator reads names.
  • No generic types of your own, no classes, no values.
  • A struct extends nothing: repeat the members, or name the other struct as a member.

Declare a channel

A channel is an interface that extends one marker:

TypeScript
export interface Dial extends Service<"acme.dial", DialParams, DialEvents> { … }
export interface PlayerView extends View<"acme.player", PlayerParams, PlayerEvents> { … }
export interface Player extends UIHostNode<"acme.player", PlayerProps, PlayerEvents> { … }
markerchannelthe three arguments
Serviceno pixelsname, params, events
Viewfull screenname, params, events
UIHostNodea box in a screen's layoutkind, props, events
  • The name is a string literal in camelCase, with your vendor's prefix before a dot: "acme.dial". It is what the halves register under.
  • A contract may declare several channels. A View and a host node may share one name.
  • A channel may also extend plain interfaces of the contract that hold methods. Their methods are its own — how a View and a host node share theirs:
TypeScript
export interface PlayerMethods { seek(seconds: f64): Promise<void> }
export interface PlayerView extends View<"acme.player", PlayerParams, PlayerEvents>, PlayerMethods {}
export interface Player extends UIHostNode<"acme.player", PlayerProps, PlayerEvents>, PlayerMethods {}

Params: what a channel starts with

TypeScript
export interface DialParams { rate?: f64 }

The params are an interface of the contract. Every param is optional — the half falls back on its default. A service's params are what a session is opened with; a View's are what it is created with. A channel without any still names an empty interface.

Methods: what the app asks

TypeScript
  /** @rejects unavailable busy */
  read(channel: i32, raw?: boolean): Promise<DialReading>
  • A method returns Promise<…>; Promise<void> for no result.
  • An argument is name: Type or name?: Type; optional ones come last.
  • No overloads, no generic methods, no optional methods (a method added later is tagged @since).
  • A name must be usable in TypeScript, Swift and Kotlin: no keyword of the three, no leading _ or $, and none of the names every wrapper has already (call, on, off, open, close, destroy, style).

Events: what the half sends

TypeScript
export interface DialEvents {
  /** @lazy */
  reading: DialReading
  connected: void
}

An event maps its name to its payload; void for none. An event is not optional and is not written as a method.

The types a contract can carry

typemeaning
i32, f64, boolean, stringscalars. number is refused: say which. A number that is not finite arrives as null
Uint8Arraybytes, as they are: Data in Swift, ByteArray in Kotlin
Filebytes that stay in the host; what crosses is a handle — a photo the app then shows, uploads or shares
Jsonany JSON value, passed through untyped
Entity, Scene3Da 3D node or scene of the app, for a half that drives the 3D engine
a union of string literalsan enum: "front" \| "back"
interfacea struct
T[], [A, B], { [key: string]: T }a list, a tuple of two or three, a map
T \| null, name?:a nullable value, an optional member or argument

void is the payload of an event or the result of a call, nothing else. An absent value is an optional member, not undefined.

Tags

A tag goes in the doc comment of a method or an event.

@rejects <code…> — how a call can fail

TypeScript
  /** @rejects unavailable busy failed */
  set(on: boolean): Promise<void>

The codes become an enum in each half (reply.reject(.busy)), and the app's promise rejects with the code as the error's message. A code is camelCase.

@since <n> — a member added later

TypeScript
  /** @since 2 */
  health(): Promise<f64>

The contract's version is the highest @since in it, 1 without any. A half registers with the version it was generated from. The generated supports("health") tells the app whether the half in this build has the method; a call to one it lacks rejects.

@permission <kind…> — an OS permission the call needs

TypeScript
  /** @permission microphone
   *  @rejects denied failed */
  record(): Promise<File>

The generated native code asks for the permission before the call reaches the half, and rejects with denied when the user refuses. camera and microphone are built in; any other kind is one the plugin declares in its manifest (The plugin manifest). The web half asks nothing: the browser's own API prompts. notifications is not tagged — it is asked by hand.

@lazy — an event sent only while the app listens

TypeScript
  /** @lazy */
  reading: DialReading

For an event that ticks: a position, a level. Each half gets a readingObserved(observed) to implement — start what produces the event on true, stop it on false — and an event sent while nobody listens is dropped. Tag the event when you add it: an app built before the tag never says it listens. Not for a host node's event.

@sets <param> — the call that changes a setting

TypeScript
export interface CameraParams { facingMode?: Facing }

export interface CameraView extends View<"camera", CameraParams, CameraEvents> {
  /** @sets facingMode
   *  @rejects unavailable */
  setFacingMode(mode: Facing): Promise<void>
}

A setting is a param (of a View) or a prop (of a host node) that has such a call. The call takes the new value alone and returns Promise<void>. The generated wrapper then gives the setting one shape on the app's side: a read-only property (camera.facingMode) that follows a call that went through, and the call as the only way to change it — it rejects where it is asked (Plugin nodes). One call per setting; not for a Service.

What a View adds

A View's half is created with the params and draws full screen. The methods and events are as a service's; the half also gets a destroy() when the view is gone. The hosts create a view's native side when it is first presented and keep it across dismissals, so a call before the first presentation has nothing to reach.

What a host node adds

TypeScript
export interface TabItem { id: string; label: string }
export interface TabBarProps { items: TabItem[]; selected: string }
export interface TabBarEvents { select: { id: string; index: i32 } }

export interface TabBar extends UIHostNode<"acme.tabbar", TabBarProps, TabBarEvents> {
  styles: "tintColor" | "color"
  measure: true
}
  • Props are state the app writes: one setter each, never read back from the host. A prop is named in camelCase and crosses as text, so it holds no File, Uint8Array or Entity — a method takes those.
  • styles lists the styles the half is handed, already resolved (a color with the theme applied, a number). Among them, colors: color, tintColor, placeholderColor, rippleColor, overlayColor; numbers: fontSize, fontWeight, lineHeight, letterSpacing, lineClamp. A name that is not such a style is refused with the full list. The box itself — its size, corner radius, border, shadow, opacity — is drawn around the half by the host and is never listed.
  • measure: true — the half answers the node's size. Left out, the node's styles size it.
  • paints: "self" — the half paints its own background; backgroundColor may then be listed in styles.
  • An event is named in camelCase and is listened to as on<Event>. A node's call takes no @permission: ask in a Service of the same plugin.
  • A name the element has already is refused for a prop, a method or an event's listener (name, style, onClick, parent, children and the like).

Name the interface without a Node ending: the generated element adds it (TabBar → TabBarNode).

What lecodes plugin gen writes

For a channel whose interface is X:

Service or Viewhost node
the app (sdk/)XBase — the methods and a typed on; supports(); the finished export X when there is no hand-written sdk/<id>.tsthe element XNode, its style type XNodeStyle, XNode.supported
iOSXCode, XEvents, protocol XPlugin, XChannel.register(in:)XNodeInstance, XNodeEvents, XNodeGlue.register(in:)
Androidthe same names in Kotlinthe same names in Kotlin
webXError, XEvents, XPlugin, registerX(host, make)XNodeHalf, XNodeEvents, registerXNode(host, make)
  • The app's file is sdk/<id>.gen.ts for a contract with one channel, and sdk/<id>.<Interface>.gen.ts per channel for one with several.
  • The native files are a .gen.swift file per channel in ios/, a .gen.kt file per channel in the folder of the plugin's Kotlin package, and web/<id>.gen.ts.
  • A platform's file is written where its folder exists.
  • lecodes plugin gen --check writes nothing and exits with 1 when a generated file is missing or stale.

Next