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