##// END OF EJS Templates
Use rsvp.js for Promises
Jonathan Frederic -
Show More
@@ -5,7 +5,8 b' define(['
5 'base/js/namespace',
5 'base/js/namespace',
6 'jquery',
6 'jquery',
7 'codemirror/lib/codemirror',
7 'codemirror/lib/codemirror',
8 ], function(IPython, $, CodeMirror){
8 'components/rsvp/rsvp.min',
9 ], function(IPython, $, CodeMirror, rsvp){
9 "use strict";
10 "use strict";
10
11
11 IPython.load_extensions = function () {
12 IPython.load_extensions = function () {
@@ -606,13 +607,41 b' define(['
606 });
607 });
607 };
608 };
608
609
610 var WrappedError = function(message, error){
611 // Wrappable Error class
612
613 // The Error class doesn't actually act on `this`. Instead it always
614 // returns a new instance of Error. Here we capture that instance so we
615 // can apply it's properties to `this`.
616 var tmp = Error.apply(this, [message]);
617
618 // Copy the properties of the error over to this.
619 var properties = Object.getOwnPropertyNames(tmp);
620 for (var i = 0; i < properties.length; i++) {
621 this[properties[i]] = tmp[properties[i]];
622 }
623
624 // Keep a stack of the original error messages.
625 if (error instanceof WrappedError) {
626 this.error_stack = error.error_stack;
627 } else {
628 this.error_stack = [error];
629 }
630 this.error_stack.push(tmp);
631
632 return this;
633 };
634
635 WrappedError.prototype = Object.create(Error.prototype, {});
636
637
609 var load_class = function(class_name, module_name, registry) {
638 var load_class = function(class_name, module_name, registry) {
610 // Tries to load a class
639 // Tries to load a class
611 //
640 //
612 // Tries to load a class from a module using require.js, if a module
641 // Tries to load a class from a module using require.js, if a module
613 // is specified, otherwise tries to load a class from the global
642 // is specified, otherwise tries to load a class from the global
614 // registry, if the global registry is provided.
643 // registry, if the global registry is provided.
615 return new Promise(function(resolve, reject) {
644 return new rsvp.Promise(function(resolve, reject) {
616
645
617 // Try loading the view module using require.js
646 // Try loading the view module using require.js
618 if (module_name) {
647 if (module_name) {
@@ -638,13 +667,13 b' define(['
638
667
639 var resolve_dict = function(d) {
668 var resolve_dict = function(d) {
640 // Resolve a promiseful dictionary.
669 // Resolve a promiseful dictionary.
641 // Returns a single Promise.
670 // Returns a single rsvp.Promise.
642 var keys = Object.keys(d);
671 var keys = Object.keys(d);
643 var values = [];
672 var values = [];
644 keys.forEach(function(key) {
673 keys.forEach(function(key) {
645 values.push(d[key]);
674 values.push(d[key]);
646 });
675 });
647 return Promise.all(values).then(function(v) {
676 return rsvp.Promise.all(values).then(function(v) {
648 d = {};
677 d = {};
649 for(var i=0; i<keys.length; i++) {
678 for(var i=0; i<keys.length; i++) {
650 d[keys[i]] = v[i];
679 d[keys[i]] = v[i];
@@ -681,15 +710,15 b' define(['
681 WrappedError.prototype = Object.create(Error.prototype, {});
710 WrappedError.prototype = Object.create(Error.prototype, {});
682
711
683 var reject = function(message, log) {
712 var reject = function(message, log) {
684 // Creates a wrappable Promise rejection function.
713 // Creates a wrappable rsvp.Promise rejection function.
685 //
714 //
686 // Creates a function that returns a Promise.reject with a new WrappedError
715 // Creates a function that returns a rsvp.Promise.reject with a new WrappedError
687 // that has the provided message and wraps the original error that
716 // that has the provided message and wraps the original error that
688 // caused the promise to reject.
717 // caused the promise to reject.
689 return function(error) {
718 return function(error) {
690 var wrapped_error = new WrappedError(message, error);
719 var wrapped_error = new WrappedError(message, error);
691 if (log) console.error(wrapped_error);
720 if (log) console.error(wrapped_error);
692 return Promise.reject(wrapped_error);
721 return rsvp.Promise.reject(wrapped_error);
693 };
722 };
694 };
723 };
695
724
@@ -722,9 +751,9 b' define(['
722 XHR_ERROR : XHR_ERROR,
751 XHR_ERROR : XHR_ERROR,
723 wrap_ajax_error : wrap_ajax_error,
752 wrap_ajax_error : wrap_ajax_error,
724 promising_ajax : promising_ajax,
753 promising_ajax : promising_ajax,
754 WrappedError: WrappedError,
725 load_class: load_class,
755 load_class: load_class,
726 resolve_dict: resolve_dict,
756 resolve_dict: resolve_dict,
727 WrappedError: WrappedError,
728 reject: reject,
757 reject: reject,
729 };
758 };
730
759
@@ -5,7 +5,8 b' define(['
5 'base/js/namespace',
5 'base/js/namespace',
6 'jquery',
6 'jquery',
7 'base/js/utils',
7 'base/js/utils',
8 ], function(IPython, $, utils) {
8 'components/rsvp/rsvp.min',
9 ], function(IPython, $, utils, rsvp) {
9 "use strict";
10 "use strict";
10
11
11 //-----------------------------------------------------------------------
12 //-----------------------------------------------------------------------
@@ -78,7 +79,7 b' define(['
78 comm.close();
79 comm.close();
79 that.unregister_comm(comm);
80 that.unregister_comm(comm);
80 var error = new utils.WrappedError("Exception opening new comm", e);
81 var error = new utils.WrappedError("Exception opening new comm", e);
81 return Promise.reject(error);
82 return rsvp.Promise.reject(error);
82 }
83 }
83 return comm;
84 return comm;
84 }, utils.reject('Could not open comm', true));
85 }, utils.reject('Could not open comm', true));
@@ -6,8 +6,9 b' define(['
6 "backbone",
6 "backbone",
7 "jquery",
7 "jquery",
8 "base/js/utils",
8 "base/js/utils",
9 "base/js/namespace"
9 "base/js/namespace",
10 ], function (_, Backbone, $, utils, IPython) {
10 'components/rsvp/rsvp.min',
11 ], function (_, Backbone, $, utils, IPython, rsvp) {
11 "use strict";
12 "use strict";
12 //--------------------------------------------------------------------
13 //--------------------------------------------------------------------
13 // WidgetManager class
14 // WidgetManager class
@@ -48,7 +49,7 b' define(['
48 //--------------------------------------------------------------------
49 //--------------------------------------------------------------------
49 WidgetManager.prototype.display_view = function(msg, model) {
50 WidgetManager.prototype.display_view = function(msg, model) {
50 // Displays a view for a particular model.
51 // Displays a view for a particular model.
51 return new Promise(function(resolve, reject) {
52 return new rsvp.Promise(function(resolve, reject) {
52 var cell = this.get_msg_cell(msg.parent_header.msg_id);
53 var cell = this.get_msg_cell(msg.parent_header.msg_id);
53 if (cell === null) {
54 if (cell === null) {
54 reject(new Error("Could not determine where the display" +
55 reject(new Error("Could not determine where the display" +
@@ -230,7 +231,7 b' define(['
230 }, function(error) {
231 }, function(error) {
231 delete that._models[model_id];
232 delete that._models[model_id];
232 var wrapped_error = new utils.WrappedError("Couldn't create model", error);
233 var wrapped_error = new utils.WrappedError("Couldn't create model", error);
233 return Promise.reject(wrapped_error);
234 return rsvp.Promise.reject(wrapped_error);
234 });
235 });
235 this._models[model_id] = model_promise;
236 this._models[model_id] = model_promise;
236 return model_promise;
237 return model_promise;
@@ -7,7 +7,8 b' define(["widgets/js/manager",'
7 "jquery",
7 "jquery",
8 "base/js/utils",
8 "base/js/utils",
9 "base/js/namespace",
9 "base/js/namespace",
10 ], function(widgetmanager, _, Backbone, $, utils, IPython){
10 "components/rsvp/rsvp.min",
11 ], function(widgetmanager, _, Backbone, $, utils, IPython, rsvp){
11
12
12 var WidgetModel = Backbone.Model.extend({
13 var WidgetModel = Backbone.Model.extend({
13 constructor: function (widget_manager, model_id, comm) {
14 constructor: function (widget_manager, model_id, comm) {
@@ -22,7 +23,7 b' define(["widgets/js/manager",'
22 // An ID unique to this model.
23 // An ID unique to this model.
23 // comm : Comm instance (optional)
24 // comm : Comm instance (optional)
24 this.widget_manager = widget_manager;
25 this.widget_manager = widget_manager;
25 this.state_change = Promise.resolve();
26 this.state_change = rsvp.Promise.resolve();
26 this._buffered_state_diff = {};
27 this._buffered_state_diff = {};
27 this.pending_msgs = 0;
28 this.pending_msgs = 0;
28 this.msg_buffer = null;
29 this.msg_buffer = null;
@@ -257,7 +258,7 b' define(["widgets/js/manager",'
257 _.each(value, function(sub_value, key) {
258 _.each(value, function(sub_value, key) {
258 unpacked.push(that._unpack_models(sub_value));
259 unpacked.push(that._unpack_models(sub_value));
259 });
260 });
260 return Promise.all(unpacked);
261 return rsvp.Promise.all(unpacked);
261 } else if (value instanceof Object) {
262 } else if (value instanceof Object) {
262 unpacked = {};
263 unpacked = {};
263 _.each(value, function(sub_value, key) {
264 _.each(value, function(sub_value, key) {
@@ -268,7 +269,7 b' define(["widgets/js/manager",'
268 // get_model returns a promise already
269 // get_model returns a promise already
269 return this.widget_manager.get_model(value.slice(10, value.length));
270 return this.widget_manager.get_model(value.slice(10, value.length));
270 } else {
271 } else {
271 return Promise.resolve(value);
272 return rsvp.Promise.resolve(value);
272 }
273 }
273 },
274 },
274
275
General Comments 0
You need to be logged in to leave comments. Login now