##// END OF EJS Templates
Add state packet delta compression.
Jonathan Frederic -
Show More
@@ -32,7 +32,7 b' define(["static/components/underscore/underscore-min.js",'
32 //--------------------------------------------------------------------
32 //--------------------------------------------------------------------
33 var WidgetModel = Backbone.Model.extend({
33 var WidgetModel = Backbone.Model.extend({
34 apply: function(sender) {
34 apply: function(sender) {
35 this.save();
35 this.save(this.changedAttributes(), {patch: true});
36
36
37 for (var index in this.views) {
37 for (var index in this.views) {
38 var view = this.views[index];
38 var view = this.views[index];
@@ -95,7 +95,7 b' define(["static/components/underscore/underscore-min.js",'
95
95
96 var that = this;
96 var that = this;
97 Backbone.sync = function(method, model, options, error) {
97 Backbone.sync = function(method, model, options, error) {
98 var result = that.send_sync(method, model);
98 var result = that.send_sync(method, model, options);
99 if (options.success) {
99 if (options.success) {
100 options.success(result);
100 options.success(result);
101 }
101 }
@@ -214,6 +214,7 b' define(["static/components/underscore/underscore-min.js",'
214 }
214 }
215 }
215 }
216 }
216 }
217 comm.model.id = comm.comm_id;
217 comm.model.save();
218 comm.model.save();
218 this.updating = false;
219 this.updating = false;
219 }
220 }
@@ -234,7 +235,7 b' define(["static/components/underscore/underscore-min.js",'
234 }
235 }
235
236
236 // Send widget state to python backend.
237 // Send widget state to python backend.
237 WidgetManager.prototype.send_sync = function (method, model) {
238 WidgetManager.prototype.send_sync = function (method, model, options) {
238 var model_json = model.toJSON();
239 var model_json = model.toJSON();
239
240
240 // Only send updated state if the state hasn't been changed during an update.
241 // Only send updated state if the state hasn't been changed during an update.
@@ -250,10 +251,21 b' define(["static/components/underscore/underscore-min.js",'
250 clear_output : $.proxy(outputarea.handle_clear_output, outputarea)}
251 clear_output : $.proxy(outputarea.handle_clear_output, outputarea)}
251 };
252 };
252 };
253 };
253 var data = {sync_method: method, sync_data: model_json};
254
255 // If this is a patch operation, just send the changes.
256 var send_json = model_json;
257 if (method=='patch') {
258 send_json = {};
259 for (var attr in options.attrs) {
260 send_json[attr] = options.attrs[attr];
261 }
262 }
263 var data = {sync_method: method, sync_data: send_json};
254 comm.send(data, callbacks);
264 comm.send(data, callbacks);
255 }
265 }
256
266
267 // Since the comm is a one-way communication, assume the message
268 // arrived.
257 return model_json;
269 return model_json;
258 }
270 }
259
271
@@ -96,11 +96,10 b' class Widget(LoggingConfigurable):'
96 ### Event handlers
96 ### Event handlers
97 def _handle_msg(self, msg):
97 def _handle_msg(self, msg):
98
98
99 # Handle backbone sync methods
99 # Handle backbone sync methods CREATE, PATCH, and UPDATE
100 sync_method = msg['content']['data']['sync_method']
100 sync_method = msg['content']['data']['sync_method']
101 sync_data = msg['content']['data']['sync_data']
101 sync_data = msg['content']['data']['sync_data']
102 if sync_method.lower() in ['create', 'update']:
102 self._handle_recieve_state(sync_data) # handles all methods
103 self._handle_recieve_state(sync_data)
104
103
105
104
106 def _handle_recieve_state(self, sync_data):
105 def _handle_recieve_state(self, sync_data):
@@ -119,7 +118,7 b' class Widget(LoggingConfigurable):'
119 if not self._property_lock and self.comm is not None:
118 if not self._property_lock and self.comm is not None:
120 # TODO: Validate properties.
119 # TODO: Validate properties.
121 # Send new state to frontend
120 # Send new state to frontend
122 self.send_state()
121 self.send_state(key=name)
123
122
124
123
125 def _handle_close(self):
124 def _handle_close(self):
@@ -154,8 +153,15 b' class Widget(LoggingConfigurable):'
154 return None
153 return None
155
154
156
155
157 def send_state(self):
156 def send_state(self, key=None):
158 state = {}
157 state = {}
158
159 # If a key is provided, just send the state of that key.
160 keys = []
161 if key is None:
162 keys.extend(self.keys)
163 else:
164 keys.append(key)
159 for key in self.keys:
165 for key in self.keys:
160 try:
166 try:
161 state[key] = getattr(self, key)
167 state[key] = getattr(self, key)
General Comments 0
You need to be logged in to leave comments. Login now