• Create an HMR-safe singleton accessor.

    In Next.js (and other HMR-enabled environments) module scope is re-evaluated on every hot reload, destroying any let / const singleton. This helper stores the instance on globalThis so it survives reloads.

    Type Parameters

    • T

      Type of the singleton instance.

    Parameters

    • key: string

      Unique global key (e.g. 'synapseClient').

    • factory: (() => T)

      Function that creates the instance.

        • (): T
        • Returns T

    • Optionaloptions: SingletonOptions

      Optional version for cache invalidation.

    Returns (() => T)

    A zero-arg getter that returns the singleton.

      • (): T
      • Returns T

    1.2.2

    import { createSingleton } from '@oobe-protocol-labs/synapse-client-sdk/utils';
    import { SynapseClient } from '@oobe-protocol-labs/synapse-client-sdk';

    export const getSynapseClient = createSingleton('synapseClient', () =>
    SynapseClient.fromEndpoint({ network: 'mainnet', region: 'US', apiKey: process.env.SYNAPSE_API_KEY! }),
    );

    // With version invalidation — bump to recreate:
    export const getGateway = createSingleton('agentGateway', () =>
    createAgentGateway(getSynapseClient(), config),
    { version: 4 },
    );