Introduce a Plugin system, implement <py-terminal> as a plugin (#917)

This PR does two things:

1. introduce the first minimal version of a Plugin system. Plugins are subclasses of the Plugin class, and pyscript will call the relevant methods/hooks at the right time. Currently, I have added only the minimal sets of hooks which were needed for this PR.

2. Implement <py-terminal> as a plugin.
This commit is contained in:
Antonio Cuni
2022-11-09 16:17:21 +01:00
committed by GitHub
parent f0a6fb913f
commit 0d3c3eef4e
10 changed files with 532 additions and 13 deletions

70
pyscriptjs/src/plugin.ts Normal file
View File

@@ -0,0 +1,70 @@
import type { PyScriptApp } from './main';
import type { AppConfig } from './pyconfig';
import type { Runtime } from './runtime';
export class Plugin {
/** Validate the configuration of the plugin and handle default values.
*
* Individual plugins are expected to check that the config keys/sections
* which are relevant to them contains valid values, and to raise an error
* if they contains unknown keys.
*
* This is also a good place where set default values for those keys which
* are not specified by the user.
*
* This hook should **NOT** contain expensive operations, else it delays
* the download of the python interpreter which is initiated later.
*/
configure(config: AppConfig) {
}
/** The preliminary initialization phase is complete and we are about to
* download and launch the Python interpreter.
*
* We can assume that the page is already shown to the user and that the
* DOM content has been loaded. This is a good place where to add tags to
* the DOM, if needed.
*
* This hook should **NOT** contain expensive operations, else it delays
* the download of the python interpreter which is initiated later.
*/
beforeLaunch(config: AppConfig) {
}
/** The Python interpreter has been launched, the virtualenv has been
* installed and we are ready to execute user code.
*
* The <py-script> tags will be executed after this hook.
*/
afterSetup(runtime: Runtime) {
}
}
export class PluginManager {
_plugins: Plugin[];
constructor() {
this._plugins = [];
}
add(p: Plugin) {
this._plugins.push(p);
}
configure(config: AppConfig) {
for (const p of this._plugins)
p.configure(config);
}
beforeLaunch(config: AppConfig) {
for (const p of this._plugins)
p.beforeLaunch(config);
}
afterSetup(runtime: Runtime) {
for (const p of this._plugins)
p.afterSetup(runtime);
}
}