import { PromiseDelegate } from '@lumino/coreutils' ; import { Datastore , IServerAdapter , Schema } from '@lumino/datastore' ; import { WSAdapterMessages } from './messages' ; import { WSConnection } from './wsbase' ; /**
 * A websocket based adapter for a datastore.
 */ export class WSAdapter extends WSConnection < WSAdapterMessages . IMessage , WSAdapterMessages . IMessage > implements IServerAdapter { param wsFactory constructor ( wsFactory ) { super ( wsFactory ) ; this . _delegates = new Map < string , PromiseDelegate < WSAdapterMessages . IReplyMessage > > ( ) ; } param schemas returns async createStore ( schemas ) { let storeId = await this . _createStoreId ( ) ; let datastore = Datastore . create ( { id : storeId , schemas , adapter : this } ) ; let fetchMsg = WSAdapterMessages . createHistoryRequestMessage ( ) ; let historyMsg = await this . _requestMessageReply ( fetchMsg ) ; if ( this . onRemoteTransaction ) { for ( let t of historyMsg . content . history . transactions ) { this . onRemoteTransaction ( t ) ; } } return datastore ; } param transaction broadcast ( transaction ) { let msg = WSAdapterMessages . createTransactionBroadcastMessage ( [ transaction ] ) ; void this . _requestMessageReply ( msg ) ; } param id returns async undo ( id ) { let msg = WSAdapterMessages . createUndoRequestMessage ( id ) ; let reply = await this . _requestMessageReply ( msg ) ; this . _handleUndo ( reply . content . transaction ) ; } param id returns async redo ( id ) { let msg = WSAdapterMessages . createRedoRequestMessage ( id ) ; let reply = await this . _requestMessageReply ( msg ) ; this . _handleRedo ( reply . content . transaction ) ; } /**
     * A callback for when a remote transaction is received by the server adapter.
     */ get onRemoteTransaction ( ) : ( ( transaction : Datastore . Transaction ) => void ) | null { return this . _onRemoteTransaction ; } set onRemoteTransaction ( value ) { this . _onRemoteTransaction = value ; } /**
     * A callback for when an undo is received by the server adapter.
     */ get onUndo ( ) : ( ( transaction : Datastore . Transaction ) => void ) | null { return this . _onUndo ; } set onUndo ( value ) { this . _onUndo = value ; } /**
     * A callback for when a redo is received by the server adapter.
     */ get onRedo ( ) : ( ( transaction : Datastore . Transaction ) => void ) | null { return this . _onRedo ; } set onRedo ( value ) { this . _onRedo = value ; } /**
     * Dispose of the resources held by the adapter.
     */ dispose ( ) { this . _onRemoteTransaction = null ; this . _onUndo = null ; this . _onRedo = null ; this . _delegates . clear ( ) ; super . dispose ( ) ; } param msg returns protected handleMessage ( msg ) { if ( WSAdapterMessages . isReply ( msg ) ) { let delegate = this . _delegates . get ( msg . parentId ! ) ; if ( delegate ) { delegate . resolve ( msg ) ; return true ; } } if ( msg . msgType === 'transaction-broadcast' ) { this . _handleTransactions ( msg . content . transactions ) ; return true ; } if ( msg . msgType === 'undo-reply' ) { this . _handleUndo ( msg . content . transaction ) ; return true ; } if ( msg . msgType === 'redo-reply' ) { this . _handleRedo ( msg . content . transaction ) ; return true ; } return false ; } returns private async _createStoreId ( ) { await this . ready ; let msg = WSAdapterMessages . createStoreIdRequestMessage ( ) ; let reply = await this . _requestMessageReply ( msg ) ; return reply . content . storeId ; } param transaction private _handleUndo ( transaction ) { if ( this . onUndo ) { this . onUndo ( transaction ) ; } } param transaction private _handleRedo ( transaction ) { if ( this . onRedo ) { this . onRedo ( transaction ) ; } } param transactions private _handleTransactions ( transactions ) { if ( this . isDisposed ) { return ; } if ( this . onRemoteTransaction ) { for ( let t of transactions ) { this . onRemoteTransaction ( t ) ; } } } param msg returns private _requestMessageReply ( msg ) ; private _requestMessageReply ( msg ) ; private _requestMessageReply ( msg ) ; private _requestMessageReply ( msg ) ; private _requestMessageReply ( msg ) ; private _requestMessageReply ( msg ) { let delegate = new PromiseDelegate < WSAdapterMessages . IReplyMessage > ( ) ; this . _delegates . set ( msg . msgId , delegate ) ; let promise = delegate . promise . then ( reply => { this . _delegates . delete ( msg . msgId ) ; return reply ; } ) ; this . sendMessage ( msg ) ; return promise ; } private _delegates : Map < string , PromiseDelegate < WSAdapterMessages . IReplyMessage > > ; private _onRemoteTransaction : ( ( transaction : Datastore . Transaction ) => void ) | null = null ; private _onUndo : ( ( transaction : Datastore . Transaction ) => void ) | null = null ; private _onRedo : ( ( transaction : Datastore . Transaction ) => void ) | null = null ; }