##// END OF EJS Templates
Add CommManager.new_comm...
MinRK -
Show More
@@ -1,183 +1,192 b''
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 CommManager.prototype.new_comm = function (target_name, data, callbacks, metadata, comm_id) {
44 // Create a new Comm, register it, and open its Kernel-side counterpart
45 // Mimics the auto-registration in `Comm.__init__` in the IPython Comm
46 var comm = new Comm(target_name, comm_id);
47 this.register_comm(comm);
48 comm.open(data, callbacks, metadata);
49 return comm;
50 };
51
43 52 CommManager.prototype.register_target = function (target_name, f) {
44 53 // Register a target function for a given target name
45 54 this.targets[target_name] = f;
46 55 };
47 56
48 57 CommManager.prototype.unregister_target = function (target_name, f) {
49 58 // Unregister a target function for a given target name
50 59 delete this.targets[target_name];
51 60 };
52 61
53 62 CommManager.prototype.register_comm = function (comm) {
54 63 // Register a comm in the mapping
55 64 this.comms[comm.comm_id] = comm;
56 65 comm.kernel = this.kernel;
57 66 return comm.comm_id;
58 67 };
59 68
60 69 CommManager.prototype.unregister_comm = function (comm_id) {
61 70 // Remove a comm from the mapping
62 71 delete this.comms[comm_id];
63 72 };
64 73
65 74 // comm message handlers
66 75
67 76 CommManager.prototype.comm_open = function (msg) {
68 77 var content = msg.content;
69 78 var f = this.targets[content.target_name];
70 79 if (f === undefined) {
71 80 console.log("No such target registered: ", content.target_name);
72 81 console.log("Available targets are: ", this.targets);
73 82 return;
74 83 }
75 var comm = new Comm(content.comm_id);
84 var comm = new Comm(content.target_name, content.comm_id);
76 85 this.register_comm(comm);
77 86 try {
78 87 f(comm, msg);
79 88 } catch (e) {
80 89 console.log("Exception opening new comm:", e, msg);
81 90 comm.close();
82 91 this.unregister_comm(comm);
83 92 }
84 93 };
85 94
86 95 CommManager.prototype.comm_close = function (msg) {
87 96 var content = msg.content;
88 97 var comm = this.comms[content.comm_id];
89 98 if (comm === undefined) {
90 99 return;
91 100 }
92 101 delete this.comms[content.comm_id];
93 102 try {
94 103 comm.handle_close(msg);
95 104 } catch (e) {
96 105 console.log("Exception closing comm: ", e, msg);
97 106 }
98 107 };
99 108
100 109 CommManager.prototype.comm_msg = function (msg) {
101 110 var content = msg.content;
102 111 var comm = this.comms[content.comm_id];
103 112 if (comm === undefined) {
104 113 return;
105 114 }
106 115 try {
107 116 comm.handle_msg(msg);
108 117 } catch (e) {
109 118 console.log("Exception handling comm msg: ", e, msg);
110 119 }
111 120 };
112 121
113 122 //-----------------------------------------------------------------------
114 123 // Comm base class
115 124 //-----------------------------------------------------------------------
116 125
117 var Comm = function (comm_id, target_name) {
118 this.comm_id = comm_id || new IPython.utils.uuid();
126 var Comm = function (target_name, comm_id) {
119 127 this.target_name = target_name;
128 this.comm_id = comm_id || IPython.utils.uuid();
120 129 this._msg_callback = this._close_callback = null;
121 130 };
122 131
123 132 // methods for sending messages
124 133 Comm.prototype.open = function (data, callbacks, metadata) {
125 134 var content = {
126 135 comm_id : this.comm_id,
127 136 target_name : this.target_name,
128 137 data : data || {},
129 138 };
130 139 return this.kernel.send_shell_message("comm_open", content, callbacks, metadata);
131 140 };
132 141
133 142 Comm.prototype.send = function (data, callbacks, metadata) {
134 143 var content = {
135 144 comm_id : this.comm_id,
136 145 data : data || {},
137 146 };
138 147 return this.kernel.send_shell_message("comm_msg", content, callbacks, metadata);
139 148 };
140 149
141 150 Comm.prototype.close = function (data, callbacks, metadata) {
142 151 var content = {
143 152 comm_id : this.comm_id,
144 153 data : data || {},
145 154 };
146 155 return this.kernel.send_shell_message("comm_close", content, callbacks, metadata);
147 156 };
148 157
149 158 // methods for registering callbacks for incoming messages
150 159 Comm.prototype._register_callback = function (key, callback) {
151 160 this['_' + key + '_callback'] = callback;
152 161 };
153 162
154 163 Comm.prototype.on_msg = function (callback) {
155 164 this._register_callback('msg', callback);
156 165 };
157 166
158 167 Comm.prototype.on_close = function (callback) {
159 168 this._register_callback('close', callback);
160 169 };
161 170
162 171 // methods for handling incoming messages
163 172
164 173 Comm.prototype._maybe_callback = function (key, msg) {
165 174 var callback = this['_' + key + '_callback'];
166 175 if (callback) callback(msg);
167 176 };
168 177
169 178 Comm.prototype.handle_msg = function (msg) {
170 179 this._maybe_callback('msg', msg);
171 180 };
172 181
173 182 Comm.prototype.handle_close = function (msg) {
174 183 this._maybe_callback('close', msg);
175 184 };
176 185
177 186 IPython.CommManager = CommManager;
178 187 IPython.Comm = Comm;
179 188
180 189 return IPython;
181 190
182 191 }(IPython));
183 192
General Comments 0
You need to be logged in to leave comments. Login now