Add state packet delta compression.

This commit is contained in:
Jonathan Frederic 2013-10-22 17:30:30 +00:00
parent d72262e071
commit 0d25759d53
2 changed files with 27 additions and 9 deletions

View File

@ -32,7 +32,7 @@ define(["static/components/underscore/underscore-min.js",
//-------------------------------------------------------------------- //--------------------------------------------------------------------
var WidgetModel = Backbone.Model.extend({ var WidgetModel = Backbone.Model.extend({
apply: function(sender) { apply: function(sender) {
this.save(); this.save(this.changedAttributes(), {patch: true});
for (var index in this.views) { for (var index in this.views) {
var view = this.views[index]; var view = this.views[index];
@ -95,7 +95,7 @@ define(["static/components/underscore/underscore-min.js",
var that = this; var that = this;
Backbone.sync = function(method, model, options, error) { Backbone.sync = function(method, model, options, error) {
var result = that.send_sync(method, model); var result = that.send_sync(method, model, options);
if (options.success) { if (options.success) {
options.success(result); options.success(result);
} }
@ -214,6 +214,7 @@ define(["static/components/underscore/underscore-min.js",
} }
} }
} }
comm.model.id = comm.comm_id;
comm.model.save(); comm.model.save();
this.updating = false; this.updating = false;
} }
@ -234,7 +235,7 @@ define(["static/components/underscore/underscore-min.js",
} }
// Send widget state to python backend. // Send widget state to python backend.
WidgetManager.prototype.send_sync = function (method, model) { WidgetManager.prototype.send_sync = function (method, model, options) {
var model_json = model.toJSON(); var model_json = model.toJSON();
// Only send updated state if the state hasn't been changed during an update. // Only send updated state if the state hasn't been changed during an update.
@ -250,10 +251,21 @@ define(["static/components/underscore/underscore-min.js",
clear_output : $.proxy(outputarea.handle_clear_output, outputarea)} clear_output : $.proxy(outputarea.handle_clear_output, outputarea)}
}; };
}; };
var data = {sync_method: method, sync_data: model_json};
// If this is a patch operation, just send the changes.
var send_json = model_json;
if (method=='patch') {
send_json = {};
for (var attr in options.attrs) {
send_json[attr] = options.attrs[attr];
}
}
var data = {sync_method: method, sync_data: send_json};
comm.send(data, callbacks); comm.send(data, callbacks);
} }
// Since the comm is a one-way communication, assume the message
// arrived.
return model_json; return model_json;
} }

View File

@ -96,11 +96,10 @@ class Widget(LoggingConfigurable):
### Event handlers ### Event handlers
def _handle_msg(self, msg): def _handle_msg(self, msg):
# Handle backbone sync methods # Handle backbone sync methods CREATE, PATCH, and UPDATE
sync_method = msg['content']['data']['sync_method'] sync_method = msg['content']['data']['sync_method']
sync_data = msg['content']['data']['sync_data'] sync_data = msg['content']['data']['sync_data']
if sync_method.lower() in ['create', 'update']: self._handle_recieve_state(sync_data) # handles all methods
self._handle_recieve_state(sync_data)
def _handle_recieve_state(self, sync_data): def _handle_recieve_state(self, sync_data):
@ -119,7 +118,7 @@ class Widget(LoggingConfigurable):
if not self._property_lock and self.comm is not None: if not self._property_lock and self.comm is not None:
# TODO: Validate properties. # TODO: Validate properties.
# Send new state to frontend # Send new state to frontend
self.send_state() self.send_state(key=name)
def _handle_close(self): def _handle_close(self):
@ -154,8 +153,15 @@ class Widget(LoggingConfigurable):
return None return None
def send_state(self): def send_state(self, key=None):
state = {} state = {}
# If a key is provided, just send the state of that key.
keys = []
if key is None:
keys.extend(self.keys)
else:
keys.append(key)
for key in self.keys: for key in self.keys:
try: try:
state[key] = getattr(self, key) state[key] = getattr(self, key)