define([
'base/js/namespace',
'jquery',
'base/js/utils',
'services/kernels/kernel',
], function(IPython, $, utils, kernel) {
"use strict";
var Session = function (options) {
this.id = null;
this.notebook_model = {
path: options.notebook_path
};
this.kernel_model = {
id: null,
name: options.kernel_name
};
this.base_url = options.base_url;
this.ws_url = options.ws_url;
this.session_service_url = utils.url_join_encode(this.base_url, 'api/sessions');
this.session_url = null;
this.notebook = options.notebook;
this.kernel = null;
this.events = options.notebook.events;
this.bind_events();
};
Session.prototype.bind_events = function () {
var that = this;
var record_status = function (evt, info) {
console.log('Session: ' + evt.type + ' (' + info.session.id + ')');
};
this.events.on('kernel_created.Session', record_status);
this.events.on('kernel_dead.Session', record_status);
this.events.on('kernel_killed.Session', record_status);
this.events.on('kernel_dead.Kernel', function () {
that.delete();
});
};
Session.prototype.list = function (success, error) {
$.ajax(this.session_service_url, {
processData: false,
cache: false,
type: "GET",
dataType: "json",
success: success,
error: this._on_error(error)
});
};
Session.prototype.start = function (success, error) {
var that = this;
var on_success = function (data, status, xhr) {
if (that.kernel) {
that.kernel.name = that.kernel_model.name;
} else {
var kernel_service_url = utils.url_path_join(that.base_url, "api/kernels");
that.kernel = new kernel.Kernel(kernel_service_url, that.ws_url, that.notebook, that.kernel_model.name);
}
that.events.trigger('kernel_created.Session', {session: that, kernel: that.kernel});
that.kernel._kernel_created(data.kernel);
if (success) {
success(data, status, xhr);
}
};
var on_error = function (xhr, status, err) {
that.events.trigger('kernel_dead.Session', {session: that, xhr: xhr, status: status, error: err});
if (error) {
error(xhr, status, err);
}
};
$.ajax(this.session_service_url, {
processData: false,
cache: false,
type: "POST",
data: JSON.stringify(this._get_model()),
dataType: "json",
success: this._on_success(on_success),
error: this._on_error(on_error)
});
};
Session.prototype.get_info = function (success, error) {
$.ajax(this.session_url, {
processData: false,
cache: false,
type: "GET",
dataType: "json",
success: this._on_success(success),
error: this._on_error(error)
});
};
Session.prototype.rename_notebook = function (path, success, error) {
if (path !== undefined) {
this.notebook_model.path = path;
}
$.ajax(this.session_url, {
processData: false,
cache: false,
type: "PATCH",
data: JSON.stringify(this._get_model()),
dataType: "json",
success: this._on_success(success),
error: this._on_error(error)
});
};
Session.prototype.delete = function (success, error) {
if (this.kernel) {
this.events.trigger('kernel_killed.Session', {session: this, kernel: this.kernel});
this.kernel._kernel_dead();
}
$.ajax(this.session_url, {
processData: false,
cache: false,
type: "DELETE",
dataType: "json",
success: this._on_success(success),
error: this._on_error(error)
});
};
Session.prototype.restart = function (options, success, error) {
var that = this;
var start = function () {
if (options && options.notebook_path) {
that.notebook_model.path = options.notebook_path;
}
if (options && options.kernel_name) {
that.kernel_model.name = options.kernel_name;
}
that.kernel_model.id = null;
that.start(success, error);
};
this.delete(start, start);
};
Session.prototype._get_model = function () {
return {
notebook: this.notebook_model,
kernel: this.kernel_model
};
};
Session.prototype._update_model = function (data) {
if (data && data.id) {
this.id = data.id;
this.session_url = utils.url_join_encode(this.session_service_url, this.id);
}
if (data && data.notebook) {
this.notebook_model.path = data.notebook.path;
}
if (data && data.kernel) {
this.kernel_model.name = data.kernel.name;
this.kernel_model.id = data.kernel.id;
}
};
Session.prototype._on_success = function (success) {
var that = this;
return function (data, status, xhr) {
that._update_model(data);
if (success) {
success(data, status, xhr);
}
};
};
Session.prototype._on_error = function (error) {
return function (xhr, status, err) {
utils.log_ajax_error(xhr, status, err);
if (error) {
error(xhr, status, err);
}
};
};
var SessionAlreadyStarting = function (message) {
this.name = "SessionAlreadyStarting";
this.message = (message || "");
};
SessionAlreadyStarting.prototype = Error.prototype;
IPython.Session = Session;
return {
Session: Session,
SessionAlreadyStarting: SessionAlreadyStarting
};
});