##// END OF EJS Templates
Use rsvp.js for Promises
Jonathan Frederic -
Show More
@@ -5,7 +5,8 b' define(['
5 5 'base/js/namespace',
6 6 'jquery',
7 7 'codemirror/lib/codemirror',
8 ], function(IPython, $, CodeMirror){
8 'components/rsvp/rsvp.min',
9 ], function(IPython, $, CodeMirror, rsvp){
9 10 "use strict";
10 11
11 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 638 var load_class = function(class_name, module_name, registry) {
610 639 // Tries to load a class
611 640 //
612 641 // Tries to load a class from a module using require.js, if a module
613 642 // is specified, otherwise tries to load a class from the global
614 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 646 // Try loading the view module using require.js
618 647 if (module_name) {
@@ -638,13 +667,13 b' define(['
638 667
639 668 var resolve_dict = function(d) {
640 669 // Resolve a promiseful dictionary.
641 // Returns a single Promise.
670 // Returns a single rsvp.Promise.
642 671 var keys = Object.keys(d);
643 672 var values = [];
644 673 keys.forEach(function(key) {
645 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 677 d = {};
649 678 for(var i=0; i<keys.length; i++) {
650 679 d[keys[i]] = v[i];
@@ -681,15 +710,15 b' define(['
681 710 WrappedError.prototype = Object.create(Error.prototype, {});
682 711
683 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 716 // that has the provided message and wraps the original error that
688 717 // caused the promise to reject.
689 718 return function(error) {
690 719 var wrapped_error = new WrappedError(message, error);
691 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 751 XHR_ERROR : XHR_ERROR,
723 752 wrap_ajax_error : wrap_ajax_error,
724 753 promising_ajax : promising_ajax,
754 WrappedError: WrappedError,
725 755 load_class: load_class,
726 756 resolve_dict: resolve_dict,
727 WrappedError: WrappedError,
728 757 reject: reject,
729 758 };
730 759
@@ -5,7 +5,8 b' define(['
5 5 'base/js/namespace',
6 6 'jquery',
7 7 'base/js/utils',
8 ], function(IPython, $, utils) {
8 'components/rsvp/rsvp.min',
9 ], function(IPython, $, utils, rsvp) {
9 10 "use strict";
10 11
11 12 //-----------------------------------------------------------------------
@@ -78,7 +79,7 b' define(['
78 79 comm.close();
79 80 that.unregister_comm(comm);
80 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 84 return comm;
84 85 }, utils.reject('Could not open comm', true));
@@ -6,8 +6,9 b' define(['
6 6 "backbone",
7 7 "jquery",
8 8 "base/js/utils",
9 "base/js/namespace"
10 ], function (_, Backbone, $, utils, IPython) {
9 "base/js/namespace",
10 'components/rsvp/rsvp.min',
11 ], function (_, Backbone, $, utils, IPython, rsvp) {
11 12 "use strict";
12 13 //--------------------------------------------------------------------
13 14 // WidgetManager class
@@ -48,7 +49,7 b' define(['
48 49 //--------------------------------------------------------------------
49 50 WidgetManager.prototype.display_view = function(msg, model) {
50 51 // Displays a view for a particular model.
51 return new Promise(function(resolve, reject) {
52 return new rsvp.Promise(function(resolve, reject) {
52 53 var cell = this.get_msg_cell(msg.parent_header.msg_id);
53 54 if (cell === null) {
54 55 reject(new Error("Could not determine where the display" +
@@ -230,7 +231,7 b' define(['
230 231 }, function(error) {
231 232 delete that._models[model_id];
232 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 236 this._models[model_id] = model_promise;
236 237 return model_promise;
@@ -7,7 +7,8 b' define(["widgets/js/manager",'
7 7 "jquery",
8 8 "base/js/utils",
9 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 13 var WidgetModel = Backbone.Model.extend({
13 14 constructor: function (widget_manager, model_id, comm) {
@@ -22,7 +23,7 b' define(["widgets/js/manager",'
22 23 // An ID unique to this model.
23 24 // comm : Comm instance (optional)
24 25 this.widget_manager = widget_manager;
25 this.state_change = Promise.resolve();
26 this.state_change = rsvp.Promise.resolve();
26 27 this._buffered_state_diff = {};
27 28 this.pending_msgs = 0;
28 29 this.msg_buffer = null;
@@ -257,7 +258,7 b' define(["widgets/js/manager",'
257 258 _.each(value, function(sub_value, key) {
258 259 unpacked.push(that._unpack_models(sub_value));
259 260 });
260 return Promise.all(unpacked);
261 return rsvp.Promise.all(unpacked);
261 262 } else if (value instanceof Object) {
262 263 unpacked = {};
263 264 _.each(value, function(sub_value, key) {
@@ -268,7 +269,7 b' define(["widgets/js/manager",'
268 269 // get_model returns a promise already
269 270 return this.widget_manager.get_model(value.slice(10, value.length));
270 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