/**
 * This module defines shims for @jupyterlab/services that allows you to use the
 * old comm API.  Use this, @jupyterlab/services, and the widget base manager to
 * embed live widgets in a context outside of the notebook.
 */ import { Kernel , KernelMessage } from '@jupyterlab/services' ; /**
 * Callbacks for services shim comms.
 */ export interface ICallbacks { shell ? : { [ key ] : ( msg : KernelMessage . IMessage ) => void ; } ; iopub ? : { [ key ] : ( msg : KernelMessage . IMessage ) => void ; } ; input ? : ( msg : KernelMessage . IMessage ) => void ; } export interface IClassicComm { return { string } comm_id : string ; return { string } target_name : string ; param data param callbacks param metadata param buffers return open ( data , callbacks , metadata ? , buffers ? ) : string ; param data param callbacks param metadata param buffers return send ( data , callbacks , metadata ? , buffers ? ) : string ; param data param callbacks param metadata param buffers return close ( data ? , callbacks ? , metadata ? , buffers ? ) : string ; param callback on_msg ( callback ) : void ; param callback on_close ( callback ) : void ; } export namespace shims { export namespace services { param jsServicesKernel jupyterlab export class CommManager { constructor ( jsServicesKernel ) { this . init_kernel ( jsServicesKernel ) ; } param { Kernel . IKernel } jsServicesKernel jupyterlab init_kernel ( jsServicesKernel ) { this . kernel = jsServicesKernel ; this . jsServicesKernel = jsServicesKernel ; } /**
             * Creates a new connected comm
             */ async new_comm ( target_name , data , callbacks , metadata , comm_id , buffers ? ) { const c = this . jsServicesKernel . createComm ( target_name , comm_id ) ; const comm = new Comm ( c ) ; this . register_comm ( comm ) ; comm . open ( data , callbacks , metadata , buffers ) ; return comm ; } param { string } target_name param { ( Comm , object ) => void } f register_target ( target_name , f ) { const handle = this . jsServicesKernel . registerCommTarget ( target_name , ( jsServicesComm , msg ) => { const comm = new Comm ( jsServicesComm ) ; this . register_comm ( comm ) ; try { return f ( comm , msg ) ; } catch ( e ) { comm . close ( ) ; console . error ( e ) ; console . error ( new Error ( 'Exception opening new comm' ) ) ; } } ) ; this . targets [ target_name ] = handle ; } param { string } target_name unregister_target ( target_name , f ) { const handle = this . targets [ target_name ] ; handle . dispose ( ) ; delete this . targets [ target_name ] ; } /**
             * Register a comm in the mapping
             */ register_comm ( comm ) { this . comms [ comm . comm_id ] = Promise . resolve ( comm ) ; comm . kernel = this . kernel ; return comm . comm_id ; } targets = Object . create ( null ) ; comms = Object . create ( null ) ; kernel : Kernel . IKernelConnection ; jsServicesKernel : Kernel . IKernelConnection ; } param { IComm } jsServicesComm jupyterlab export class Comm implements IClassicComm { constructor ( jsServicesComm ) { this . jsServicesComm = jsServicesComm ; } return { string } get comm_id ( ) : string { return this . jsServicesComm . commId ; } return { string } get target_name ( ) : string { return this . jsServicesComm . targetName ; } param data param callbacks param metadata return open ( data , callbacks , metadata ? , buffers ? ) { const future = this . jsServicesComm . open ( data , metadata , buffers ) ; this . _hookupCallbacks ( future , callbacks ) ; return future . msg . header . msg_id ; } param data param callbacks param metadata param buffers return send ( data , callbacks , metadata ? , buffers ? ) { const future = this . jsServicesComm . send ( data , metadata , buffers ) ; this . _hookupCallbacks ( future , callbacks ) ; return future . msg . header . msg_id ; } param data param callbacks param metadata return close ( data ? , callbacks ? , metadata ? , buffers ? ) { const future = this . jsServicesComm . close ( data , metadata , buffers ) ; this . _hookupCallbacks ( future , callbacks ) ; return future . msg . header . msg_id ; } param callback on_msg ( callback ) { this . jsServicesComm . onMsg = callback . bind ( this ) ; } param callback on_close ( callback ) { this . jsServicesComm . onClose = callback . bind ( this ) ; } param jupyterlab param callbacks _hookupCallbacks ( future , callbacks ) { if ( callbacks ) { future . onReply = function ( msg ) : void { if ( callbacks . shell && callbacks . shell . reply ) { callbacks . shell . reply ( msg ) ; } } ; future . onStdin = function ( msg ) : void { if ( callbacks . input ) { callbacks . input ( msg ) ; } } ; future . onIOPub = function ( msg ) : void { if ( callbacks . iopub ) { if ( callbacks . iopub . status && msg . header . msg_type === 'status' ) { callbacks . iopub . status ( msg ) ; } else if ( callbacks . iopub . clear_output && msg . header . msg_type === 'clear_output' ) { callbacks . iopub . clear_output ( msg ) ; } else if ( callbacks . iopub . output ) { switch ( msg . header . msg_type ) { case 'display_data' : case 'execute_result' : case 'stream' : case 'error' : callbacks . iopub . output ( msg ) ; break ; default : break ; } } } } ; } } jsServicesComm : Kernel . IComm ; kernel : Kernel . IKernelConnection ; } } }