Skip to content

Interface: RouterBridge

API / @xmachines/play-svelte-spa-router / RouterBridge

Defined in: play-router/src/types.ts:436

The RouterBridge interface of a runtime infrastructure adapter

The interface defines the connection of the lifecycle between the infrastructure, for example a framework router, and the Actor. The infrastructure builds a “bridge” to the Actor: it observes the atoms of the Actor, and it manages its own lifecycle accordingly.

Architectural context: the interface implements Passive Infrastructure (INV-04), because it gives an observation in one direction. The infrastructure connects to observe the atoms of the Actor (currentRoute, currentView, and state), and it reflects each change. It makes no decision about the state.

Example

The implementation of a framework router bridge

import type { RouterBridge } from "@xmachines/play-router";
import { watchAtom } from "@xmachines/play-atom";
class MyRouterBridge implements RouterBridge {
private stop: (() => void) | null = null;
async connect(): Promise<void> {
// Start the observation of the actor.currentRoute atom
this.stop = watchAtom(actor.currentRoute, (route) => {
if (route) router.navigate(route);
});
}
async disconnect(): Promise<void> {
// Stop the observation, and release the subscription
this.stop?.();
this.stop = null;
}
}

See

Play RFC - invariant INV-04

Methods

connect()

connect(): void | Promise<void>;

Defined in: play-router/src/types.ts:453

Connects the router bridge to the Actor

The infrastructure calls it when it must start the observation of the Actor atoms, and when it must bring its own state, for example the browser URL, in line with the Actor state.

Returns

void | Promise<void>

The promise that resolves after the connection, or void for a synchronous connection

Example

const bridge: RouterBridge = createBridge(actor, router);
await bridge.connect();
// The bridge observes the actor.currentRoute atom now

disconnect()

disconnect(): void | Promise<void>;

Defined in: play-router/src/types.ts:469

Disconnects the router bridge from the Actor

The infrastructure calls it when it must stop the observation and free its resources, for example an atom watcher and an event listener.

Returns

void | Promise<void>

The promise that resolves after the disconnection, or void for a synchronous disconnection

Example

await bridge.disconnect();
// The bridge stopped its observation, and it freed its resources