mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-21 11:15:36 -05:00
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:
70
pyscriptjs/src/plugin.ts
Normal file
70
pyscriptjs/src/plugin.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user