{"version":3,"file":"Reconnection.mjs","sources":["../src/Reconnection.ts"],"sourcesContent":["import type { Room } from './Room.ts';\n\nexport interface ReconnectionOptions {\n    /**\n     * Whether automatic reconnection is enabled.\n     * Set to `false` to disable automatic reconnection entirely.\n     * @default true\n     */\n    enabled: boolean;\n\n    /**\n     * The maximum number of reconnection attempts.\n     */\n    maxRetries: number;\n\n    /**\n     * The minimum delay between reconnection attempts.\n     */\n    minDelay: number;\n\n    /**\n     * The maximum delay between reconnection attempts.\n     */\n    maxDelay: number;\n\n    /**\n     * The minimum uptime of the room before reconnection attempts can be made.\n     */\n    minUptime: number;\n\n    /**\n     * The current number of reconnection attempts.\n     */\n    retryCount: number;\n\n    /**\n     * The initial delay between reconnection attempts.\n     */\n    delay: number;\n\n    /**\n     * The function to calculate the delay between reconnection attempts.\n     * @param attempt - The current attempt number.\n     * @param delay - The initial delay between reconnection attempts.\n     * @returns The delay between reconnection attempts.\n     */\n    backoff: (attempt: number, delay: number) => number;\n\n    /**\n     * The maximum number of enqueued messages to buffer.\n     */\n    maxEnqueuedMessages: number;\n\n    /**\n     * Buffer for messages sent while connection is not open.\n     * These messages will be sent once the connection is re-established.\n     */\n    enqueuedMessages: Array<{ data: Uint8Array }>;\n\n    /**\n     * Whether the room is currently reconnecting.\n     */\n    isReconnecting: boolean;\n}\n\n/** Fresh per-room reconnection config + buffer state (defaults). One per `Room`\n *  instance — `enqueuedMessages` must not be shared. */\nexport function createReconnection(): ReconnectionOptions {\n    return {\n        enabled: true,\n        retryCount: 0,\n        maxRetries: 15,\n        delay: 100,\n        minDelay: 100,\n        maxDelay: 5000,\n        minUptime: 5000,\n        backoff: exponentialBackoff,\n        maxEnqueuedMessages: 10,\n        enqueuedMessages: [],\n        isReconnecting: false,\n    };\n}\n\nexport const exponentialBackoff = (attempt: number, delay: number) => {\n    return Math.floor(Math.pow(2, attempt) * delay);\n};\n\n/** Buffer a message to flush on (re)connect, evicting the oldest past the cap. */\nexport function enqueueMessage(room: Room, message: Uint8Array) {\n    room.reconnection.enqueuedMessages.push({ data: message });\n    if (room.reconnection.enqueuedMessages.length > room.reconnection.maxEnqueuedMessages) {\n        room.reconnection.enqueuedMessages.shift();\n    }\n}\n"],"names":[],"mappings":";;;;;;AAiEA;AACwD;SACxC,kBAAkB,GAAA;IAC9B,OAAO;AACH,QAAA,OAAO,EAAE,IAAI;AACb,QAAA,UAAU,EAAE,CAAC;AACb,QAAA,UAAU,EAAE,EAAE;AACd,QAAA,KAAK,EAAE,GAAG;AACV,QAAA,QAAQ,EAAE,GAAG;AACb,QAAA,QAAQ,EAAE,IAAI;AACd,QAAA,SAAS,EAAE,IAAI;AACf,QAAA,OAAO,EAAE,kBAAkB;AAC3B,QAAA,mBAAmB,EAAE,EAAE;AACvB,QAAA,gBAAgB,EAAE,EAAE;AACpB,QAAA,cAAc,EAAE,KAAK;KACxB;AACL;MAEa,kBAAkB,GAAG,CAAC,OAAe,EAAE,KAAa,KAAI;AACjE,IAAA,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,CAAC,GAAG,KAAK,CAAC;AACnD;AAEA;AACM,SAAU,cAAc,CAAC,IAAU,EAAE,OAAmB,EAAA;AAC1D,IAAA,IAAI,CAAC,YAAY,CAAC,gBAAgB,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;AAC1D,IAAA,IAAI,IAAI,CAAC,YAAY,CAAC,gBAAgB,CAAC,MAAM,GAAG,IAAI,CAAC,YAAY,CAAC,mBAAmB,EAAE;AACnF,QAAA,IAAI,CAAC,YAAY,CAAC,gBAAgB,CAAC,KAAK,EAAE;IAC9C;AACJ;;;;"}