Primitives / foldkit-primitives/net

SharedHost

namespace · packages/primitives/src/net/sharedHost.ts:194

const SharedHost = { define }

Its type

interface SharedHost<Opening> {
  readonly name: string
  readonly serve: (start: StartHost<Opening>) => void
  readonly connect: (
    options: SharedHostOptions<Opening>,
  ) => SharedHostConnection<Opening>
}

One server in the browser for many documents: every tab and frame of an application meets the same host. A page opens conversations, each a MessagePort with a decoded opening that says what it is for; the host, in a SharedWorker shared by every tab, or in the top document where there is no SharedWorker, is handed each one with a signal that aborts when the page that opened it is gone.

It owns the host's lifetime and the routing of conversations, nothing spoken over them: Sync's portSocket and Remote's port do that.

In the guides

Members

SharedHost.define

function · packages/primitives/src/net/sharedHost.ts:51

define<Opening, Encoded>(options: {
  readonly name: string
  readonly opening: Schema.Codec<Opening, Encoded, never, never>
}): SharedHost<Opening>

A shared host: its name, which keeps two on one origin apart, and the schema of what opens a conversation, which the host decodes before anything else, ignoring a message that does not decode.