##// END OF EJS Templates
COMM: mirror Python callback API in Javascript
MinRK -
Show More
@@ -99,6 +99,7 b' var IPython = (function (IPython) {'
99 99 var Comm = function (comm_id, target) {
100 100 this.comm_id = comm_id;
101 101 this.target = target || 'comm';
102 this._msg_callback = this._open_callback = this._close_callback = null;
102 103 };
103 104
104 105 // methods for sending messages
@@ -127,18 +128,40 b' var IPython = (function (IPython) {'
127 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 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 155 Comm.prototype.handle_open = function (msg) {
133 $([this]).trigger("comm_open", msg);
156 this._maybe_callback('open', msg);
134 157 };
135 158
136 159 Comm.prototype.handle_msg = function (msg) {
137 $([this]).trigger("comm_msg", msg);
160 this._maybe_callback('msg', msg);
138 161 };
139 162
140 163 Comm.prototype.handle_close = function (msg) {
141 $([this]).trigger("comm_close", msg);
164 this._maybe_callback('close', msg);
142 165 };
143 166
144 167 IPython.CommManager = CommManager;
General Comments 0
You need to be logged in to leave comments. Login now