Commit 665261d5 authored by Romain Casati's avatar Romain Casati
Browse files

Better initialisation API.

parent 0067b705
......@@ -77,7 +77,7 @@ class SeleniumWrapper:
)
self.driver.implicitly_wait(30)
self.driver.get(f"http://{server_hostname}:{server_port}/test.html")
self.run_js("await Basthon.load")
self.run_js("await Basthon.loaded();")
self.run_js("Error.stackTraceLimit = Infinity")
# connecting Basthon's test listeners (for run_basthon)
......
......@@ -25,12 +25,13 @@
*/
return class {
constructor() {
/** Is the kernel loaded? */
this.loaded = false;
/** The current script URL. */
this. urlScript = (function () {
/**
* Path to current script.
*
* Warning: this should not be postponed since
* document.currentScript will be undefined.
*/
this._urlScript = (function () {
if( module != null ) {
let url = window.location.origin + window.location.pathname;
url = url.substring(0, url.lastIndexOf('/')) + "/";
......@@ -38,18 +39,7 @@
} else
return document.currentScript.src;
})();
/** Promise that resolve when the page is loaded. */
this.pageLoad = this._pageLoad();
/** Promie that resolve when the kernel is loaded. */
this.load = this.launch().then( () => {
this.loaded = true;
// connecting eval to basthon.eval.request event.
this.addEventListener("eval.request",
this.evalFromEvent.bind(this));
});
}
};
/**
* Kernel version number (string).
......@@ -57,7 +47,12 @@
version () { throw new NotImplementedError("version"); };
/**
*
* Language implemented in the kernel (string).
*/
language () { throw new NotImplementedError("language"); };
/**
* Launch the kernel.
*/
async launch () { throw new NotImplementedError("launch"); };
......@@ -96,6 +91,28 @@
complete (code) { return []; };
/**
* Initialize the kernel.
*/
init () {
const that = this;
return this._loaded = this.launch().then(function () {
// connecting eval to basthon.eval.request event.
that.addEventListener("eval.request",
that.evalFromEvent.bind(that));
});
};
/**
* Promise that resolve when the kernel is loaded.
*/
loaded () { return this._loaded ?? this.init(); };
/**
* The current script URL.
*/
urlScript () { return this._urlScript; };
/**
* An URL without it's basename.
*/
......@@ -108,7 +125,7 @@
* directory inside the current script directory.
*/
basthonRoot () {
return this.dirname(this.urlScript) + "/" + this.version();
return this.dirname(this.urlScript()) + "/" + this.version();
};
/**
......@@ -143,18 +160,19 @@
/**
* Returns a promise that resolves when page is loaded.
*/
_pageLoad () {
return new Promise(function (resolve, reject) {
if( document.readyState === 'complete' ) {
resolve();
} else {
window.addEventListener("load", function() {
// just to be safe
window.removeEventListener("load", this);
pageLoad () {
return this._pageLoad ?? (this._pageLoad = new Promise(
function (resolve, reject) {
if( document.readyState === 'complete' ) {
resolve();
});
}
});
} else {
window.addEventListener("load", function() {
// just to be safe
window.removeEventListener("load", this);
resolve();
});
}
}));
};
/**
......@@ -223,8 +241,6 @@
* Internal. Code evaluation after an eval.request event.
*/
evalFromEvent (data) {
if( !this.loaded ) { return ; }
const that = this;
let stdCallback = function (std) { return function (text) {
......
......@@ -24,6 +24,9 @@
* If you want to manually hide the loader, set hideAfter to false.
*/
that.showLoader = async function (text, hideAfter=true) {
// launch kernel init
Basthon.init();
// dynamically adding the loader to the DOM
const root = document.createElement("div");
root.id = that.loaderId;
......@@ -87,7 +90,7 @@
that._rootLoader = root;
// waiting for Basthon
await Basthon.load;
await Basthon.loaded();
// hide the loader if requested
if( hideAfter ) {
......
......@@ -13,7 +13,7 @@
'use strict';
/**
*
* A Python kernel that satisfies Basthon's API.
*/
class BasthonPython extends BasthonBase {
......@@ -32,15 +32,17 @@
* Where to find pyodide.js (private).
*/
this._pyodideUrl = "https://cdn.jsdelivr.net/pyodide/v0.16.1/full/pyodide.js";
/**
* Get the URL of Basthon modules dir.
*/
this.basthonModulesRoot = this.basthonRoot() + "/modules";
}
/**
* Get the URL of Basthon modules dir.
*/
basthonModulesRoot () { return this.basthonRoot() + "/modules"; };
version () { return this.__version__; };
language () { return "Python"; };
/**
* What to do when loaded (private).
*/
......@@ -95,14 +97,13 @@
function() { console.error("Can't load Python from Pyodide"); });
// waiting until page is loaded
await this.pageLoad;
await this.pageLoad();
};
/**
* Basthon async code evaluation function.
*/
async evalAsync (code, outCallback, errCallback, data=null) {
if( !this.loaded ) { return ; }
if( typeof outCallback === 'undefined' ) {
outCallback = function (text) { console.log(text); };
}
......@@ -117,7 +118,6 @@
* Restart the kernel.
*/
restart () {
if( !this.loaded ) { return ; }
return this.__kernel__.restart();
};
......
......@@ -27,7 +27,7 @@ _import_name_to_package_name = {k: _import_name_to_package_name[k]
_pyodide_pkgs = set(_import_name_to_package_name.keys())
# Root path to internal modules
_internal_modules_path = js.Basthon.basthonModulesRoot
_internal_modules_path = js.Basthon.basthonModulesRoot()
# Packages not implemented in Pyodide but in Basthon (internal)
# (dict pointing to Pypi or internal addresse).
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment