• RAII pattern: acquire a ref, run fn, auto-release.

    The ref is released in a finally block so cleanup is guaranteed even if fn throws. Zero chance of leaking.

    Type Parameters

    • T

      The service type.

    • R

      The return type of fn.

    Parameters

    • ctx: SynapseContext

      The context to acquire from.

    • token: ServiceToken<T>

      The service token.

    • fn: ((service: T) => R)

      Receives the unwrapped service value.

        • (service): R
        • Parameters

          • service: T

          Returns R

    Returns R

    The return value of fn.

    1.2.0

    const balance = withRef(ctx, Tokens.RPC, rpc => rpc.getBalance(pubkey));
    // ref is auto-released — no leak