##// END OF EJS Templates
Clean up promises code some more.
Jason Grout -
Show More
@@ -70,14 +70,10 b' define(['
70
70
71 this.comms[comm_id] = utils.load_class(content.target_name, content.target_module,
71 this.comms[comm_id] = utils.load_class(content.target_name, content.target_module,
72 this.targets).then(function(target) {
72 this.targets).then(function(target) {
73
74 var comm = new Comm(content.target_name, comm_id);
73 var comm = new Comm(content.target_name, comm_id);
75 comm.kernel = that.kernel;
74 comm.kernel = that.kernel;
76 try {
75 try {
77 var response = target(comm, msg);
76 var response = target(comm, msg);
78 if (response instanceof Promise) {
79 return response.then(function() { return Promise.resolve(comm); });
80 }
81 } catch (e) {
77 } catch (e) {
82 comm.close();
78 comm.close();
83 that.unregister_comm(comm);
79 that.unregister_comm(comm);
@@ -85,7 +81,9 b' define(['
85 console.error(wrapped_error);
81 console.error(wrapped_error);
86 return Promise.reject(wrapped_error);
82 return Promise.reject(wrapped_error);
87 }
83 }
88 return Promise.resolve(comm);
84 // Regardless of the target return value, we need to
85 // then return the comm
86 return Promise.resolve(response).then(function() {return comm;});
89 }, utils.reject('Could not open comm', true));
87 }, utils.reject('Could not open comm', true));
90 return this.comms[comm_id];
88 return this.comms[comm_id];
91 };
89 };
@@ -104,6 +102,8 b' define(['
104 } catch (e) {
102 } catch (e) {
105 console.log("Exception closing comm: ", e, e.stack, msg);
103 console.log("Exception closing comm: ", e, e.stack, msg);
106 }
104 }
105 // don't return a comm, so that further .then() functions
106 // get an undefined comm input
107 });
107 });
108 };
108 };
109
109
@@ -120,7 +120,7 b' define(['
120 } catch (e) {
120 } catch (e) {
121 console.log("Exception handling comm msg: ", e, e.stack, msg);
121 console.log("Exception handling comm msg: ", e, e.stack, msg);
122 }
122 }
123 return Promise.resolve(comm);
123 return comm;
124 });
124 });
125 };
125 };
126
126
@@ -49,24 +49,24 b' define(['
49 WidgetManager.prototype.display_view = function(msg, model) {
49 WidgetManager.prototype.display_view = function(msg, model) {
50 // Displays a view for a particular model.
50 // Displays a view for a particular model.
51 var that = this;
51 var that = this;
52 return new Promise(function(resolve, reject) {
52 var cell = this.get_msg_cell(msg.parent_header.msg_id);
53 var cell = that.get_msg_cell(msg.parent_header.msg_id);
54 if (cell === null) {
53 if (cell === null) {
55 reject(new Error("Could not determine where the display" +
54 return Promise.reject(new Error("Could not determine where the display" +
56 " message was from. Widget will not be displayed"));
55 " message was from. Widget will not be displayed"));
57 } else if (cell.widget_subarea) {
56 } else if (cell.widget_subarea) {
58 var dummy = $('<div />');
57 var dummy = $('<div />');
59 cell.widget_subarea.append(dummy);
58 cell.widget_subarea.append(dummy);
60 that.create_view(model, {cell: cell}).then(function(view) {
59 return this.create_view(model, {cell: cell}).then(
60 function(view) {
61 that._handle_display_view(view);
61 that._handle_display_view(view);
62 dummy.replaceWith(view.$el);
62 dummy.replaceWith(view.$el);
63 view.trigger('displayed');
63 view.trigger('displayed');
64 resolve(view);
64 return view;
65 }, function(error) {
65 },
66 reject(new utils.WrappedError('Could not display view', error));
66 function(error) {
67 return Promise.reject(new utils.WrappedError('Could not display view', error));
67 });
68 });
68 }
69 }
69 });
70 };
70 };
71
71
72 WidgetManager.prototype._handle_display_view = function (view) {
72 WidgetManager.prototype._handle_display_view = function (view) {
@@ -95,7 +95,6 b' define(["widgets/js/manager",'
95 } finally {
95 } finally {
96 that.state_lock = null;
96 that.state_lock = null;
97 }
97 }
98 return Promise.resolve();
99 }, utils.reject("Couldn't set model state", true));
98 }, utils.reject("Couldn't set model state", true));
100 },
99 },
101
100
General Comments 0
You need to be logged in to leave comments. Login now