{
  "version": 3,
  "sources": ["../../../src/providers/http-polling/http-polling-provider.ts"],
  "sourcesContent": ["/**\n * External dependencies\n */\nimport type * as Y from 'yjs';\nimport { ObservableV2 } from 'lib0/observable';\nimport { Awareness } from 'y-protocols/awareness';\n\n/**\n * Internal dependencies\n */\nimport type {\n\tConnectionStatus,\n\tProviderCreator,\n\tProviderCreatorResult,\n} from '../../types';\nimport { pollingManager } from './polling-manager';\n\nexport interface ProviderOptions {\n\tawareness?: Awareness;\n\tdebug?: boolean;\n\troom: string;\n\tydoc: Y.Doc;\n}\n\n/**\n * Event types for HttpPollingProvider.\n * ObservableV2 expects event handlers as functions.\n */\ntype HttpPollingEvents = {\n\tstatus: ( status: ConnectionStatus ) => void;\n};\n\n/**\n * Yjs provider that uses HTTP polling for real-time synchronization. It manages\n * document updates and awareness states through a central sync server.\n */\nclass HttpPollingProvider extends ObservableV2< HttpPollingEvents > {\n\tprotected awareness: Awareness;\n\tprotected status: ConnectionStatus[ 'status' ] = 'disconnected';\n\tprotected synced = false;\n\n\tpublic constructor( protected options: ProviderOptions ) {\n\t\tsuper();\n\t\tthis.log( 'Initializing', { room: options.room } );\n\n\t\tthis.awareness = options.awareness ?? new Awareness( options.ydoc );\n\t\tthis.connect();\n\t}\n\n\t/**\n\t * Connect to the endpoint and initialize sync.\n\t */\n\tpublic connect(): void {\n\t\tthis.log( 'Connecting' );\n\n\t\tpollingManager.registerRoom( {\n\t\t\troom: this.options.room,\n\t\t\tdoc: this.options.ydoc,\n\t\t\tawareness: this.awareness,\n\t\t\tlog: this.log,\n\t\t\tonStatusChange: this.emitStatus,\n\t\t\tonSync: this.onSync,\n\t\t} );\n\t}\n\n\t/**\n\t * Destroy the provider and cleanup resources.\n\t */\n\tpublic destroy(): void {\n\t\tthis.disconnect();\n\t\tsuper.destroy();\n\t}\n\n\t/**\n\t * Disconnect the provider and allow reconnection later.\n\t */\n\tpublic disconnect(): void {\n\t\tthis.log( 'Disconnecting' );\n\n\t\tpollingManager.unregisterRoom( this.options.room );\n\t\tthis.emitStatus( { status: 'disconnected' } );\n\t}\n\n\t/**\n\t * Emit connection status, passing the full object through so that\n\t * additional fields (e.g. `willAutoRetryInMs`) are preserved for consumers.\n\t *\n\t * @param connectionStatus The connection status object\n\t */\n\tprotected emitStatus = ( connectionStatus: ConnectionStatus ): void => {\n\t\tconst { status } = connectionStatus;\n\t\tconst error =\n\t\t\tstatus === 'disconnected' ? connectionStatus.error : undefined;\n\n\t\tif ( this.status === status && ! error ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Only emit 'connecting' status if transitioning from 'disconnected'.\n\t\tif ( status === 'connecting' && this.status !== 'disconnected' ) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.log( 'Status change', { status, error } );\n\n\t\t// ObservableV2 expects arguments as an array\n\t\tthis.status = status;\n\t\tthis.emit( 'status', [ connectionStatus ] );\n\t};\n\n\t/**\n\t * Log debug messages if debugging is enabled.\n\t *\n\t * @param message    The debug message\n\t * @param debug      Additional debug information\n\t * @param errorLevel The console method to use for logging\n\t * @param force      Whether to force logging regardless of debug setting\n\t */\n\tprotected log = (\n\t\tmessage: string,\n\t\tdebug: object = {},\n\t\terrorLevel: 'log' | 'warn' | 'error' = 'log',\n\t\tforce = false\n\t): void => {\n\t\tif ( ! this.options.debug && ! force ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// eslint-disable-next-line no-console\n\t\tconst logFn = console[ errorLevel ] || console.log;\n\n\t\tlogFn( `[${ this.constructor.name }]: ${ message }`, {\n\t\t\troom: this.options.room,\n\t\t\t...debug,\n\t\t} );\n\t};\n\n\t/**\n\t * Handle synchronization events from the polling manager.\n\t */\n\tprotected onSync = (): void => {\n\t\tif ( ! this.synced ) {\n\t\t\tthis.synced = true;\n\t\t\tthis.log( 'Synced' );\n\t\t}\n\t};\n}\n\n/**\n * Create a provider creator function for the HttpPollingProvider\n */\nexport function createHttpPollingProvider(): ProviderCreator {\n\treturn async ( {\n\t\tawareness,\n\t\tobjectType,\n\t\tobjectId,\n\t\tydoc,\n\t} ): Promise< ProviderCreatorResult > => {\n\t\t// Generate room name from objectType and objectId\n\t\tconst room = objectId ? `${ objectType }:${ objectId }` : objectType;\n\t\tconst provider = new HttpPollingProvider( {\n\t\t\tawareness,\n\t\t\t// debug: true,\n\t\t\troom,\n\t\t\tydoc,\n\t\t} );\n\n\t\treturn {\n\t\t\tdestroy: () => provider.destroy(),\n\t\t\t// Adapter: ObservableV2.on is compatible with ProviderOn\n\t\t\t// The callback receives data as the first parameter\n\t\t\ton: ( event, callback ) => {\n\t\t\t\tprovider.on( event, callback );\n\t\t\t},\n\t\t};\n\t};\n}\n"],
  "mappings": ";AAIA,SAAS,oBAAoB;AAC7B,SAAS,iBAAiB;AAU1B,SAAS,sBAAsB;AAqB/B,IAAM,sBAAN,cAAkC,aAAkC;AAAA,EAK5D,YAAuB,SAA2B;AACxD,UAAM;AADuB;AAE7B,SAAK,IAAK,gBAAgB,EAAE,MAAM,QAAQ,KAAK,CAAE;AAEjD,SAAK,YAAY,QAAQ,aAAa,IAAI,UAAW,QAAQ,IAAK;AAClE,SAAK,QAAQ;AAAA,EACd;AAAA,EAVU;AAAA,EACA,SAAuC;AAAA,EACvC,SAAS;AAAA;AAAA;AAAA;AAAA,EAaZ,UAAgB;AACtB,SAAK,IAAK,YAAa;AAEvB,mBAAe,aAAc;AAAA,MAC5B,MAAM,KAAK,QAAQ;AAAA,MACnB,KAAK,KAAK,QAAQ;AAAA,MAClB,WAAW,KAAK;AAAA,MAChB,KAAK,KAAK;AAAA,MACV,gBAAgB,KAAK;AAAA,MACrB,QAAQ,KAAK;AAAA,IACd,CAAE;AAAA,EACH;AAAA;AAAA;AAAA;AAAA,EAKO,UAAgB;AACtB,SAAK,WAAW;AAChB,UAAM,QAAQ;AAAA,EACf;AAAA;AAAA;AAAA;AAAA,EAKO,aAAmB;AACzB,SAAK,IAAK,eAAgB;AAE1B,mBAAe,eAAgB,KAAK,QAAQ,IAAK;AACjD,SAAK,WAAY,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