"use strict";
/**
 * Composes {@link runReconnectLoop} + {@link createBufferedDispatcher} +
 * {@link makeSubscriptionHandle} for single-source listeners (slot, native SOL).
 *
 * Multi-source listeners (oracle, fan-out per address) compose these
 * primitives directly — they share one dispatcher across multiple loops,
 * which is outside this helper's single-source shape.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.createStandaloneSubscription = createStandaloneSubscription;
const bufferedDispatcher_1 = require("./bufferedDispatcher");
const subscriptionHandle_1 = require("./subscriptionHandle");
const wsReconnectLoop_1 = require("./wsReconnectLoop");
function createStandaloneSubscription(opts) {
    const dispatcher = (0, bufferedDispatcher_1.createBufferedDispatcher)({
        scheduler: opts.scheduler,
        throttleMs: opts.throttleMs,
        keyOf: opts.keyOf,
        onDeliver: opts.onChange,
        onError: opts.onError,
        errorPrefix: opts.errorPrefix,
        callSiteName: opts.callSiteName,
    });
    const stopReconnectLoop = (0, wsReconnectLoop_1.runReconnectLoop)({
        wsRpc: opts.wsRpc,
        reconnectDelayMs: opts.reconnectDelayMs,
        onError: opts.onError,
        onLog: opts.onLog,
        onBeforeReconnect: opts.onBeforeReconnect,
        subscribe: opts.subscribe,
        onMessage: (message) => dispatcher.push(opts.toEvent(message)),
    });
    return (0, subscriptionHandle_1.makeSubscriptionHandle)(() => {
        // Drop dispatcher first so an in-flight scheduler tick can't re-fire
        // while we're tearing down the loop.
        dispatcher.unsubscribe();
        stopReconnectLoop();
    }, () => dispatcher.flush());
}
//# sourceMappingURL=createStandaloneSubscription.js.map