Polkadot Apps
    Preparing search index...

    Higher-level abstraction providing last-write-wins channel semantics over the statement store.

    Each channel name maps to a single value. When a new value is written to a channel, it replaces the previous one if its timestamp is newer. This is ideal for presence announcements, signaling, and ephemeral state.

    interface Presence {
    type: "presence";
    peerId: string;
    timestamp: number;
    }

    const channels = new ChannelStore<Presence>(client, { topic2: "doc-123" });

    // Write presence
    await channels.write("presence/peer-abc", {
    type: "presence",
    peerId: "abc",
    timestamp: Date.now(),
    });

    // Read all presences
    for (const [name, value] of channels.readAll()) {
    console.log(`${name}: ${value.peerId}`);
    }

    // React to changes
    channels.onChange((name, value, previous) => {
    console.log(`Channel ${name} updated`);
    });

    channels.destroy();

    Type Parameters

    • T extends { timestamp?: number }

      The type of values stored in channels. Values should include a timestamp field for ordering; if omitted, the current time is used automatically.

    Index

    Constructors

    Accessors

    Methods

    Constructors

    Accessors

    Methods

    • Subscribe to channel value changes.

      The callback is invoked whenever a channel value is updated (either from the network or from a local write).

      Parameters

      • callback: (channelName: string, value: T, previous: T | undefined) => void

        Called with the channel key (hex hash for network-received, hex hash for local writes), new value, and previous value.

      Returns Unsubscribable

      A handle to unsubscribe.

    • Read the latest value for a channel by its human-readable name.

      Looks up the channel hash from the name, then retrieves the value. Also checks the hash directly for values received from the network before any local write established the name mapping.

      Parameters

      • channelName: string

        The channel name (e.g., "presence/peer-abc").

      Returns T | undefined

      The latest value, or undefined if no value has been received.

    • Read all channel values as a read-only map.

      Keys are hex-encoded channel hashes. Use read for lookup by human-readable name.

      Returns ReadonlyMap<string, T>

      A map of channel hash to latest value.

    • Write a value to a named channel.

      If the value doesn't include a timestamp, one is added automatically using Date.now(). The value is published to the statement store with the channel name as the statement channel.

      Parameters

      • channelName: string

        The channel name (e.g., "presence/peer-abc").

      • value: T

        The value to write.

      Returns Promise<boolean>

      true if the statement was accepted by the network.