UNPKG

11.6 kBJavaScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5 return new (P || (P = Promise))(function (resolve, reject) {
6 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
7 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
9 step((generator = generator.apply(thisArg, _arguments || [])).next());
10 });
11};
12var __generator = (this && this.__generator) || function (thisArg, body) {
13 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
14 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
15 function verb(n) { return function (v) { return step([n, v]); }; }
16 function step(op) {
17 if (f) throw new TypeError("Generator is already executing.");
18 while (_) try {
19 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
20 if (y = 0, t) op = [op[0] & 2, t.value];
21 switch (op[0]) {
22 case 0: case 1: t = op; break;
23 case 4: _.label++; return { value: op[1], done: false };
24 case 5: _.label++; y = op[1]; op = [0]; continue;
25 case 7: op = _.ops.pop(); _.trys.pop(); continue;
26 default:
27 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
28 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
29 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
30 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
31 if (t[2]) _.ops.pop();
32 _.trys.pop(); continue;
33 }
34 op = body.call(thisArg, _);
35 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
36 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
37 }
38};
39export var shims;
40(function (shims) {
41 var services;
42 (function (services) {
43 /**
44 * Public constructor
45 * @param jsServicesKernel - @jupyterlab/services Kernel.IKernel instance
46 */
47 var CommManager = /** @class */ (function () {
48 function CommManager(jsServicesKernel) {
49 this.targets = Object.create(null);
50 this.comms = Object.create(null);
51 this.kernel = null;
52 this.jsServicesKernel = null;
53 this.init_kernel(jsServicesKernel);
54 }
55 /**
56 * Hookup kernel events.
57 * @param {Kernel.IKernel} jsServicesKernel - @jupyterlab/services Kernel.IKernel instance
58 */
59 CommManager.prototype.init_kernel = function (jsServicesKernel) {
60 this.kernel = jsServicesKernel; // These aren't really the same.
61 this.jsServicesKernel = jsServicesKernel;
62 };
63 /**
64 * Creates a new connected comm
65 */
66 CommManager.prototype.new_comm = function (target_name, data, callbacks, metadata, comm_id, buffers) {
67 return __awaiter(this, void 0, void 0, function () {
68 var c, comm;
69 return __generator(this, function (_a) {
70 c = this.jsServicesKernel.createComm(target_name, comm_id);
71 comm = new Comm(c);
72 this.register_comm(comm);
73 comm.open(data, callbacks, metadata, buffers);
74 return [2 /*return*/, comm];
75 });
76 });
77 };
78 /**
79 * Register a comm target
80 * @param {string} target_name
81 * @param {(Comm, object) => void} f - callback that is called when the
82 * comm is made. Signature of f(comm, msg).
83 */
84 CommManager.prototype.register_target = function (target_name, f) {
85 var _this = this;
86 var handle = this.jsServicesKernel.registerCommTarget(target_name, function (jsServicesComm, msg) {
87 // Create the comm.
88 var comm = new Comm(jsServicesComm);
89 _this.register_comm(comm);
90 // Call the callback for the comm.
91 try {
92 return f(comm, msg);
93 }
94 catch (e) {
95 comm.close();
96 console.error(e);
97 console.error(new Error('Exception opening new comm'));
98 }
99 });
100 this.targets[target_name] = handle;
101 };
102 /**
103 * Unregisters a comm target
104 * @param {string} target_name
105 */
106 CommManager.prototype.unregister_target = function (target_name, f) {
107 var handle = this.targets[target_name];
108 handle.dispose();
109 delete this.targets[target_name];
110 };
111 /**
112 * Register a comm in the mapping
113 */
114 CommManager.prototype.register_comm = function (comm) {
115 this.comms[comm.comm_id] = Promise.resolve(comm);
116 comm.kernel = this.kernel;
117 return comm.comm_id;
118 };
119 return CommManager;
120 }());
121 services.CommManager = CommManager;
122 /**
123 * Public constructor
124 * @param {IComm} jsServicesComm - @jupyterlab/services IComm instance
125 */
126 var Comm = /** @class */ (function () {
127 function Comm(jsServicesComm) {
128 this.jsServicesComm = null;
129 this.kernel = null;
130 this.jsServicesComm = jsServicesComm;
131 }
132 Object.defineProperty(Comm.prototype, "comm_id", {
133 /**
134 * Comm id
135 * @return {string}
136 */
137 get: function () {
138 return this.jsServicesComm.commId;
139 },
140 enumerable: true,
141 configurable: true
142 });
143 Object.defineProperty(Comm.prototype, "target_name", {
144 /**
145 * Target name
146 * @return {string}
147 */
148 get: function () {
149 return this.jsServicesComm.targetName;
150 },
151 enumerable: true,
152 configurable: true
153 });
154 /**
155 * Opens a sibling comm in the backend
156 * @param data
157 * @param callbacks
158 * @param metadata
159 * @return msg id
160 */
161 Comm.prototype.open = function (data, callbacks, metadata, buffers) {
162 var future = this.jsServicesComm.open(data, metadata, buffers);
163 this._hookupCallbacks(future, callbacks);
164 return future.msg.header.msg_id;
165 };
166 /**
167 * Sends a message to the sibling comm in the backend
168 * @param data
169 * @param callbacks
170 * @param metadata
171 * @param buffers
172 * @return message id
173 */
174 Comm.prototype.send = function (data, callbacks, metadata, buffers) {
175 var future = this.jsServicesComm.send(data, metadata, buffers);
176 this._hookupCallbacks(future, callbacks);
177 return future.msg.header.msg_id;
178 };
179 /**
180 * Closes the sibling comm in the backend
181 * @param data
182 * @param callbacks
183 * @param metadata
184 * @return msg id
185 */
186 Comm.prototype.close = function (data, callbacks, metadata, buffers) {
187 var future = this.jsServicesComm.close(data, metadata, buffers);
188 this._hookupCallbacks(future, callbacks);
189 return future.msg.header.msg_id;
190 };
191 /**
192 * Register a message handler
193 * @param callback, which is given a message
194 */
195 Comm.prototype.on_msg = function (callback) {
196 this.jsServicesComm.onMsg = callback.bind(this);
197 };
198 /**
199 * Register a handler for when the comm is closed by the backend
200 * @param callback, which is given a message
201 */
202 Comm.prototype.on_close = function (callback) {
203 this.jsServicesComm.onClose = callback.bind(this);
204 };
205 /**
206 * Hooks callback object up with @jupyterlab/services IKernelFuture
207 * @param @jupyterlab/services IKernelFuture instance
208 * @param callbacks
209 */
210 Comm.prototype._hookupCallbacks = function (future, callbacks) {
211 if (callbacks) {
212 future.onReply = function (msg) {
213 if (callbacks.shell && callbacks.shell.reply) {
214 callbacks.shell.reply(msg);
215 }
216 // TODO: Handle payloads. See https://github.com/jupyter/notebook/blob/master/notebook/static/services/kernels/kernel.js#L923-L947
217 };
218 future.onStdin = function (msg) {
219 if (callbacks.input) {
220 callbacks.input(msg);
221 }
222 };
223 future.onIOPub = function (msg) {
224 if (callbacks.iopub) {
225 if (callbacks.iopub.status && msg.header.msg_type === 'status') {
226 callbacks.iopub.status(msg);
227 }
228 else if (callbacks.iopub.clear_output && msg.header.msg_type === 'clear_output') {
229 callbacks.iopub.clear_output(msg);
230 }
231 else if (callbacks.iopub.output) {
232 switch (msg.header.msg_type) {
233 case 'display_data':
234 case 'execute_result':
235 case 'stream':
236 case 'error':
237 callbacks.iopub.output(msg);
238 break;
239 default: break;
240 }
241 }
242 }
243 };
244 }
245 };
246 return Comm;
247 }());
248 services.Comm = Comm;
249 })(services = shims.services || (shims.services = {}));
250})(shims || (shims = {}));