Show More
@@ -1,150 +1,173 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2013 The IPython Development Team |
|
2 | // Copyright (C) 2013 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // Comm and CommManager bases |
|
9 | // Comm and CommManager bases | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 | /** |
|
11 | /** | |
12 | * Base Comm classes |
|
12 | * Base Comm classes | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | * @submodule comm |
|
15 | * @submodule comm | |
16 | */ |
|
16 | */ | |
17 |
|
17 | |||
18 | var IPython = (function (IPython) { |
|
18 | var IPython = (function (IPython) { | |
19 | "use strict"; |
|
19 | "use strict"; | |
20 |
|
20 | |||
21 | //----------------------------------------------------------------------- |
|
21 | //----------------------------------------------------------------------- | |
22 | // CommManager class |
|
22 | // CommManager class | |
23 | //----------------------------------------------------------------------- |
|
23 | //----------------------------------------------------------------------- | |
24 |
|
24 | |||
25 | var CommManager = function (kernel) { |
|
25 | var CommManager = function (kernel) { | |
26 | this.comms = {}; |
|
26 | this.comms = {}; | |
27 | this.targets = {comm : Comm}; |
|
27 | this.targets = {comm : Comm}; | |
28 | if (kernel !== undefined) { |
|
28 | if (kernel !== undefined) { | |
29 | this.init_kernel(kernel); |
|
29 | this.init_kernel(kernel); | |
30 | } |
|
30 | } | |
31 | }; |
|
31 | }; | |
32 |
|
32 | |||
33 | CommManager.prototype.init_kernel = function (kernel) { |
|
33 | CommManager.prototype.init_kernel = function (kernel) { | |
34 | // connect the kernel, and register message handlers |
|
34 | // connect the kernel, and register message handlers | |
35 | this.kernel = kernel; |
|
35 | this.kernel = kernel; | |
36 | var msg_types = ['comm_open', 'comm_msg', 'comm_close']; |
|
36 | var msg_types = ['comm_open', 'comm_msg', 'comm_close']; | |
37 | for (var i = 0; i < msg_types.length; i++) { |
|
37 | for (var i = 0; i < msg_types.length; i++) { | |
38 | var msg_type = msg_types[i]; |
|
38 | var msg_type = msg_types[i]; | |
39 | kernel.register_iopub_handler(msg_type, $.proxy(this[msg_type], this)); |
|
39 | kernel.register_iopub_handler(msg_type, $.proxy(this[msg_type], this)); | |
40 | } |
|
40 | } | |
41 | }; |
|
41 | }; | |
42 |
|
42 | |||
43 | CommManager.prototype.register_target = function (target, constructor) { |
|
43 | CommManager.prototype.register_target = function (target, constructor) { | |
44 | // Register a constructor for a given target key |
|
44 | // Register a constructor for a given target key | |
45 | this.targets[target] = constructor; |
|
45 | this.targets[target] = constructor; | |
46 | }; |
|
46 | }; | |
47 |
|
47 | |||
48 | CommManager.prototype.register_comm = function (comm) { |
|
48 | CommManager.prototype.register_comm = function (comm) { | |
49 | // Register a comm in the mapping |
|
49 | // Register a comm in the mapping | |
50 | this.comms[comm.comm_id] = comm; |
|
50 | this.comms[comm.comm_id] = comm; | |
51 | comm.kernel = this.kernel; |
|
51 | comm.kernel = this.kernel; | |
52 | return comm.comm_id; |
|
52 | return comm.comm_id; | |
53 | }; |
|
53 | }; | |
54 |
|
54 | |||
55 | CommManager.prototype.unregister_comm = function (comm_id) { |
|
55 | CommManager.prototype.unregister_comm = function (comm_id) { | |
56 | // Remove a comm from the mapping |
|
56 | // Remove a comm from the mapping | |
57 | delete this.comms[comm_id]; |
|
57 | delete this.comms[comm_id]; | |
58 | }; |
|
58 | }; | |
59 |
|
59 | |||
60 | // comm message handlers |
|
60 | // comm message handlers | |
61 |
|
61 | |||
62 | CommManager.prototype.comm_open = function (msg) { |
|
62 | CommManager.prototype.comm_open = function (msg) { | |
63 | var content = msg.content; |
|
63 | var content = msg.content; | |
64 | var callback = this.targets[content.target]; |
|
64 | var callback = this.targets[content.target]; | |
65 | if (callback === undefined) { |
|
65 | if (callback === undefined) { | |
66 | console.log("No such target registered: ", content.target); |
|
66 | console.log("No such target registered: ", content.target); | |
67 | console.log("Available targets are: ", this.targets); |
|
67 | console.log("Available targets are: ", this.targets); | |
68 | return; |
|
68 | return; | |
69 | } |
|
69 | } | |
70 | var comm = new Comm(content.comm_id); |
|
70 | var comm = new Comm(content.comm_id); | |
71 | this.register_comm(comm); |
|
71 | this.register_comm(comm); | |
72 | callback(comm); |
|
72 | callback(comm); | |
73 | comm.handle_open(msg); |
|
73 | comm.handle_open(msg); | |
74 | }; |
|
74 | }; | |
75 |
|
75 | |||
76 | CommManager.prototype.comm_close = function (msg) { |
|
76 | CommManager.prototype.comm_close = function (msg) { | |
77 | var content = msg.content; |
|
77 | var content = msg.content; | |
78 | var comm = this.comms[content.comm_id]; |
|
78 | var comm = this.comms[content.comm_id]; | |
79 | if (comm === undefined) { |
|
79 | if (comm === undefined) { | |
80 | return; |
|
80 | return; | |
81 | } |
|
81 | } | |
82 | delete this.comms[content.comm_id]; |
|
82 | delete this.comms[content.comm_id]; | |
83 | comm.handle_close(msg); |
|
83 | comm.handle_close(msg); | |
84 | }; |
|
84 | }; | |
85 |
|
85 | |||
86 | CommManager.prototype.comm_msg = function (msg) { |
|
86 | CommManager.prototype.comm_msg = function (msg) { | |
87 | var content = msg.content; |
|
87 | var content = msg.content; | |
88 | var comm = this.comms[content.comm_id]; |
|
88 | var comm = this.comms[content.comm_id]; | |
89 | if (comm === undefined) { |
|
89 | if (comm === undefined) { | |
90 | return; |
|
90 | return; | |
91 | } |
|
91 | } | |
92 | comm.handle_msg(msg); |
|
92 | comm.handle_msg(msg); | |
93 | }; |
|
93 | }; | |
94 |
|
94 | |||
95 | //----------------------------------------------------------------------- |
|
95 | //----------------------------------------------------------------------- | |
96 | // Comm base class |
|
96 | // Comm base class | |
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; | |
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 | |
105 | Comm.prototype.open = function (data) { |
|
106 | Comm.prototype.open = function (data) { | |
106 | var content = { |
|
107 | var content = { | |
107 | comm_id : this.comm_id, |
|
108 | comm_id : this.comm_id, | |
108 | target : this.target, |
|
109 | target : this.target, | |
109 | data : data || {}, |
|
110 | data : data || {}, | |
110 | }; |
|
111 | }; | |
111 | this.kernel.send_shell_message("comm_open", content); |
|
112 | this.kernel.send_shell_message("comm_open", content); | |
112 | }; |
|
113 | }; | |
113 |
|
114 | |||
114 | Comm.prototype.send = function (data) { |
|
115 | Comm.prototype.send = function (data) { | |
115 | var content = { |
|
116 | var content = { | |
116 | comm_id : this.comm_id, |
|
117 | comm_id : this.comm_id, | |
117 | data : data || {}, |
|
118 | data : data || {}, | |
118 | }; |
|
119 | }; | |
119 | return this.kernel.send_shell_message("comm_msg", content); |
|
120 | return this.kernel.send_shell_message("comm_msg", content); | |
120 | }; |
|
121 | }; | |
121 |
|
122 | |||
122 | Comm.prototype.close = function (data) { |
|
123 | Comm.prototype.close = function (data) { | |
123 | var content = { |
|
124 | var content = { | |
124 | comm_id : this.comm_id, |
|
125 | comm_id : this.comm_id, | |
125 | data : data || {}, |
|
126 | data : data || {}, | |
126 | }; |
|
127 | }; | |
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; | |
145 | IPython.Comm = Comm; |
|
168 | IPython.Comm = Comm; | |
146 |
|
169 | |||
147 | return IPython; |
|
170 | return IPython; | |
148 |
|
171 | |||
149 | }(IPython)); |
|
172 | }(IPython)); | |
150 |
|
173 |
General Comments 0
You need to be logged in to leave comments.
Login now