Connection.layer
The connection of an app, as a layer over its wallets. It starts from what storage held, and asks no wallet anything until you connect.
import { Layer, ManagedRuntime } from "effect";import { FetchHttpClient } from "effect/http";import { Connection, Signer, Transport, Wallets } from "@ethect/core";
// The layers of an app, read from the bottom up.const App = Signer.connected().pipe( // 4. signs with the connected wallet Layer.provideMerge(Connection.layer()), // 3. which wallet is connected Layer.provideMerge(Wallets.browser), // 2. which wallets are installed Layer.provideMerge(Transport.http("https://ethereum-rpc.publicnode.com")), // 1. the node it reads from Layer.provide(FetchHttpClient.layer),);
// Built once, for the life of the page: the connection is the one of the runtime.const runtime = ManagedRuntime.make(App);An app on several chains gives
Transport.chains in place of Transport.http. Nothing else
changes.
What storage held
Section titled “What storage held”initial is the state the last visit stored. A stored connection starts as reconnecting:
enough to render with, and not yet confirmed by the wallet.
import { Effect, Layer } from "effect";import { Connection, Wallets } from "@ethect/core";
// `cookies` is `document.cookie`, which the app wrote to with `Connection.toCookie`.const App = (cookies: string | undefined) => Connection.layer({ initial: Connection.fromCookie(cookies) }).pipe( Layer.provideMerge(Wallets.browser), );
// In the browser, on load: ask the remembered wallet for its accounts, without a prompt.const onLoad = Effect.gen(function* () { const before = yield* Connection.get; // { status: "reconnecting", wallet: "io.metamask", … } const after = yield* Connection.reconnect; // "connected", or "disconnected" if the wallet is gone return { before, after };});See Connection.toCookie for the cookie,
Connection.toJson for what it holds, and
Connection.reconnect for what confirms it.
On a server
Section titled “On a server”The same layers with no wallet. The state of the cookie is the state, and nothing can sign.
import { Layer } from "effect";import { FetchHttpClient } from "effect/http";import { Connection, Signer, Transport, Wallets } from "@ethect/core";
// `cookies` is the `Cookie` header of the request, or `null` when it has none.const Server = (cookies: string | null) => Signer.connected().pipe( Layer.provideMerge(Connection.layer({ initial: Connection.fromCookie(cookies) })), Layer.provideMerge(Wallets.of([])), Layer.provideMerge(Transport.http("https://ethereum-rpc.publicnode.com")), Layer.provide(FetchHttpClient.layer), );Wallets.browser works there too: where there is no window it is an empty list. Building
these layers on a server touches no browser API.
How long a wallet is waited for
Section titled “How long a wallet is waited for”A browser wallet announces itself a moment after the page loads.
reconnect waits for the remembered one, and
gives up after reconnectTimeout. Once the wallet is there, its answer is waited for as long
again.
import { Connection } from "@ethect/core";
declare const stored: Connection.State;const Patient = Connection.layer({ initial: stored, reconnectTimeout: "5 seconds" });Returns
Section titled “Returns”Layer<Connection.Connection, never, Wallets.Wallets>
A layer that provides the Connection service. It
needs the Wallets of the app, and building it cannot fail.
Parameters
Section titled “Parameters”options.initial (optional)
Section titled “options.initial (optional)”- Type:
Connection.State - Default:
{ status: "disconnected" }
What storage held, usually the result of
Connection.fromCookie or of
Connection.fromJson.
initial is |
The connection starts as |
|---|---|
reconnecting, or connected |
reconnecting, with the same wallet, accounts and chain |
disconnected, connecting, or not given |
disconnected |
options.reconnectTimeout (optional)
Section titled “options.reconnectTimeout (optional)”- Type:
Duration.Input - Default:
"2 seconds"
How long reconnect waits, twice over. A number
is milliseconds.
| It waits for | Up to | Past it |
|---|---|---|
the remembered wallet to be among the Wallets |
reconnectTimeout |
disconnected |
| then that wallet’s provider and its answer, together | reconnectTimeout again |
disconnected |
So a reconnection takes twice reconnectTimeout at most: four seconds by default. It cannot
wait without end, and neither can a send that waits for it.
The first wait is only for a list that may still grow. Over
Wallets.of, or Wallets.browser where there is no window, a
wallet that is not listed ends the reconnection at once.
Behavior
Section titled “Behavior”- One connection per layer. Build the layer once, in the runtime of the app. Each build has a state of its own.
- No wallet is asked when it is built. No request, no listener, no prompt: a wallet is
first asked by
connector byreconnect. - A stored connection is not a connection. It starts as
reconnecting, and stays so untilreconnectruns. What it holds comes from a cookie: it is good enough to render with, and it proves nothing. - One wallet at a time. Connecting another wallet lets go of the one before.
- Everything goes when its scope closes. The listeners on the connected wallet are
removed, a
connectthat waits for its prompt is interrupted, and areconnectthat waits for its wallet is too.
Under the hood
Section titled “Under the hood”building the layer
- your programthe state is
initial, read as above. No wallet is asked
- your programthe state is
while a wallet is connected
- your programlistens to
accountsChanged,chainChangedanddisconnecton its provider - the wallet
eth_accountsadisconnectasks it, once: the connection stays if the wallet still has accounts
- your programlistens to
the layer's scope closes
- your programthe listeners are removed
The requests are sent by connect and reconnect and by
switchChain.