##// END OF EJS Templates
hook up output for comm messages
MinRK -
Show More
@@ -97,35 +97,35 b' var IPython = (function (IPython) {'
97 //-----------------------------------------------------------------------
97 //-----------------------------------------------------------------------
98
98
99 var Comm = function (comm_id, target) {
99 var Comm = function (comm_id, target) {
100 this.comm_id = comm_id;
100 this.comm_id = comm_id || new IPython.utils.uuid();
101 this.target = target || 'comm';
101 this.target = target || 'comm';
102 this._msg_callback = this._open_callback = this._close_callback = null;
102 this._msg_callback = this._open_callback = this._close_callback = null;
103 };
103 };
104
104
105 // methods for sending messages
105 // methods for sending messages
106 Comm.prototype.open = function (data) {
106 Comm.prototype.open = function (data, callbacks) {
107 var content = {
107 var content = {
108 comm_id : this.comm_id,
108 comm_id : this.comm_id,
109 target : this.target,
109 target : this.target,
110 data : data || {},
110 data : data || {},
111 };
111 };
112 this.kernel.send_shell_message("comm_open", content);
112 return this.kernel.send_shell_message("comm_open", content, callbacks);
113 };
113 };
114
114
115 Comm.prototype.send = function (data) {
115 Comm.prototype.send = function (data, callbacks) {
116 var content = {
116 var content = {
117 comm_id : this.comm_id,
117 comm_id : this.comm_id,
118 data : data || {},
118 data : data || {},
119 };
119 };
120 return this.kernel.send_shell_message("comm_msg", content);
120 return this.kernel.send_shell_message("comm_msg", content, callbacks);
121 };
121 };
122
122
123 Comm.prototype.close = function (data) {
123 Comm.prototype.close = function (data, callbacks) {
124 var content = {
124 var content = {
125 comm_id : this.comm_id,
125 comm_id : this.comm_id,
126 data : data || {},
126 data : data || {},
127 };
127 };
128 return this.kernel.send_shell_message("comm_close", content);
128 return this.kernel.send_shell_message("comm_close", content, callbacks);
129 };
129 };
130
130
131 // methods for registering callbacks for incoming messages
131 // methods for registering callbacks for incoming messages
@@ -144,7 +144,7 b' var IPython = (function (IPython) {'
144 Comm.prototype.on_close = function (callback) {
144 Comm.prototype.on_close = function (callback) {
145 this._register_callback('close', callback);
145 this._register_callback('close', callback);
146 };
146 };
147
147
148 // methods for handling incoming messages
148 // methods for handling incoming messages
149
149
150 Comm.prototype._maybe_callback = function (key, msg) {
150 Comm.prototype._maybe_callback = function (key, msg) {
@@ -166,7 +166,7 b' var IPython = (function (IPython) {'
166
166
167 IPython.CommManager = CommManager;
167 IPython.CommManager = CommManager;
168 IPython.Comm = Comm;
168 IPython.Comm = Comm;
169
169
170 return IPython;
170 return IPython;
171
171
172 }(IPython));
172 }(IPython));
@@ -11,6 +11,8 b''
11 # Imports
11 # Imports
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13
13
14 import sys
15
14 from IPython.config import LoggingConfigurable
16 from IPython.config import LoggingConfigurable
15 from IPython.core.prompts import LazyEvaluate
17 from IPython.core.prompts import LazyEvaluate
16 from IPython.core.getipython import get_ipython
18 from IPython.core.getipython import get_ipython
@@ -33,6 +35,23 b' def lazy_keys(dikt):'
33 return LazyEvaluate(lambda d: list(d.keys()))
35 return LazyEvaluate(lambda d: list(d.keys()))
34
36
35
37
38 def with_output(method):
39 """method decorator for ensuring output is handled properly in a message handler
40
41 - sets parent header before entering the method
42 - flushes stdout/stderr after
43 """
44 def method_with_output(self, stream, ident, msg):
45 self.shell.set_parent(msg['header'])
46 try:
47 return method(self, stream, ident, msg)
48 finally:
49 sys.stdout.flush()
50 sys.stderr.flush()
51
52 return method_with_output
53
54
36 class CommManager(LoggingConfigurable):
55 class CommManager(LoggingConfigurable):
37 """Manager for Comms in the Kernel"""
56 """Manager for Comms in the Kernel"""
38
57
@@ -97,7 +116,7 b' class CommManager(LoggingConfigurable):'
97 return comm
116 return comm
98
117
99 # Message handlers
118 # Message handlers
100
119 @with_output
101 def comm_open(self, stream, ident, msg):
120 def comm_open(self, stream, ident, msg):
102 """Handler for comm_open messages"""
121 """Handler for comm_open messages"""
103 content = msg['content']
122 content = msg['content']
@@ -117,6 +136,7 b' class CommManager(LoggingConfigurable):'
117 comm.handle_open(msg)
136 comm.handle_open(msg)
118 self.register_comm(comm)
137 self.register_comm(comm)
119
138
139 @with_output
120 def comm_msg(self, stream, ident, msg):
140 def comm_msg(self, stream, ident, msg):
121 """Handler for comm_msg messages"""
141 """Handler for comm_msg messages"""
122 content = msg['content']
142 content = msg['content']
@@ -127,6 +147,7 b' class CommManager(LoggingConfigurable):'
127 return
147 return
128 comm.handle_msg(msg)
148 comm.handle_msg(msg)
129
149
150 @with_output
130 def comm_close(self, stream, ident, msg):
151 def comm_close(self, stream, ident, msg):
131 """Handler for comm_close messages"""
152 """Handler for comm_close messages"""
132 content = msg['content']
153 content = msg['content']
General Comments 0
You need to be logged in to leave comments. Login now