Show More
@@ -1,192 +1,198 | |||
|
1 | 1 | //---------------------------------------------------------------------------- |
|
2 | 2 | // Copyright (C) 2013 The IPython Development Team |
|
3 | 3 | // |
|
4 | 4 | // Distributed under the terms of the BSD License. The full license is in |
|
5 | 5 | // the file COPYING, distributed as part of this software. |
|
6 | 6 | //---------------------------------------------------------------------------- |
|
7 | 7 | |
|
8 | 8 | //============================================================================ |
|
9 | 9 | // Comm and CommManager bases |
|
10 | 10 | //============================================================================ |
|
11 | 11 | /** |
|
12 | 12 | * Base Comm classes |
|
13 | 13 | * @module IPython |
|
14 | 14 | * @namespace IPython |
|
15 | 15 | * @submodule comm |
|
16 | 16 | */ |
|
17 | 17 | |
|
18 | 18 | var IPython = (function (IPython) { |
|
19 | 19 | "use strict"; |
|
20 | 20 | |
|
21 | 21 | //----------------------------------------------------------------------- |
|
22 | 22 | // CommManager class |
|
23 | 23 | //----------------------------------------------------------------------- |
|
24 | 24 | |
|
25 | 25 | var CommManager = function (kernel) { |
|
26 | 26 | this.comms = {}; |
|
27 | 27 | this.targets = {}; |
|
28 | 28 | if (kernel !== undefined) { |
|
29 | 29 | this.init_kernel(kernel); |
|
30 | 30 | } |
|
31 | 31 | }; |
|
32 | 32 | |
|
33 | 33 | CommManager.prototype.init_kernel = function (kernel) { |
|
34 | 34 | // connect the kernel, and register message handlers |
|
35 | 35 | this.kernel = kernel; |
|
36 | 36 | var msg_types = ['comm_open', 'comm_msg', 'comm_close']; |
|
37 | 37 | for (var i = 0; i < msg_types.length; i++) { |
|
38 | 38 | var msg_type = msg_types[i]; |
|
39 | 39 | kernel.register_iopub_handler(msg_type, $.proxy(this[msg_type], this)); |
|
40 | 40 | } |
|
41 | 41 | }; |
|
42 | 42 | |
|
43 | 43 | CommManager.prototype.new_comm = function (target_name, data, callbacks, metadata, comm_id) { |
|
44 | 44 | // Create a new Comm, register it, and open its Kernel-side counterpart |
|
45 | 45 | // Mimics the auto-registration in `Comm.__init__` in the IPython Comm |
|
46 | 46 | var comm = new Comm(target_name, comm_id); |
|
47 | 47 | this.register_comm(comm); |
|
48 | 48 | comm.open(data, callbacks, metadata); |
|
49 | 49 | return comm; |
|
50 | 50 | }; |
|
51 | 51 | |
|
52 | 52 | CommManager.prototype.register_target = function (target_name, f) { |
|
53 | 53 | // Register a target function for a given target name |
|
54 | 54 | this.targets[target_name] = f; |
|
55 | 55 | }; |
|
56 | 56 | |
|
57 | 57 | CommManager.prototype.unregister_target = function (target_name, f) { |
|
58 | 58 | // Unregister a target function for a given target name |
|
59 | 59 | delete this.targets[target_name]; |
|
60 | 60 | }; |
|
61 | 61 | |
|
62 | 62 | CommManager.prototype.register_comm = function (comm) { |
|
63 | 63 | // Register a comm in the mapping |
|
64 | 64 | this.comms[comm.comm_id] = comm; |
|
65 | 65 | comm.kernel = this.kernel; |
|
66 | 66 | return comm.comm_id; |
|
67 | 67 | }; |
|
68 | 68 | |
|
69 | 69 | CommManager.prototype.unregister_comm = function (comm_id) { |
|
70 | 70 | // Remove a comm from the mapping |
|
71 | 71 | delete this.comms[comm_id]; |
|
72 | 72 | }; |
|
73 | 73 | |
|
74 | 74 | // comm message handlers |
|
75 | 75 | |
|
76 | 76 | CommManager.prototype.comm_open = function (msg) { |
|
77 | 77 | var content = msg.content; |
|
78 | 78 | var f = this.targets[content.target_name]; |
|
79 | 79 | if (f === undefined) { |
|
80 | 80 | console.log("No such target registered: ", content.target_name); |
|
81 | 81 | console.log("Available targets are: ", this.targets); |
|
82 | 82 | return; |
|
83 | 83 | } |
|
84 | 84 | var comm = new Comm(content.target_name, content.comm_id); |
|
85 | 85 | this.register_comm(comm); |
|
86 | 86 | try { |
|
87 | 87 | f(comm, msg); |
|
88 | 88 | } catch (e) { |
|
89 | 89 | console.log("Exception opening new comm:", e, msg); |
|
90 | 90 | comm.close(); |
|
91 | 91 | this.unregister_comm(comm); |
|
92 | 92 | } |
|
93 | 93 | }; |
|
94 | 94 | |
|
95 | 95 | CommManager.prototype.comm_close = function (msg) { |
|
96 | 96 | var content = msg.content; |
|
97 | 97 | var comm = this.comms[content.comm_id]; |
|
98 | 98 | if (comm === undefined) { |
|
99 | 99 | return; |
|
100 | 100 | } |
|
101 | 101 | delete this.comms[content.comm_id]; |
|
102 | 102 | try { |
|
103 | 103 | comm.handle_close(msg); |
|
104 | 104 | } catch (e) { |
|
105 | 105 | console.log("Exception closing comm: ", e, msg); |
|
106 | 106 | } |
|
107 | 107 | }; |
|
108 | 108 | |
|
109 | 109 | CommManager.prototype.comm_msg = function (msg) { |
|
110 | 110 | var content = msg.content; |
|
111 | 111 | var comm = this.comms[content.comm_id]; |
|
112 | 112 | if (comm === undefined) { |
|
113 | 113 | return; |
|
114 | 114 | } |
|
115 | 115 | try { |
|
116 | 116 | comm.handle_msg(msg); |
|
117 | 117 | } catch (e) { |
|
118 | 118 | console.log("Exception handling comm msg: ", e, msg); |
|
119 | 119 | } |
|
120 | 120 | }; |
|
121 | 121 | |
|
122 | 122 | //----------------------------------------------------------------------- |
|
123 | 123 | // Comm base class |
|
124 | 124 | //----------------------------------------------------------------------- |
|
125 | 125 | |
|
126 | 126 | var Comm = function (target_name, comm_id) { |
|
127 | 127 | this.target_name = target_name; |
|
128 | 128 | this.comm_id = comm_id || IPython.utils.uuid(); |
|
129 | 129 | this._msg_callback = this._close_callback = null; |
|
130 | 130 | }; |
|
131 | 131 | |
|
132 | 132 | // methods for sending messages |
|
133 | 133 | Comm.prototype.open = function (data, callbacks, metadata) { |
|
134 | 134 | var content = { |
|
135 | 135 | comm_id : this.comm_id, |
|
136 | 136 | target_name : this.target_name, |
|
137 | 137 | data : data || {}, |
|
138 | 138 | }; |
|
139 | 139 | return this.kernel.send_shell_message("comm_open", content, callbacks, metadata); |
|
140 | 140 | }; |
|
141 | 141 | |
|
142 | 142 | Comm.prototype.send = function (data, callbacks, metadata) { |
|
143 | 143 | var content = { |
|
144 | 144 | comm_id : this.comm_id, |
|
145 | 145 | data : data || {}, |
|
146 | 146 | }; |
|
147 | 147 | return this.kernel.send_shell_message("comm_msg", content, callbacks, metadata); |
|
148 | 148 | }; |
|
149 | 149 | |
|
150 | 150 | Comm.prototype.close = function (data, callbacks, metadata) { |
|
151 | 151 | var content = { |
|
152 | 152 | comm_id : this.comm_id, |
|
153 | 153 | data : data || {}, |
|
154 | 154 | }; |
|
155 | 155 | return this.kernel.send_shell_message("comm_close", content, callbacks, metadata); |
|
156 | 156 | }; |
|
157 | 157 | |
|
158 | 158 | // methods for registering callbacks for incoming messages |
|
159 | 159 | Comm.prototype._register_callback = function (key, callback) { |
|
160 | 160 | this['_' + key + '_callback'] = callback; |
|
161 | 161 | }; |
|
162 | 162 | |
|
163 | 163 | Comm.prototype.on_msg = function (callback) { |
|
164 | 164 | this._register_callback('msg', callback); |
|
165 | 165 | }; |
|
166 | 166 | |
|
167 | 167 | Comm.prototype.on_close = function (callback) { |
|
168 | 168 | this._register_callback('close', callback); |
|
169 | 169 | }; |
|
170 | 170 | |
|
171 | 171 | // methods for handling incoming messages |
|
172 | 172 | |
|
173 | 173 | Comm.prototype._maybe_callback = function (key, msg) { |
|
174 | 174 | var callback = this['_' + key + '_callback']; |
|
175 |
if (callback) |
|
|
175 | if (callback) { | |
|
176 | try { | |
|
177 | callback(msg); | |
|
178 | } catch (e) { | |
|
179 | console.log("Exception in Comm callback", e, msg); | |
|
180 | } | |
|
181 | } | |
|
176 | 182 | }; |
|
177 | 183 | |
|
178 | 184 | Comm.prototype.handle_msg = function (msg) { |
|
179 | 185 | this._maybe_callback('msg', msg); |
|
180 | 186 | }; |
|
181 | 187 | |
|
182 | 188 | Comm.prototype.handle_close = function (msg) { |
|
183 | 189 | this._maybe_callback('close', msg); |
|
184 | 190 | }; |
|
185 | 191 | |
|
186 | 192 | IPython.CommManager = CommManager; |
|
187 | 193 | IPython.Comm = Comm; |
|
188 | 194 | |
|
189 | 195 | return IPython; |
|
190 | 196 | |
|
191 | 197 | }(IPython)); |
|
192 | 198 |
General Comments 0
You need to be logged in to leave comments.
Login now