1 |
|
2 |
|
3 | var __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 | };
|
12 | var __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 | };
|
39 | export var shims;
|
40 | (function (shims) {
|
41 | var services;
|
42 | (function (services) {
|
43 | |
44 |
|
45 |
|
46 |
|
47 | var CommManager = (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 |
|
57 |
|
58 |
|
59 | CommManager.prototype.init_kernel = function (jsServicesKernel) {
|
60 | this.kernel = jsServicesKernel;
|
61 | this.jsServicesKernel = jsServicesKernel;
|
62 | };
|
63 | |
64 |
|
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 , comm];
|
75 | });
|
76 | });
|
77 | };
|
78 | |
79 |
|
80 |
|
81 |
|
82 |
|
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 |
|
88 | var comm = new Comm(jsServicesComm);
|
89 | _this.register_comm(comm);
|
90 |
|
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 |
|
104 |
|
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 |
|
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 |
|
124 |
|
125 |
|
126 | var Comm = (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 |
|
135 |
|
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 |
|
146 |
|
147 |
|
148 | get: function () {
|
149 | return this.jsServicesComm.targetName;
|
150 | },
|
151 | enumerable: true,
|
152 | configurable: true
|
153 | });
|
154 | |
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
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 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
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 |
|
181 |
|
182 |
|
183 |
|
184 |
|
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 |
|
193 |
|
194 |
|
195 | Comm.prototype.on_msg = function (callback) {
|
196 | this.jsServicesComm.onMsg = callback.bind(this);
|
197 | };
|
198 | |
199 |
|
200 |
|
201 |
|
202 | Comm.prototype.on_close = function (callback) {
|
203 | this.jsServicesComm.onClose = callback.bind(this);
|
204 | };
|
205 | |
206 |
|
207 |
|
208 |
|
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 |
|
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 = {}));
|