Macros
Wired classes
static wire = import.meta.og.wire({ encode, decode }) tells a class how its instances cross into islands, and every island that gets one gets the same live object. A codec with an id keeps it across navigations.
What crosses into an island is a serialized copy of a prop, not a reference. A plain object survives that copy. A class instance doesn’t: its prototype and methods are lost. A class opts in by declaring how it travels, with a codec in a static member named wire.
encode turns an instance into data that can be serialized, and decode builds an instance back from that data. You write both, so nothing is guessed from the class’s shape. Once a class has a codec, its instances can be island props and context values, and every island that holds one shares one live object, whose $state is reactive across all of them. Share one live object shows it in a page.
export class Cart {
items = $state<string[]>([]);
add(item: string) {
this.items.push(item);
}
static wire = import.meta.og.wire({
encode: (c: Cart) => $state.snapshot(c.items),
decode: (items: string[]) =>
Object.assign(new Cart(), { items })
});
}Codecs
The codec has two required fields and two optional ones:
| Field | What it does |
|---|---|
encode(instance) | Returns the data that crosses. It’s serialized like any prop, so return plain data: use $state.snapshot() on $state fields. |
decode(data) | Builds an instance from that data. It runs in the browser once per instance, and on the server when a server island receives one. |
id | A name that keeps the instance alive across navigations in the tab. See Keep it across navigations. |
merge(live, fresh) | With id: folds the next page’s server copy (fresh) into the instance the visitor already has (live). |
encode and decode are typed from each other, and merge from them, so a mistake shows in your editor.
Allowed places
wire is valid in exactly one place: a static class member named wire, set to the call, with exactly one argument. Everything else fails the build with macro_wire_misuse, which names the file, the line and what was written:
- a bare value (
const k = import.meta.og.wire); - a call anywhere but a static member: an object property, a spread, an argument, an instance member;
- a static member with another name;
- a call with no argument, or more than one.
The class also has to be exported from a module your app compiles: export class Cart, export { Cart }, or export default class Cart. That’s how the build finds it and registers it under its file and export name. An anonymous export default class with a codec fails the build (wire_class_anonymous): give it a name. A <script module> in a .svelte file works too. A class declared somewhere the plugin doesn’t compile, such as an installed package that doesn’t list the file in "ogygia.files", can’t be sent (transportable_unregistered).
An island that only receives an instance doesn’t need to import the class: the build makes sure it loads. If it can’t, the island fails with transportable_class_missing; import the class as a value there.
Design
You might expect a computed key, static [import.meta.og.wire] = …. TypeScript doesn’t allow it: a computed key on a class property must have a literal type or a unique symbol type, and import.meta.og.wire has neither, so it could never type-check. The call form type-checks. The build replaces the whole member and writes the real key into the compiled output:
The key never appears in your source, so there’s nothing to import, alias or leak. The member’s name wire is what the build looks for.
static [Symbol.for('ogygia.wire')] = { encode, decode };Persistence across navigations
By default an instance lasts as long as the page. Give the codec an id, and it lasts for the session: the same live object follows the visitor across SPA router navigations in the tab. On each new page the browser joins the server’s fresh copy to the instance it already has and calls merge. Without merge the live instance wins, so a cart mid-edit beats a fresh read. Write merge to pull in what the server knows better.
No two classes may share an id; two classes with one id log an error in the browser, in every build (continuity_id_collision). Keep state across navigations covers the whole behavior.
export class Cart {
items = $state<string[]>([]);
updatedAt = $state(0);
static wire = import.meta.og.wire({
id: 'cart',
encode: (c: Cart) => ({
items: $state.snapshot(c.items),
updatedAt: c.updatedAt
}),
decode: (d: { items: string[]; updatedAt: number }) =>
Object.assign(new Cart(), d),
merge: (live, fresh) => {
if (fresh.updatedAt > live.updatedAt)
live.items = fresh.items;
}
});
}Limits
- The server never remembers an instance. Each request decodes fresh, so one visitor’s object can’t reach another’s page. Create instances per request, in the page or in
load. - It doesn’t cross into another app. A wired instance can’t pass into a federated fragment from another build (
wired_class_foreign): pass its data instead. - It’s for classes. A function crosses with
import.meta.og.$, a store crosses on its own, and a component is a held region.