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:
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:
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> { … }| marker | channel | the three arguments |
|---|---|---|
Service | no pixels | name, params, events |
View | full screen | name, params, events |
UIHostNode | a box in a screen's layout | kind, 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:
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
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
/** @rejects unavailable busy */
read(channel: i32, raw?: boolean): Promise<DialReading>- A method returns
Promise<…>;Promise<void>for no result. - An argument is
name: Typeorname?: 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
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
| type | meaning |
|---|---|
i32, f64, boolean, string | scalars. number is refused: say which. A number that is not finite arrives as null |
Uint8Array | bytes, as they are: Data in Swift, ByteArray in Kotlin |
File | bytes that stay in the host; what crosses is a handle — a photo the app then shows, uploads or shares |
Json | any JSON value, passed through untyped |
Entity, Scene3D | a 3D node or scene of the app, for a half that drives the 3D engine |
| a union of string literals | an enum: "front" \| "back" |
interface | a 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
/** @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
/** @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
/** @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
/** @lazy */
reading: DialReadingFor 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
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
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,Uint8ArrayorEntity— a method takes those. styleslists 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;backgroundColormay then be listed instyles.- 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,childrenand 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 View | host node | |
|---|---|---|
the app (sdk/) | XBase — the methods and a typed on; supports(); the finished export X when there is no hand-written sdk/<id>.ts | the element XNode, its style type XNodeStyle, XNode.supported |
| iOS | XCode, XEvents, protocol XPlugin, XChannel.register(in:) | XNodeInstance, XNodeEvents, XNodeGlue.register(in:) |
| Android | the same names in Kotlin | the same names in Kotlin |
| web | XError, XEvents, XPlugin, registerX(host, make) | XNodeHalf, XNodeEvents, registerXNode(host, make) |
- The app's file is
sdk/<id>.gen.tsfor a contract with one channel, andsdk/<id>.<Interface>.gen.tsper channel for one with several. - The native files are a
.gen.swiftfile per channel inios/, a.gen.ktfile per channel in the folder of the plugin's Kotlin package, andweb/<id>.gen.ts. - A platform's file is written where its folder exists.
lecodes plugin gen --checkwrites nothing and exits with 1 when a generated file is missing or stale.
Next
- Write a native plugin — the walk through a whole plugin
- The plugin manifest — what each platform needs to build it