Use rsvp.js for Promises

pull/37/head
Jonathan Frederic 12 years ago
parent c67dcc0dc3
commit 3870bb571b

@ -5,7 +5,8 @@ define([
'base/js/namespace',
'jquery',
'codemirror/lib/codemirror',
], function(IPython, $, CodeMirror){
'components/rsvp/rsvp.min',
], function(IPython, $, CodeMirror, rsvp){
"use strict";
IPython.load_extensions = function () {
@ -606,13 +607,41 @@ define([
});
};
var WrappedError = function(message, error){
// Wrappable Error class
// The Error class doesn't actually act on `this`. Instead it always
// returns a new instance of Error. Here we capture that instance so we
// can apply it's properties to `this`.
var tmp = Error.apply(this, [message]);
// Copy the properties of the error over to this.
var properties = Object.getOwnPropertyNames(tmp);
for (var i = 0; i < properties.length; i++) {
this[properties[i]] = tmp[properties[i]];
}
// Keep a stack of the original error messages.
if (error instanceof WrappedError) {
this.error_stack = error.error_stack;
} else {
this.error_stack = [error];
}
this.error_stack.push(tmp);
return this;
};
WrappedError.prototype = Object.create(Error.prototype, {});
var load_class = function(class_name, module_name, registry) {
// Tries to load a class
//
// Tries to load a class from a module using require.js, if a module
// is specified, otherwise tries to load a class from the global
// registry, if the global registry is provided.
return new Promise(function(resolve, reject) {
return new rsvp.Promise(function(resolve, reject) {
// Try loading the view module using require.js
if (module_name) {
@ -638,13 +667,13 @@ define([
var resolve_dict = function(d) {
// Resolve a promiseful dictionary.
// Returns a single Promise.
// Returns a single rsvp.Promise.
var keys = Object.keys(d);
var values = [];
keys.forEach(function(key) {
values.push(d[key]);
});
return Promise.all(values).then(function(v) {
return rsvp.Promise.all(values).then(function(v) {
d = {};
for(var i=0; i<keys.length; i++) {
d[keys[i]] = v[i];
@ -681,15 +710,15 @@ define([
WrappedError.prototype = Object.create(Error.prototype, {});
var reject = function(message, log) {
// Creates a wrappable Promise rejection function.
// Creates a wrappable rsvp.Promise rejection function.
//
// Creates a function that returns a Promise.reject with a new WrappedError
// Creates a function that returns a rsvp.Promise.reject with a new WrappedError
// that has the provided message and wraps the original error that
// caused the promise to reject.
return function(error) {
var wrapped_error = new WrappedError(message, error);
if (log) console.error(wrapped_error);
return Promise.reject(wrapped_error);
return rsvp.Promise.reject(wrapped_error);
};
};
@ -722,9 +751,9 @@ define([
XHR_ERROR : XHR_ERROR,
wrap_ajax_error : wrap_ajax_error,
promising_ajax : promising_ajax,
WrappedError: WrappedError,
load_class: load_class,
resolve_dict: resolve_dict,
WrappedError: WrappedError,
reject: reject,
};

@ -5,7 +5,8 @@ define([
'base/js/namespace',
'jquery',
'base/js/utils',
], function(IPython, $, utils) {
'components/rsvp/rsvp.min',
], function(IPython, $, utils, rsvp) {
"use strict";
//-----------------------------------------------------------------------
@ -78,7 +79,7 @@ define([
comm.close();
that.unregister_comm(comm);
var error = new utils.WrappedError("Exception opening new comm", e);
return Promise.reject(error);
return rsvp.Promise.reject(error);
}
return comm;
}, utils.reject('Could not open comm', true));

@ -6,8 +6,9 @@ define([
"backbone",
"jquery",
"base/js/utils",
"base/js/namespace"
], function (_, Backbone, $, utils, IPython) {
"base/js/namespace",
'components/rsvp/rsvp.min',
], function (_, Backbone, $, utils, IPython, rsvp) {
"use strict";
//--------------------------------------------------------------------
// WidgetManager class
@ -48,7 +49,7 @@ define([
//--------------------------------------------------------------------
WidgetManager.prototype.display_view = function(msg, model) {
// Displays a view for a particular model.
return new Promise(function(resolve, reject) {
return new rsvp.Promise(function(resolve, reject) {
var cell = this.get_msg_cell(msg.parent_header.msg_id);
if (cell === null) {
reject(new Error("Could not determine where the display" +
@ -230,7 +231,7 @@ define([
}, function(error) {
delete that._models[model_id];
var wrapped_error = new utils.WrappedError("Couldn't create model", error);
return Promise.reject(wrapped_error);
return rsvp.Promise.reject(wrapped_error);
});
this._models[model_id] = model_promise;
return model_promise;

@ -7,7 +7,8 @@ define(["widgets/js/manager",
"jquery",
"base/js/utils",
"base/js/namespace",
], function(widgetmanager, _, Backbone, $, utils, IPython){
"components/rsvp/rsvp.min",
], function(widgetmanager, _, Backbone, $, utils, IPython, rsvp){
var WidgetModel = Backbone.Model.extend({
constructor: function (widget_manager, model_id, comm) {
@ -22,7 +23,7 @@ define(["widgets/js/manager",
// An ID unique to this model.
// comm : Comm instance (optional)
this.widget_manager = widget_manager;
this.state_change = Promise.resolve();
this.state_change = rsvp.Promise.resolve();
this._buffered_state_diff = {};
this.pending_msgs = 0;
this.msg_buffer = null;
@ -257,7 +258,7 @@ define(["widgets/js/manager",
_.each(value, function(sub_value, key) {
unpacked.push(that._unpack_models(sub_value));
});
return Promise.all(unpacked);
return rsvp.Promise.all(unpacked);
} else if (value instanceof Object) {
unpacked = {};
_.each(value, function(sub_value, key) {
@ -268,7 +269,7 @@ define(["widgets/js/manager",
// get_model returns a promise already
return this.widget_manager.get_model(value.slice(10, value.length));
} else {
return Promise.resolve(value);
return rsvp.Promise.resolve(value);
}
},

Loading…
Cancel
Save