/**
 * Stream Usage Tracker
 *
 * Tracks which stream variables are actively used by components via useEffect.
 * This is the ONLY reliable signal for component lifecycle (not render-time).
 *
 * Architecture:
 * 1. Atom effect starts SSE immediately and registers a cleanup callback
 * 2. Components register via useEffect (increment count)
 * 3. Components unregister via useEffect cleanup (decrement count)
 * 4. When count hits 0 → debounce → call cleanup callbacks → SSE stops
 *
 * The atom effect cleanup is NOT reliable (Recoil caching), so we actively
 * manage cleanup from here when components unregister.
 *
 * Subscriptions are keyed by uid+extras to handle different auth contexts
 * using the same variable independently.
 */
import { type RequestExtras } from '../../api/http';
interface StreamConnection {
    /** Function to start/restart the SSE connection, returns cleanup and controller */
    start: () => {
        cleanup: () => void;
        controller: AbortController;
    };
    /** Cleanup function to abort SSE connection (set when start() is called) */
    cleanup?: () => void;
    /** AbortController for this connection (for direct abort access) */
    controller?: AbortController;
    /** Whether connection is currently active */
    active: boolean;
}
interface StreamUsage {
    /** Number of components actively subscribed (via useEffect) */
    count: number;
    /** Timer for debounced cleanup when count reaches 0 */
    cleanupTimer?: ReturnType<typeof setTimeout>;
    /** Timer for orphan cleanup when connection registers but no subscription arrives */
    orphanTimer?: ReturnType<typeof setTimeout>;
    /** Registered connections for this uid (atomKey -> connection) */
    connections: Map<string, StreamConnection>;
}
/**
 * Abort an existing connection for the given atomKey, if one exists.
 * Called before starting a new connection to ensure no duplicates.
 *
 * @param atomKey Unique key for the connection (serialized params)
 * @returns true if a connection was aborted, false otherwise
 */
export declare function abortExistingConnection(atomKey: string): boolean;
/**
 * Register a stream connection from atom effect.
 * The start function is called immediately, and will be called again
 * if the connection needs to restart after cleanup.
 *
 * @param uid Stream variable uid
 * @param extras Request extras (auth headers, etc.)
 * @param atomKey Unique key for this connection (serialized params)
 * @param start Function to start the SSE connection, returns { cleanup, controller }
 * @returns Unregister function (backup cleanup, not relied upon)
 */
export declare function registerStreamConnection(uid: string, extras: RequestExtras, atomKey: string, start: () => {
    cleanup: () => void;
    controller: AbortController;
}): () => void;
/**
 * Subscribe to a stream. Call this from component useEffect.
 *
 * @param uid Stream variable uid
 * @param extras Request extras (auth headers, etc.)
 * @returns Unsubscribe function to call in useEffect cleanup
 */
export declare function subscribeStream(uid: string, extras: RequestExtras): () => void;
/**
 * Check if a stream has active subscribers.
 */
export declare function isStreamActive(uid: string, extras: RequestExtras): boolean;
/**
 * Get subscriber count for a stream.
 */
export declare function getStreamSubscriberCount(uid: string, extras: RequestExtras): number;
/**
 * Force cleanup all streams. Useful for testing or app shutdown.
 */
export declare function cleanupAllStreams(): void;
/**
 * Clear all stream usage tracking. Used for testing.
 */
export declare function clearStreamUsage_TEST(): void;
/**
 * Get total count of active connections across all subscription keys.
 * Useful for testing.
 */
export declare function getActiveConnectionCount(): number;
/**
 * Get all active connection keys (atomKeys).
 * Useful for testing.
 */
export declare function getActiveConnectionKeys(): string[];
/**
 * Get the AbortController for a specific atomKey, if active.
 * Useful for testing.
 */
export declare function getConnectionController(atomKey: string): AbortController | undefined;
/**
 * Internal exports for testing
 */
export declare const _internal: {
    streamUsage: Map<string, StreamUsage>;
    PAUSE_DEBOUNCE_MS: number;
    ORPHAN_TIMEOUT_MS: number;
};
export {};
//# sourceMappingURL=stream-usage-tracker.d.ts.map