"use strict";(self.webpackChunk_jupyter_collaboration_extension=self.webpackChunk_jupyter_collaboration_extension||[]).push([[933,8],{2008:(e,t,s)=>{s.r(t),s.d(t,{ICollaborativeDrive:()=>_,WebSocketProvider:()=>h,YDrive:()=>d});var o=s(9474),r=s(3020),n=s(9324),i=s(7930),a=s(4901),c=s(7099);class h{constructor(e){this._onConnectionClosed=e=>{1003===e.code&&(console.error("Document provider closed:",e.reason),(0,n.showErrorMessage)(this._trans.__("Document session error"),e.reason,[n.Dialog.okButton()]),this._sharedModel.dispose())},this._onSync=e=>{var t;e&&(this._ready.resolve(),null===(t=this._yWebsocketProvider)||void 0===t||t.off("sync",this._onSync))},this._ready=new i.PromiseDelegate,this._isDisposed=!1,this._path=e.path,this._contentType=e.contentType,this._format=e.format,this._serverUrl=e.url,this._sharedModel=e.model,this._awareness=e.model.awareness,this._yWebsocketProvider=null,this._trans=e.translator;const t=e.user;t.ready.then((()=>{this._onUserChanged(t)})).catch((e=>console.error(e))),t.userChanged.connect(this._onUserChanged,this),this._connect().catch((e=>console.warn(e)))}get isDisposed(){return this._isDisposed}get ready(){return this._ready.promise}dispose(){var e,t,s;this.isDisposed||(this._isDisposed=!0,null===(e=this._yWebsocketProvider)||void 0===e||e.off("connection-close",this._onConnectionClosed),null===(t=this._yWebsocketProvider)||void 0===t||t.off("sync",this._onSync),null===(s=this._yWebsocketProvider)||void 0===s||s.destroy(),a.Signal.clearData(this))}async _connect(){const e=await async function(e,t,s){const n=r.ServerConnection.makeSettings(),i=o.URLExt.join(n.baseUrl,"api/collaboration/session",encodeURIComponent(s)),a={method:"PUT",body:JSON.stringify({format:e,type:t})};let c;try{c=await r.ServerConnection.makeRequest(i,a,n)}catch(e){throw new r.ServerConnection.NetworkError(e)}let h=await c.text();if(h.length>0)try{h=JSON.parse(h)}catch(e){console.log("Not a JSON response body.",c)}if(!c.ok)throw new r.ServerConnection.ResponseError(c,h.message||h);return h}(this._format,this._contentType,this._path);this._yWebsocketProvider=new c.WebsocketProvider(this._serverUrl,`${e.format}:${e.type}:${e.fileId}`,this._sharedModel.ydoc,{disableBc:!0,params:{sessionId:e.sessionId},awareness:this._awareness}),this._yWebsocketProvider.on("sync",this._onSync),this._yWebsocketProvider.on("connection-close",this._onConnectionClosed)}_onUserChanged(e){this._awareness.setLocalStateField("user",e.identity)}}class d extends r.Drive{constructor(e,t){super({name:"RTC"}),this._onCreate=(e,t)=>{if("string"==typeof e.format)try{const s=new h({url:o.URLExt.join(this.serverSettings.wsUrl,"api/collaboration/room"),path:e.path,format:e.format,contentType:e.contentType,model:t,user:this._user,translator:this._trans}),r=`${e.format}:${e.contentType}:${e.path}`;this._providers.set(r,s),t.disposed.connect((()=>{const e=this._providers.get(r);e&&(e.dispose(),this._providers.delete(r))}))}catch(t){console.error(`Failed to open websocket connection for ${e.path}.\n:${t}`)}},this._user=e,this._trans=t,this._providers=new Map,this.sharedModelFactory=new l(this._onCreate)}dispose(){this.isDisposed||(this._providers.forEach((e=>e.dispose())),this._providers.clear(),super.dispose())}async get(e,t){if(t&&t.format&&t.type){const s=`${t.format}:${t.type}:${e}`,o=this._providers.get(s);if(o){const s=super.get(e,{...t,content:!1});return await o.ready,s}}return super.get(e,t)}async save(e,t={}){if(t.format&&t.type){const s=`${t.format}:${t.type}:${e}`;if(this._providers.get(s))return this.get(e,{...t,content:!1})}return super.save(e,t)}}class l{constructor(e){this._onCreate=e,this.collaborative=!0,this._documentFactories=new Map}registerDocumentFactory(e,t){if(this._documentFactories.has(e))throw new Error(`The content type ${e} already exists`);this._documentFactories.set(e,t)}createNew(e){if("string"==typeof e.format){if(e.collaborative&&this._documentFactories.has(e.contentType)){const t=this._documentFactories.get(e.contentType)(e);return this._onCreate(e,t),t}}else console.warn(`Only defined format are supported; got ${e.format}.`)}}const _=new i.Token("@jupyter/collaboration-extension:ICollaborativeDrive")}}]);