UNPKG

6.46 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Jupyter Development Team.
3// Distributed under the terms of the Modified BSD License.
4var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5 if (k2 === undefined) k2 = k;
6 var desc = Object.getOwnPropertyDescriptor(m, k);
7 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8 desc = { enumerable: true, get: function() { return m[k]; } };
9 }
10 Object.defineProperty(o, k2, desc);
11}) : (function(o, m, k, k2) {
12 if (k2 === undefined) k2 = k;
13 o[k2] = m[k];
14}));
15var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
16 Object.defineProperty(o, "default", { enumerable: true, value: v });
17}) : function(o, v) {
18 o["default"] = v;
19});
20var __importStar = (this && this.__importStar) || function (mod) {
21 if (mod && mod.__esModule) return mod;
22 var result = {};
23 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
24 __setModuleDefault(result, mod);
25 return result;
26};
27Object.defineProperty(exports, "__esModule", { value: true });
28exports.CommHandler = void 0;
29const disposable_1 = require("@lumino/disposable");
30const KernelMessage = __importStar(require("./messages"));
31/**
32 * Comm channel handler.
33 */
34class CommHandler extends disposable_1.DisposableDelegate {
35 /**
36 * Construct a new comm channel.
37 */
38 constructor(target, id, kernel, disposeCb) {
39 super(disposeCb);
40 this._target = '';
41 this._id = '';
42 this._id = id;
43 this._target = target;
44 this._kernel = kernel;
45 }
46 /**
47 * The unique id for the comm channel.
48 */
49 get commId() {
50 return this._id;
51 }
52 /**
53 * The target name for the comm channel.
54 */
55 get targetName() {
56 return this._target;
57 }
58 /**
59 * Get the callback for a comm close event.
60 *
61 * #### Notes
62 * This is called when the comm is closed from either the server or client.
63 *
64 * **See also:** [[ICommClose]], [[close]]
65 */
66 get onClose() {
67 return this._onClose;
68 }
69 /**
70 * Set the callback for a comm close event.
71 *
72 * #### Notes
73 * This is called when the comm is closed from either the server or client. If
74 * the function returns a promise, and the kernel was closed from the server,
75 * kernel message processing will pause until the returned promise is
76 * fulfilled.
77 *
78 * **See also:** [[close]]
79 */
80 set onClose(cb) {
81 this._onClose = cb;
82 }
83 /**
84 * Get the callback for a comm message received event.
85 */
86 get onMsg() {
87 return this._onMsg;
88 }
89 /**
90 * Set the callback for a comm message received event.
91 *
92 * #### Notes
93 * This is called when a comm message is received. If the function returns a
94 * promise, kernel message processing will pause until it is fulfilled.
95 */
96 set onMsg(cb) {
97 this._onMsg = cb;
98 }
99 /**
100 * Open a comm with optional data and metadata.
101 *
102 * #### Notes
103 * This sends a `comm_open` message to the server.
104 *
105 * **See also:** [[ICommOpen]]
106 */
107 open(data, metadata, buffers = []) {
108 if (this.isDisposed || this._kernel.isDisposed) {
109 throw new Error('Cannot open');
110 }
111 const msg = KernelMessage.createMessage({
112 msgType: 'comm_open',
113 channel: 'shell',
114 username: this._kernel.username,
115 session: this._kernel.clientId,
116 content: {
117 comm_id: this._id,
118 target_name: this._target,
119 data: data !== null && data !== void 0 ? data : {}
120 },
121 metadata,
122 buffers
123 });
124 return this._kernel.sendShellMessage(msg, false, true);
125 }
126 /**
127 * Send a `comm_msg` message to the kernel.
128 *
129 * #### Notes
130 * This is a no-op if the comm has been closed.
131 *
132 * **See also:** [[ICommMsg]]
133 */
134 send(data, metadata, buffers = [], disposeOnDone = true) {
135 if (this.isDisposed || this._kernel.isDisposed) {
136 throw new Error('Cannot send');
137 }
138 const msg = KernelMessage.createMessage({
139 msgType: 'comm_msg',
140 channel: 'shell',
141 username: this._kernel.username,
142 session: this._kernel.clientId,
143 content: {
144 comm_id: this._id,
145 data: data
146 },
147 metadata,
148 buffers
149 });
150 return this._kernel.sendShellMessage(msg, false, disposeOnDone);
151 }
152 /**
153 * Close the comm.
154 *
155 * #### Notes
156 * This will send a `comm_close` message to the kernel, and call the
157 * `onClose` callback if set.
158 *
159 * This is a no-op if the comm is already closed.
160 *
161 * **See also:** [[ICommClose]], [[onClose]]
162 */
163 close(data, metadata, buffers = []) {
164 if (this.isDisposed || this._kernel.isDisposed) {
165 throw new Error('Cannot close');
166 }
167 const msg = KernelMessage.createMessage({
168 msgType: 'comm_close',
169 channel: 'shell',
170 username: this._kernel.username,
171 session: this._kernel.clientId,
172 content: {
173 comm_id: this._id,
174 data: data !== null && data !== void 0 ? data : {}
175 },
176 metadata,
177 buffers
178 });
179 const future = this._kernel.sendShellMessage(msg, false, true);
180 const onClose = this._onClose;
181 if (onClose) {
182 const ioMsg = KernelMessage.createMessage({
183 msgType: 'comm_close',
184 channel: 'iopub',
185 username: this._kernel.username,
186 session: this._kernel.clientId,
187 content: {
188 comm_id: this._id,
189 data: data !== null && data !== void 0 ? data : {}
190 },
191 metadata,
192 buffers
193 });
194 // In the future, we may want to communicate back to the user the possible
195 // promise returned from onClose.
196 void onClose(ioMsg);
197 }
198 this.dispose();
199 return future;
200 }
201}
202exports.CommHandler = CommHandler;
203//# sourceMappingURL=comm.js.map
\No newline at end of file