Show More
@@ -99,6 +99,7 b' var IPython = (function (IPython) {' | |||||
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; | |
101 | this.target = target || 'comm'; |
|
101 | this.target = target || 'comm'; | |
|
102 | this._msg_callback = this._open_callback = this._close_callback = null; | |||
102 | }; |
|
103 | }; | |
103 |
|
104 | |||
104 | // methods for sending messages |
|
105 | // methods for sending messages | |
@@ -127,18 +128,40 b' var IPython = (function (IPython) {' | |||||
127 | return this.kernel.send_shell_message("comm_close", content); |
|
128 | return this.kernel.send_shell_message("comm_close", content); | |
128 | }; |
|
129 | }; | |
129 |
|
130 | |||
|
131 | // methods for registering callbacks for incoming messages | |||
|
132 | Comm.prototype._register_callback = function (key, callback) { | |||
|
133 | this['_' + key + '_callback'] = callback; | |||
|
134 | }; | |||
|
135 | ||||
|
136 | Comm.prototype.on_open = function (callback) { | |||
|
137 | this._register_callback('open', callback); | |||
|
138 | }; | |||
|
139 | ||||
|
140 | Comm.prototype.on_msg = function (callback) { | |||
|
141 | this._register_callback('msg', callback); | |||
|
142 | }; | |||
|
143 | ||||
|
144 | Comm.prototype.on_close = function (callback) { | |||
|
145 | this._register_callback('close', callback); | |||
|
146 | }; | |||
|
147 | ||||
130 | // methods for handling incoming messages |
|
148 | // methods for handling incoming messages | |
131 |
|
149 | |||
|
150 | Comm.prototype._maybe_callback = function (key, msg) { | |||
|
151 | var callback = this['_' + key + '_callback']; | |||
|
152 | if (callback) callback(msg); | |||
|
153 | }; | |||
|
154 | ||||
132 | Comm.prototype.handle_open = function (msg) { |
|
155 | Comm.prototype.handle_open = function (msg) { | |
133 |
|
|
156 | this._maybe_callback('open', msg); | |
134 | }; |
|
157 | }; | |
135 |
|
158 | |||
136 | Comm.prototype.handle_msg = function (msg) { |
|
159 | Comm.prototype.handle_msg = function (msg) { | |
137 | $([this]).trigger("comm_msg", msg); |
|
160 | this._maybe_callback('msg', msg); | |
138 | }; |
|
161 | }; | |
139 |
|
162 | |||
140 | Comm.prototype.handle_close = function (msg) { |
|
163 | Comm.prototype.handle_close = function (msg) { | |
141 |
|
|
164 | this._maybe_callback('close', msg); | |
142 | }; |
|
165 | }; | |
143 |
|
166 | |||
144 | IPython.CommManager = CommManager; |
|
167 | IPython.CommManager = CommManager; |
General Comments 0
You need to be logged in to leave comments.
Login now