Skip to content

Class: SvelteSpaRouterBridge

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

Defined in: play-svelte-spa-router/src/svelte-spa-router-bridge.ts:30

The Svelte SPA router bridge — it connects svelte-spa-router to RouterBridgeBase.

It implements the three abstract methods:

  • navigateRouter → push(path) from svelte-spa-router
  • watchRouterChanges → win.addEventListener("hashchange", ...)
  • unwatchRouterChanges → win.removeEventListener("hashchange", ...)

It overrides two optional hooks:

  • getInitialRouterPath → router.loc.location
  • getInitialRouterSearch → the querystring of router.loc

The bridge inherits the restore-or-deeplink detection, isProcessingNavigation, and lastSyncedPath from RouterBridgeBase.

Param

actor

A Routable actor.

Param

routeMap

The bidirectional route map.

Param

win

The window-like object for the hashchange subscription. The default is the global window, when one exists. In an environment without a window, such as SSR or Node, the bridge skips the hashchange subscription. The constructor and connect() are then safe, as the documented behavior “it does nothing in an SSR environment” says. Give a mock in a test.

Extends

Constructors

Constructor

new SvelteSpaRouterBridge(
actor,
routeMap,
win?,
options?
): SvelteSpaRouterBridge;

Defined in: play-svelte-spa-router/src/svelte-spa-router-bridge.ts:33

Parameters

ParameterType
actorRoutableActor
routeMapRouteMap
winWindowLike | undefined
options?RouterBridgeOptions

Returns

SvelteSpaRouterBridge

Overrides

RouterBridgeBase.constructor

Accessors

basePath

Get Signature

get basePath(): string;

Defined in: play-router/src/router-bridge-base.ts:282

The resolved URL prefix that the machine of this bridge is mounted under, or "" when the machine owns the complete router.

Returns

string

The resolved URL prefix of the mount, or "" when the machine owns the complete router.

Inherited from

RouterBridgeBase.basePath


basePathParams

Get Signature

get basePathParams(): Readonly<Record<string, string>>;

Defined in: play-router/src/router-bridge-base.ts:311

The resolved values of the :param segments of the mount, or {} for a prefix without a param.

These params belong to the HOST: the host wrote the prefix and resolved the values, and they describe the route of the host and not the route of the machine. They therefore travel in NO play.route event. The machine is authoritative over its own params, and event.params holds what the pattern of the machine declares, and nothing else.

They live here instead, because a value that reached the actor on a navigation ALONE would go stale: setBasePath() can move the mount while the machine stays on the same route, no event goes out, and event.params would then contradict basePath. A read of this accessor cannot go stale.

A machine that needs the identity of its host — a machineId, a tenant — takes it through the input of the actor, where it belongs: that identity decides WHICH machine runs, and it is not a param of a route inside the machine.

Example
bridge.basePath; // "/abc123/play"
bridge.basePathParams; // { machineId: "abc123" }
Returns

Readonly<Record<string, string>>

The resolved values of the :param segments of the mount, or {} for a prefix without a param.

These params belong to the HOST, so they travel in NO play.route event — the machine is authoritative over its own params. This accessor is therefore the one way a host reads them back, and it cannot go stale: setBasePath() can move the mount while the machine stays on the same route, and no event goes out.

Inherited from

RouterBridgeBase.basePathParams

Methods

connect()

connect(): void;

Defined in: play-router/src/router-bridge-base.ts:533

Connects the router bridge to the Actor.

The method installs the atom watcher of the direction from the actor to the router. It then starts the watch of the router changes, which each framework does in its own way.

The order of these steps is part of the contract of the bridge:

  • The constructor seeds lastActorRoute from actor.currentRoute, and it records no location
  • The method installs the actor watcher before the router subscriptions of the adapter
  • The first synchronization then separates a deep link from a restore, with actor.initialRoute

An adapter that needs a different behavior of the first synchronization overrides getInitialRouterPath(). It does not change the order of the steps of connect().

Returns

void

Inherited from

RouterBridgeBase.connect


disconnect()

disconnect(): void;

Defined in: play-router/src/router-bridge-base.ts:795

Disconnects the router bridge from the Actor.

The method stops the watch of the atom, and it removes the router listener of the framework.

Returns

void

Inherited from

RouterBridgeBase.disconnect


setBasePath()

setBasePath(basePath?, basePathParams?): void;

Defined in: play-router/src/router-bridge-base.ts:351

Moves the machine to a different mount point, while it stays connected.

This is the “load and unload” half of a shared router. It moves WHERE an actor is mounted, and never WHICH actor is mounted: the actor, the route map, and its LRU cache all stay, and nothing goes away.

An actor never changes identity. A prefix that IDENTIFIES the actor — a machineId that names the document it runs — therefore never moves through this method: a new identity is a new actor, and a new actor takes a new bridge, because connect() permits one bridge for each actor. The segments that move here are the ones that LOCATE: a region, a locale, a tenant, a workspace slug.

The call then brings the location in step with the NEW prefix. A location inside the new mount runs the same first-synchronization decision as connect(): it drives the actor, and a restore keeps the route of the actor. A location OUTSIDE the new mount is the old mount in practice, so the bridge writes the new one itself, and it keeps the route of the actor while it does so — nothing else moves the address bar. A call that resolves to the current prefix changes no location, and it therefore reconciles nothing.

Parameters

ParameterTypeDescription
basePath?stringThe new prefix, as a pattern or as a concrete path. An absent value, "", and "/" all remove the prefix, and they give the machine the complete router again.
basePathParams?Record<string, string | number>The values of the :param segments of basePath.

Returns

void

Throws

For a prefix that resolves to one concrete path never.

Throws

When a :param of the prefix has no value.

Example

// The host moved this actor to another place in its URL space. `machineId` names
// WHICH document the actor runs, and it does not change: a new identity is a new
// actor, and therefore a new bridge, because one actor takes one bridge.
bridge.setBasePath("/:region/:machineId/play", { region: "us", machineId });

Inherited from

RouterBridgeBase.setBasePath