Skip to content

Subscription.overload

Groups several definitions of one subscription kind into a single function whose items depend on its params. A call goes to the first variant whose params accept the arguments.

import { Schema } from "effect";
import { Hash } from "@ethect/core";
import { Subscription, Transaction } from "@ethect/core/rpc";
const full = Schema.Literal(true);
const hashes = Schema.optionalKey(Schema.Literal(false));
const pending = Subscription.overload(
Subscription.make<[full: unknown]>()("newPendingTransactions", [full], Transaction.schema),
Subscription.make<[full?: unknown]>()("newPendingTransactions", [hashes], Hash.schema),
);
pending(); // Stream<Hash, …>: the hash of each pending transaction
pending(true); // Stream<Transaction, …>: each pending transaction in full

This is how Eth.subscribe.newPendingTransactions is defined.

A function with the call signatures of every variant, plus two properties:

Property Type Holds
kind string the kind of the first variant
variants a tuple of Subscription the definitions, in order

The definitions to group. They normally share a kind and differ by a literal in their params.

import { Schema } from "effect";
import { Hash } from "@ethect/core";
import { Block, Subscription } from "@ethect/core/rpc";
const full = Schema.Literal("full");
const heads = Subscription.overload(
Subscription.make<[detail: unknown]>()("acme_heads", [full], Block.headerSchema),
Subscription.make()("acme_heads", [], Hash.schema),
);
  • First match wins. The arguments are tested against each variant’s params in order: put the most specific variant first.
  • No match falls back to the first variant, whose stream then fails with a CodecError on the params.
  • No params or item of its own. Read them on a variant: Eth.subscribe.newPendingTransactions.variants[0].item.