##// END OF EJS Templates
Fix bugs introduced into session.js
Jessica B. Hamrick -
Show More
@@ -1,186 +1,195
1 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3
3
4 define([
4 define([
5 'base/js/namespace',
5 'base/js/namespace',
6 'jquery',
6 'jquery',
7 'base/js/utils',
7 'base/js/utils',
8 'services/kernels/js/kernel',
8 'services/kernels/js/kernel',
9 ], function(IPython, $, utils, kernel) {
9 ], function(IPython, $, utils, kernel) {
10 "use strict";
10 "use strict";
11
11
12 var Session = function (options) {
12 var Session = function (options) {
13 this.id = null;
13 this.id = null;
14 this.notebook = {
14 this.notebook_model = {
15 name: options.notebook_name,
15 name: options.notebook_name,
16 path: options.notebook_path
16 path: options.notebook_path
17 };
17 };
18 this.kernel = {
18 this.kernel_model = {
19 id: null,
19 name: options.kernel_name
20 name: options.kernel_name
20 };
21 };
21
22
22 this.base_url = options.base_url;
23 this.base_url = options.base_url;
23 this.ws_url = options.ws_url;
24 this.ws_url = options.ws_url;
24 this.sessions_url = utils.url_join_encode(this.base_url, 'api/sessions');
25 this.sessions_url = utils.url_join_encode(this.base_url, 'api/sessions');
25
26
26 this.notebook_obj = options.notebook;
27 this.notebook = options.notebook;
28 this.kernel = null;
27 this.events = options.events;
29 this.events = options.events;
28 };
30 };
29
31
30 /**
32 /**
31 * GET /api/sessions
33 * GET /api/sessions
32 */
34 */
33 Session.prototype.list = function (success, error) {
35 Session.prototype.list = function (success, error) {
34 $.ajax(this.sessions_url, {
36 $.ajax(this.sessions_url, {
35 processData: false,
37 processData: false,
36 cache: false,
38 cache: false,
37 type: "GET",
39 type: "GET",
38 dataType: "json",
40 dataType: "json",
39 success: success,
41 success: success,
40 error: this._on_error(error)
42 error: this._on_error(error)
41 });
43 });
42 };
44 };
43
45
44 /**
46 /**
45 * POST /api/sessions
47 * POST /api/sessions
46 */
48 */
47 Session.prototype.start = function (success, error) {
49 Session.prototype.start = function (success, error) {
48 var that = this;
50 var that = this;
49 var on_success = function (data, status, xhr) {
51 var on_success = function (data, status, xhr) {
50 var kernel_service_url = utils.url_path_join(that.base_url, "api/kernels");
52 var kernel_service_url = utils.url_path_join(that.base_url, "api/kernels");
51 that.kernel = new kernel.Kernel(kernel_service_url, that.ws_url, that.notebook_obj, that.kernel_name);
53 that.kernel = new kernel.Kernel(
54 kernel_service_url, that.ws_url, that.notebook,
55 that.kernel_model.id, that.kernel_model.name);
52 that.kernel._kernel_started(data.kernel);
56 that.kernel._kernel_started(data.kernel);
53 if (success) {
57 if (success) {
54 success(data, status, xhr);
58 success(data, status, xhr);
55 }
59 }
56 };
60 };
57 var on_error = function (xhr, status, err) {
61 var on_error = function (xhr, status, err) {
58 that.events.trigger('status_dead.Kernel');
62 that.events.trigger('status_dead.Kernel');
59 if (error) {
63 if (error) {
60 error(xhr, status, err);
64 error(xhr, status, err);
61 }
65 }
62 };
66 };
63
67
64 $.ajax(this.sessions_url, {
68 $.ajax(this.sessions_url, {
65 processData: false,
69 processData: false,
66 cache: false,
70 cache: false,
67 type: "POST",
71 type: "POST",
68 data: JSON.stringify(this._get_model()),
72 data: JSON.stringify(this._get_model()),
69 dataType: "json",
73 dataType: "json",
70 success: this._on_success(on_success),
74 success: this._on_success(on_success),
71 error: this._on_error(on_error)
75 error: this._on_error(on_error)
72 });
76 });
73 };
77 };
74
78
75 /**
79 /**
76 * GET /api/sessions/[:session_id]
80 * GET /api/sessions/[:session_id]
77 */
81 */
78 Session.prototype.get_info = function (success, error) {
82 Session.prototype.get_info = function (success, error) {
79 var url = utils.url_join_encode(this.sessions_url, this.id);
83 var url = utils.url_join_encode(this.sessions_url, this.id);
80 $.ajax(url, {
84 $.ajax(url, {
81 processData: false,
85 processData: false,
82 cache: false,
86 cache: false,
83 type: "GET",
87 type: "GET",
84 dataType: "json",
88 dataType: "json",
85 success: this._on_success(success),
89 success: this._on_success(success),
86 error: this._on_error(error)
90 error: this._on_error(error)
87 });
91 });
88 };
92 };
89
93
90 /**
94 /**
91 * PATCH /api/sessions/[:session_id]
95 * PATCH /api/sessions/[:session_id]
92 */
96 */
93 Session.prototype.change = function (notebook_name, notebook_path, kernel_name, success, error) {
97 Session.prototype.change = function (notebook_name, notebook_path, kernel_name, success, error) {
94 this.notebook.name = notebook_name;
98 this.notebook_model.name = notebook_name;
95 this.notebook.path = notebook_path;
99 this.notebook_model.path = notebook_path;
96 this.kernel.name = kernel_name;
100 this.kernel_model.name = kernel_name;
97
101
98 var url = utils.url_join_encode(this.sessions_url, this.id);
102 var url = utils.url_join_encode(this.sessions_url, this.id);
99 $.ajax(url, {
103 $.ajax(url, {
100 processData: false,
104 processData: false,
101 cache: false,
105 cache: false,
102 type: "PATCH",
106 type: "PATCH",
103 data: JSON.stringify(this._get_model()),
107 data: JSON.stringify(this._get_model()),
104 dataType : "json",
108 dataType : "json",
105 success: this._on_success(success),
109 success: this._on_success(success),
106 error: this._on_error(error)
110 error: this._on_error(error)
107 });
111 });
108 };
112 };
109
113
110 Session.prototype.rename_notebook = function (name, path, success, error) {
114 Session.prototype.rename_notebook = function (name, path, success, error) {
111 this.change(name, path, this.kernel.name, success, error);
115 this.change(name, path, this.kernel_model.name, success, error);
112 };
116 };
113
117
114 /**
118 /**
115 * DELETE /api/sessions/[:session_id]
119 * DELETE /api/sessions/[:session_id]
116 */
120 */
117 Session.prototype.kill = function (success, error) {
121 Session.prototype.delete = function (success, error) {
118 if (this.kernel) {
122 var that = this;
119 this.kernel.running = false;
123 var on_success = function (data, status, xhr) {
120 this.kernel.stop_channels();
124 if (that.kernel) {
121 }
125 that.kernel._kernel_dead();
126 }
127 };
122
128
123 var url = utils.url_join_encode(this.sessions_url, this.id);
129 var url = utils.url_join_encode(this.sessions_url, this.id);
124 $.ajax(url, {
130 $.ajax(url, {
125 processData: false,
131 processData: false,
126 cache: false,
132 cache: false,
127 type: "DELETE",
133 type: "DELETE",
128 dataType: "json",
134 dataType: "json",
129 success: this._on_success(success),
135 success: this._on_success(on_success),
130 error: this._on_error(error)
136 error: this._on_error(error)
131 });
137 });
132 };
138 };
133
139
134 Session.prototype._get_model = function () {
140 Session.prototype._get_model = function () {
135 return {
141 return {
136 notebook: this.notebook,
142 notebook: this.notebook_model,
137 kernel: this.kernel
143 kernel: this.kernel_model
138 };
144 };
139 };
145 };
140
146
141 Session.prototype._update_model = function (data) {
147 Session.prototype._update_model = function (data) {
142 this.id = data.id;
148 if (data && data.id) {
143 if (data.notebook) {
149 this.id = data.id;
144 this.notebook.name = data.notebook.name;
150 }
145 this.notebook.path = data.notebook.path;
151 if (data && data.notebook) {
152 this.notebook_model.name = data.notebook.name;
153 this.notebook_model.path = data.notebook.path;
146 }
154 }
147 if (data.kernel) {
155 if (data && data.kernel) {
148 this.kernel.name = data.kernel.name;
156 this.kernel_model.name = data.kernel.name;
157 this.kernel_model.id = data.kernel.id;
149 }
158 }
150 };
159 };
151
160
152 Session.prototype._on_success = function (success) {
161 Session.prototype._on_success = function (success) {
153 var that = this;
162 var that = this;
154 return function (data, status, xhr) {
163 return function (data, status, xhr) {
155 that._update_model(data);
164 that._update_model(data);
156 if (success) {
165 if (success) {
157 success(data, status, xhr);
166 success(data, status, xhr);
158 }
167 }
159 };
168 };
160 };
169 };
161
170
162 Session.prototype._on_error = function (error) {
171 Session.prototype._on_error = function (error) {
163 return function (xhr, status, err) {
172 return function (xhr, status, err) {
164 utils.log_ajax_error(xhr, status, err);
173 utils.log_ajax_error(xhr, status, err);
165 if (error) {
174 if (error) {
166 error(xhr, status, err);
175 error(xhr, status, err);
167 }
176 }
168 };
177 };
169 };
178 };
170
179
171
180
172 var SessionAlreadyStarting = function (message) {
181 var SessionAlreadyStarting = function (message) {
173 this.name = "SessionAlreadyStarting";
182 this.name = "SessionAlreadyStarting";
174 this.message = (message || "");
183 this.message = (message || "");
175 };
184 };
176
185
177 SessionAlreadyStarting.prototype = Error.prototype;
186 SessionAlreadyStarting.prototype = Error.prototype;
178
187
179 // For backwards compatability.
188 // For backwards compatability.
180 IPython.Session = Session;
189 IPython.Session = Session;
181
190
182 return {
191 return {
183 Session: Session,
192 Session: Session,
184 SessionAlreadyStarting: SessionAlreadyStarting
193 SessionAlreadyStarting: SessionAlreadyStarting
185 };
194 };
186 });
195 });
General Comments 0
You need to be logged in to leave comments. Login now