##// END OF EJS Templates
Handle kernel messages synchronously...
Jason Grout -
Show More
@@ -117,6 +117,7 b' define(['
117 // don't return a comm, so that further .then() functions
117 // don't return a comm, so that further .then() functions
118 // get an undefined comm input
118 // get an undefined comm input
119 });
119 });
120 return this.comms[content.comm_id];
120 };
121 };
121
122
122 CommManager.prototype.comm_msg = function(msg) {
123 CommManager.prototype.comm_msg = function(msg) {
@@ -134,6 +135,7 b' define(['
134 }
135 }
135 return comm;
136 return comm;
136 });
137 });
138 return this.comms[content.comm_id];
137 };
139 };
138
140
139 //-----------------------------------------------------------------------
141 //-----------------------------------------------------------------------
@@ -855,22 +855,23 b' define(['
855 };
855 };
856
856
857 Kernel.prototype._handle_ws_message = function (e) {
857 Kernel.prototype._handle_ws_message = function (e) {
858 var that = this;
858 this._msg_queue = this._msg_queue.then(function() {
859 this._msg_queue = this._msg_queue.then(function() {
859 return serialize.deserialize(e.data);
860 return serialize.deserialize(e.data);
860 }).then($.proxy(this._finish_ws_message, this))
861 }).then(function(msg) {return that._finish_ws_message(msg);})
861 .catch(utils.reject("Couldn't process kernel message", true));
862 .catch(utils.reject("Couldn't process kernel message", true));
862 };
863 };
863
864
864 Kernel.prototype._finish_ws_message = function (msg) {
865 Kernel.prototype._finish_ws_message = function (msg) {
865 switch (msg.channel) {
866 switch (msg.channel) {
866 case 'shell':
867 case 'shell':
867 this._handle_shell_reply(msg);
868 return this._handle_shell_reply(msg);
868 break;
869 break;
869 case 'iopub':
870 case 'iopub':
870 this._handle_iopub_message(msg);
871 return this._handle_iopub_message(msg);
871 break;
872 break;
872 case 'stdin':
873 case 'stdin':
873 this._handle_input_request(msg);
874 return this._handle_input_request(msg);
874 break;
875 break;
875 default:
876 default:
876 console.error("unrecognized message channel", msg.channel, msg);
877 console.error("unrecognized message channel", msg.channel, msg);
@@ -879,10 +880,12 b' define(['
879
880
880 Kernel.prototype._handle_shell_reply = function (reply) {
881 Kernel.prototype._handle_shell_reply = function (reply) {
881 this.events.trigger('shell_reply.Kernel', {kernel: this, reply:reply});
882 this.events.trigger('shell_reply.Kernel', {kernel: this, reply:reply});
883 var that = this;
882 var content = reply.content;
884 var content = reply.content;
883 var metadata = reply.metadata;
885 var metadata = reply.metadata;
884 var parent_id = reply.parent_header.msg_id;
886 var parent_id = reply.parent_header.msg_id;
885 var callbacks = this.get_callbacks_for_msg(parent_id);
887 var callbacks = this.get_callbacks_for_msg(parent_id);
888 var promise = Promise.resolve();
886 if (!callbacks || !callbacks.shell) {
889 if (!callbacks || !callbacks.shell) {
887 return;
890 return;
888 }
891 }
@@ -892,17 +895,21 b' define(['
892 this._finish_shell(parent_id);
895 this._finish_shell(parent_id);
893
896
894 if (shell_callbacks.reply !== undefined) {
897 if (shell_callbacks.reply !== undefined) {
895 shell_callbacks.reply(reply);
898 promise = promise.then(function() {return shell_callbacks.reply(reply)});
896 }
899 }
897 if (content.payload && shell_callbacks.payload) {
900 if (content.payload && shell_callbacks.payload) {
898 this._handle_payloads(content.payload, shell_callbacks.payload, reply);
901 promise = promise.then(function() {
902 return that._handle_payloads(content.payload, shell_callbacks.payload, reply);
903 });
899 }
904 }
905 return promise;
900 };
906 };
901
907
902 /**
908 /**
903 * @function _handle_payloads
909 * @function _handle_payloads
904 */
910 */
905 Kernel.prototype._handle_payloads = function (payloads, payload_callbacks, msg) {
911 Kernel.prototype._handle_payloads = function (payloads, payload_callbacks, msg) {
912 var promise = [];
906 var l = payloads.length;
913 var l = payloads.length;
907 // Payloads are handled by triggering events because we don't want the Kernel
914 // Payloads are handled by triggering events because we don't want the Kernel
908 // to depend on the Notebook or Pager classes.
915 // to depend on the Notebook or Pager classes.
@@ -910,9 +917,10 b' define(['
910 var payload = payloads[i];
917 var payload = payloads[i];
911 var callback = payload_callbacks[payload.source];
918 var callback = payload_callbacks[payload.source];
912 if (callback) {
919 if (callback) {
913 callback(payload, msg);
920 promise.push(callback(payload, msg));
914 }
921 }
915 }
922 }
923 return Promise.all(promise);
916 };
924 };
917
925
918 /**
926 /**
@@ -1025,7 +1033,7 b' define(['
1025 Kernel.prototype._handle_iopub_message = function (msg) {
1033 Kernel.prototype._handle_iopub_message = function (msg) {
1026 var handler = this.get_iopub_handler(msg.header.msg_type);
1034 var handler = this.get_iopub_handler(msg.header.msg_type);
1027 if (handler !== undefined) {
1035 if (handler !== undefined) {
1028 handler(msg);
1036 return handler(msg);
1029 }
1037 }
1030 };
1038 };
1031
1039
General Comments 0
You need to be logged in to leave comments. Login now