##// END OF EJS Templates
refactor js callbacks...
MinRK -
Show More
@@ -244,12 +244,22 var IPython = (function (IPython) {
244 244 this.set_input_prompt('*');
245 245 this.element.addClass("running");
246 246 var callbacks = {
247 'execute_reply': $.proxy(this._handle_execute_reply, this),
248 'output': $.proxy(this.output_area.handle_output, this.output_area),
249 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area),
250 'set_next_input': $.proxy(this._handle_set_next_input, this),
251 'input_request': $.proxy(this._handle_input_request, this)
252 };
247 shell : {
248 reply : $.proxy(this._handle_execute_reply, this),
249 payload : {
250 set_next_input : $.proxy(this._handle_set_next_input, this),
251 page : function (payload) {
252 $([IPython.events]).trigger('open_with_text.Pager', payload);
253 }
254 }
255 },
256 iopub : {
257 output : $.proxy(this.output_area.handle_output, this.output_area),
258 clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area),
259 },
260 input : $.proxy(this._handle_input_request, this)
261 }
262
253 263 this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
254 264 };
255 265
@@ -257,8 +267,8 var IPython = (function (IPython) {
257 267 * @method _handle_execute_reply
258 268 * @private
259 269 */
260 CodeCell.prototype._handle_execute_reply = function (content) {
261 this.set_input_prompt(content.execution_count);
270 CodeCell.prototype._handle_execute_reply = function (msg) {
271 this.set_input_prompt(msg.content.execution_count);
262 272 this.element.removeClass("running");
263 273 $([IPython.events]).trigger('set_dirty.Notebook', {value: true});
264 274 }
@@ -267,8 +277,8 var IPython = (function (IPython) {
267 277 * @method _handle_set_next_input
268 278 * @private
269 279 */
270 CodeCell.prototype._handle_set_next_input = function (text) {
271 var data = {'cell': this, 'text': text}
280 CodeCell.prototype._handle_set_next_input = function (payload) {
281 var data = {'cell': this, 'text': payload.text}
272 282 $([IPython.events]).trigger('set_next_input.Notebook', data);
273 283 }
274 284
@@ -276,8 +286,8 var IPython = (function (IPython) {
276 286 * @method _handle_input_request
277 287 * @private
278 288 */
279 CodeCell.prototype._handle_input_request = function (content) {
280 this.output_area.append_raw_input(content);
289 CodeCell.prototype._handle_input_request = function (msg) {
290 this.output_area.append_raw_input(msg);
281 291 }
282 292
283 293
@@ -438,4 +448,4 var IPython = (function (IPython) {
438 448 IPython.CodeCell = CodeCell;
439 449
440 450 return IPython;
441 }(IPython)); No newline at end of file
451 }(IPython));
@@ -150,16 +150,17 var IPython = (function (IPython) {
150 150 matched_text: ""
151 151 })
152 152 } else {
153 var callbacks = {
154 'complete_reply': $.proxy(this.finish_completing, this)
155 };
153 var callbacks = { shell : {
154 reply: $.proxy(this.finish_completing, this)
155 }};
156 156 this.cell.kernel.complete(line, cur.ch, callbacks);
157 157 }
158 158 };
159 159
160 Completer.prototype.finish_completing = function (content) {
160 Completer.prototype.finish_completing = function (msg) {
161 161 // let's build a function that wrap all that stuff into what is needed
162 162 // for the new completer:
163 var content = msg.content;
163 164 var matched_text = content.matched_text;
164 165 var matches = content.matches;
165 166
@@ -231,9 +231,10 var IPython = (function (IPython) {
231 231 };
232 232
233 233
234 OutputArea.prototype.handle_output = function (msg_type, content) {
234 OutputArea.prototype.handle_output = function (msg) {
235 235 var json = {};
236 json.output_type = msg_type;
236 var msg_type = json.output_type = msg.header.msg_type;
237 var content = msg.content;
237 238 if (msg_type === "stream") {
238 239 json.text = content.data;
239 240 json.stream = content.name;
@@ -564,9 +565,10 var IPython = (function (IPython) {
564 565 element.append(toinsert);
565 566 };
566 567
567 OutputArea.prototype.append_raw_input = function (content) {
568 OutputArea.prototype.append_raw_input = function (msg) {
568 569 var that = this;
569 570 this.expand();
571 var content = msg.content;
570 572 var area = this.create_output_area();
571 573
572 574 // disable any other raw_inputs, if they are left around
@@ -618,8 +620,8 var IPython = (function (IPython) {
618 620 }
619 621
620 622
621 OutputArea.prototype.handle_clear_output = function (content) {
622 this.clear_output(content.wait);
623 OutputArea.prototype.handle_clear_output = function (msg) {
624 this.clear_output(msg.content.wait);
623 625 };
624 626
625 627
@@ -222,12 +222,12 var IPython = (function (IPython) {
222 222
223 223
224 224 Tooltip.prototype._request_tooltip = function (cell, line) {
225 var callbacks = {
226 'object_info_reply': $.proxy(this._show, this)
227 }
225 var callbacks = { shell : {
226 reply : $.proxy(this._show, this)
227 }};
228 228 var oir_token = this.extract_oir_token(line);
229 229 var msg_id = cell.kernel.object_info_request(oir_token, callbacks);
230 }
230 };
231 231
232 232 // make an imediate completion request
233 233 Tooltip.prototype.request = function (cell, hide_if_no_docstring) {
@@ -301,7 +301,8 var IPython = (function (IPython) {
301 301 Tooltip.prototype._show = function (reply) {
302 302 // move the bubble if it is not hidden
303 303 // otherwise fade it
304 this.name = reply.name;
304 var content = reply.content;
305 this.name = content.name;
305 306
306 307 // do some math to have the tooltip arrow on more or less on left or right
307 308 // width of the editor
@@ -334,20 +335,20 var IPython = (function (IPython) {
334 335 });
335 336
336 337 // build docstring
337 var defstring = reply.call_def;
338 var defstring = content.call_def;
338 339 if (defstring == null) {
339 defstring = reply.init_definition;
340 defstring = content.init_definition;
340 341 }
341 342 if (defstring == null) {
342 defstring = reply.definition;
343 defstring = content.definition;
343 344 }
344 345
345 var docstring = reply.call_docstring;
346 var docstring = content.call_docstring;
346 347 if (docstring == null) {
347 docstring = reply.init_docstring;
348 docstring = content.init_docstring;
348 349 }
349 350 if (docstring == null) {
350 docstring = reply.docstring;
351 docstring = content.docstring;
351 352 }
352 353
353 354 if (docstring == null) {
@@ -239,7 +239,7 var IPython = (function (IPython) {
239 239 this.shell_channel.send(JSON.stringify(msg));
240 240 this.set_callbacks_for_msg(msg.header.msg_id, callbacks);
241 241 return msg.header.msg_id;
242 }
242 };
243 243
244 244 /**
245 245 * Get info on object asynchronoulsy
@@ -340,7 +340,7 var IPython = (function (IPython) {
340 340 allow_stdin : false
341 341 };
342 342 callbacks = callbacks || {};
343 if (callbacks.input_request !== undefined) {
343 if (callbacks.input !== undefined) {
344 344 content.allow_stdin = true;
345 345 }
346 346 $.extend(true, content, options);
@@ -431,11 +431,22 var IPython = (function (IPython) {
431 431 delete this._msg_callbacks[msg_id];
432 432 }
433 433 };
434
435
434
435 /* Set callbacks for a particular message.
436 * Callbacks should be a struct of the following form:
437 * shell : {
438 *
439 * }
440
441 */
436 442 Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) {
437 443 if (callbacks) {
438 this._msg_callbacks[msg_id] = callbacks;
444 // shallow-copy mapping, because we will modify it at the top level
445 var cbcopy = this._msg_callbacks[msg_id] = {};
446 cbcopy.shell = callbacks.shell;
447 cbcopy.iopub = callbacks.iopub;
448 cbcopy.input = callbacks.input;
449 this._msg_callbacks[msg_id] = cbcopy;
439 450 }
440 451 };
441 452
@@ -443,37 +454,40 var IPython = (function (IPython) {
443 454 Kernel.prototype._handle_shell_reply = function (e) {
444 455 var reply = $.parseJSON(e.data);
445 456 $([IPython.events]).trigger('shell_reply.Kernel', {kernel: this, reply:reply});
446 var header = reply.header;
447 457 var content = reply.content;
448 458 var metadata = reply.metadata;
449 var msg_type = header.msg_type;
450 var callbacks = this.get_callbacks_for_msg(reply.parent_header.msg_id);
451 if (callbacks !== undefined) {
452 var cb = callbacks[msg_type];
453 if (cb !== undefined) {
454 cb(content, metadata);
455 }
459 var parent_id = reply.parent_header.msg_id;
460 var callbacks = this.get_callbacks_for_msg(parent_id);
461 if (!callbacks || !callbacks.shell) {
462 return;
456 463 }
457
458 if (content.payload !== undefined) {
459 var payload = content.payload || [];
460 this._handle_payload(callbacks, payload);
464 var shell_callbacks = callbacks.shell;
465
466 // clear callbacks on shell
467 delete callbacks.shell;
468 delete callbacks.input;
469 if (!callbacks.iopub) {
470 this.clear_callbacks_for_msg(parent_id);
471 }
472
473 if (shell_callbacks.reply !== undefined) {
474 shell_callbacks.reply(reply);
475 }
476 if (content.payload && shell_callbacks.payload) {
477 this._handle_payloads(content.payload, shell_callbacks.payload, reply);
461 478 }
462 479 };
463 480
464 481
465 Kernel.prototype._handle_payload = function (callbacks, payload) {
466 var l = payload.length;
482 Kernel.prototype._handle_payloads = function (payloads, payload_callbacks, msg) {
483 var l = payloads.length;
467 484 // Payloads are handled by triggering events because we don't want the Kernel
468 485 // to depend on the Notebook or Pager classes.
469 486 for (var i=0; i<l; i++) {
470 if (payload[i].source === 'page') {
471 var data = {'text':payload[i].text};
472 $([IPython.events]).trigger('open_with_text.Pager', data);
473 } else if (payload[i].source === 'set_next_input') {
474 if (callbacks.set_next_input !== undefined) {
475 callbacks.set_next_input(payload[i].text);
476 }
487 var payload = payloads[i];
488 var callback = payload_callbacks[payload.source];
489 if (callback) {
490 callback(payload, msg);
477 491 }
478 492 }
479 493 };
@@ -483,6 +497,17 var IPython = (function (IPython) {
483 497 if (execution_state === 'busy') {
484 498 $([IPython.events]).trigger('status_busy.Kernel', {kernel: this});
485 499 } else if (execution_state === 'idle') {
500 // clear callbacks
501 var parent_id = msg.parent_header.msg_id;
502 var callbacks = this.get_callbacks_for_msg(parent_id);
503 if (callbacks !== undefined) {
504 delete callbacks.iopub;
505 delete callbacks.input;
506 if (!callbacks.shell) {
507 this.clear_callbacks_for_msg(parent_id);
508 }
509 }
510
486 511 $([IPython.events]).trigger('status_idle.Kernel', {kernel: this});
487 512 } else if (execution_state === 'restarting') {
488 513 // autorestarting is distinct from restarting,
@@ -501,12 +526,12 var IPython = (function (IPython) {
501 526 // handle clear_output message
502 527 Kernel.prototype._handle_clear_output = function (msg) {
503 528 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
504 if (callbacks === undefined) {
529 if (!callbacks || !callbacks.iopub) {
505 530 return;
506 531 }
507 var callback = callbacks['clear_output'];
508 if (callback !== undefined) {
509 callback(msg.content, msg.metadata);
532 var callback = callbacks.clear_output;
533 if (callback) {
534 callback(msg);
510 535 }
511 536 };
512 537
@@ -514,12 +539,12 var IPython = (function (IPython) {
514 539 // handle an output message (pyout, display_data, etc.)
515 540 Kernel.prototype._handle_output_message = function (msg) {
516 541 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
517 if (callbacks === undefined) {
542 if (!callbacks || !callbacks.iopub) {
518 543 return;
519 544 }
520 var callback = callbacks['output'];
521 if (callback !== undefined) {
522 callback(msg.header.msg_type, msg.content, msg.metadata);
545 var callback = callbacks.iopub.output;
546 if (callback) {
547 callback(msg);
523 548 }
524 549 };
525 550
@@ -546,10 +571,9 var IPython = (function (IPython) {
546 571 return;
547 572 }
548 573 var callbacks = this.get_callbacks_for_msg(request.parent_header.msg_id);
549 if (callbacks !== undefined) {
550 var cb = callbacks[msg_type];
551 if (cb !== undefined) {
552 cb(content, metadata);
574 if (callbacks) {
575 if (callbacks.input) {
576 callbacks.input(request);
553 577 }
554 578 }
555 579 };
General Comments 0
You need to be logged in to leave comments. Login now