UNPKG

7.77 kBJavaScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3export var shims;
4(function (shims) {
5 let services;
6 (function (services) {
7 /**
8 * Public constructor
9 * @param jsServicesKernel - @jupyterlab/services Kernel.IKernel instance
10 */
11 class CommManager {
12 constructor(jsServicesKernel) {
13 this.targets = Object.create(null);
14 this.comms = Object.create(null);
15 this.init_kernel(jsServicesKernel);
16 }
17 /**
18 * Hookup kernel events.
19 * @param {Kernel.IKernel} jsServicesKernel - @jupyterlab/services Kernel.IKernel instance
20 */
21 init_kernel(jsServicesKernel) {
22 this.kernel = jsServicesKernel; // These aren't really the same.
23 this.jsServicesKernel = jsServicesKernel;
24 }
25 /**
26 * Creates a new connected comm
27 */
28 async new_comm(target_name, data, callbacks, metadata, comm_id, buffers) {
29 const c = this.jsServicesKernel.createComm(target_name, comm_id);
30 const comm = new Comm(c);
31 this.register_comm(comm);
32 comm.open(data, callbacks, metadata, buffers);
33 return comm;
34 }
35 /**
36 * Register a comm target
37 * @param {string} target_name
38 * @param {(Comm, object) => void} f - callback that is called when the
39 * comm is made. Signature of f(comm, msg).
40 */
41 register_target(target_name, f) {
42 const handle = this.jsServicesKernel.registerCommTarget(target_name, (jsServicesComm, msg) => {
43 // Create the comm.
44 const comm = new Comm(jsServicesComm);
45 this.register_comm(comm);
46 // Call the callback for the comm.
47 try {
48 return f(comm, msg);
49 }
50 catch (e) {
51 comm.close();
52 console.error(e);
53 console.error(new Error('Exception opening new comm'));
54 }
55 });
56 this.targets[target_name] = handle;
57 }
58 /**
59 * Unregisters a comm target
60 * @param {string} target_name
61 */
62 unregister_target(target_name, f) {
63 const handle = this.targets[target_name];
64 handle.dispose();
65 delete this.targets[target_name];
66 }
67 /**
68 * Register a comm in the mapping
69 */
70 register_comm(comm) {
71 this.comms[comm.comm_id] = Promise.resolve(comm);
72 comm.kernel = this.kernel;
73 return comm.comm_id;
74 }
75 }
76 services.CommManager = CommManager;
77 /**
78 * Public constructor
79 * @param {IComm} jsServicesComm - @jupyterlab/services IComm instance
80 */
81 class Comm {
82 constructor(jsServicesComm) {
83 this.jsServicesComm = jsServicesComm;
84 }
85 /**
86 * Comm id
87 * @return {string}
88 */
89 get comm_id() {
90 return this.jsServicesComm.commId;
91 }
92 /**
93 * Target name
94 * @return {string}
95 */
96 get target_name() {
97 return this.jsServicesComm.targetName;
98 }
99 /**
100 * Opens a sibling comm in the backend
101 * @param data
102 * @param callbacks
103 * @param metadata
104 * @return msg id
105 */
106 open(data, callbacks, metadata, buffers) {
107 const future = this.jsServicesComm.open(data, metadata, buffers);
108 this._hookupCallbacks(future, callbacks);
109 return future.msg.header.msg_id;
110 }
111 /**
112 * Sends a message to the sibling comm in the backend
113 * @param data
114 * @param callbacks
115 * @param metadata
116 * @param buffers
117 * @return message id
118 */
119 send(data, callbacks, metadata, buffers) {
120 const future = this.jsServicesComm.send(data, metadata, buffers);
121 this._hookupCallbacks(future, callbacks);
122 return future.msg.header.msg_id;
123 }
124 /**
125 * Closes the sibling comm in the backend
126 * @param data
127 * @param callbacks
128 * @param metadata
129 * @return msg id
130 */
131 close(data, callbacks, metadata, buffers) {
132 const future = this.jsServicesComm.close(data, metadata, buffers);
133 this._hookupCallbacks(future, callbacks);
134 return future.msg.header.msg_id;
135 }
136 /**
137 * Register a message handler
138 * @param callback, which is given a message
139 */
140 on_msg(callback) {
141 this.jsServicesComm.onMsg = callback.bind(this);
142 }
143 /**
144 * Register a handler for when the comm is closed by the backend
145 * @param callback, which is given a message
146 */
147 on_close(callback) {
148 this.jsServicesComm.onClose = callback.bind(this);
149 }
150 /**
151 * Hooks callback object up with @jupyterlab/services IKernelFuture
152 * @param @jupyterlab/services IKernelFuture instance
153 * @param callbacks
154 */
155 _hookupCallbacks(future, callbacks) {
156 if (callbacks) {
157 future.onReply = function (msg) {
158 if (callbacks.shell && callbacks.shell.reply) {
159 callbacks.shell.reply(msg);
160 }
161 };
162 future.onStdin = function (msg) {
163 if (callbacks.input) {
164 callbacks.input(msg);
165 }
166 };
167 future.onIOPub = function (msg) {
168 if (callbacks.iopub) {
169 if (callbacks.iopub.status && msg.header.msg_type === 'status') {
170 callbacks.iopub.status(msg);
171 }
172 else if (callbacks.iopub.clear_output &&
173 msg.header.msg_type === 'clear_output') {
174 callbacks.iopub.clear_output(msg);
175 }
176 else if (callbacks.iopub.output) {
177 switch (msg.header.msg_type) {
178 case 'display_data':
179 case 'execute_result':
180 case 'stream':
181 case 'error':
182 callbacks.iopub.output(msg);
183 break;
184 default:
185 break;
186 }
187 }
188 }
189 };
190 }
191 }
192 }
193 services.Comm = Comm;
194 })(services = shims.services || (shims.services = {}));
195})(shims || (shims = {}));
196//# sourceMappingURL=services-shim.js.map
\No newline at end of file