mirror of
https://github.com/pyscript/pyscript.git
synced 2026-03-11 13:00:17 -04:00
Make plugin methods optional (#1134)
* Make plugin methods optional using optional chaining on all methods.
This commit is contained in:
@@ -120,11 +120,7 @@ Writing plugins in Python is an excellent way if you want to use PyScript's API'
|
||||
|
||||
## Javascript plugins
|
||||
|
||||
Javascript plugins need to have a specific structure to be loaded by PyScript. The plugin must also export a default class with the following method:
|
||||
|
||||
- `afterStartup(runtime)`: This method is called after the plugin has been loaded and the runtime has been initialized.
|
||||
|
||||
Note that the `afterStartup` method must accept a single argument, but you can name it whatever you want. For Javascript plugins, this argument is not in use.
|
||||
Javascript plugins need to have a specific structure to be loaded by PyScript. The plugin export a default class with the following method, which may implement any, all, or none of the [Plugin lifecycle methods](https://github.com/pyscript/pyscript/blob/main/pyscriptjs/src/plugin.ts#L9-L65). These method will be called at the corresponding points in lifecycle of PyScript as it loads, configures itself and its Python interpreter, and executes `<py-script>` and `<py-repl>` tags.
|
||||
|
||||
```{note}
|
||||
You need to specify the file extension `.js` when adding your custom plugin to the `<py-config>` tag.
|
||||
|
||||
@@ -89,7 +89,7 @@ export class PluginManager {
|
||||
}
|
||||
|
||||
configure(config: AppConfig) {
|
||||
for (const p of this._plugins) p.configure(config);
|
||||
for (const p of this._plugins) p.configure?.(config);
|
||||
|
||||
for (const p of this._pythonPlugins) p.configure?.(config);
|
||||
}
|
||||
@@ -97,7 +97,7 @@ export class PluginManager {
|
||||
beforeLaunch(config: AppConfig) {
|
||||
for (const p of this._plugins) {
|
||||
try {
|
||||
p.beforeLaunch(config);
|
||||
p?.beforeLaunch?.(config);
|
||||
} catch (e) {
|
||||
logger.error(`Error while calling beforeLaunch hook of plugin ${p.constructor.name}`, e);
|
||||
}
|
||||
@@ -107,7 +107,7 @@ export class PluginManager {
|
||||
afterSetup(interpreter: Interpreter) {
|
||||
for (const p of this._plugins) {
|
||||
try {
|
||||
p.afterSetup(interpreter);
|
||||
p.afterSetup?.(interpreter);
|
||||
} catch (e) {
|
||||
logger.error(`Error while calling afterSetup hook of plugin ${p.constructor.name}`, e);
|
||||
}
|
||||
@@ -117,25 +117,25 @@ export class PluginManager {
|
||||
}
|
||||
|
||||
afterStartup(interpreter: Interpreter) {
|
||||
for (const p of this._plugins) p.afterStartup(interpreter);
|
||||
for (const p of this._plugins) p.afterStartup?.(interpreter);
|
||||
|
||||
for (const p of this._pythonPlugins) p.afterStartup?.(interpreter);
|
||||
}
|
||||
|
||||
beforePyScriptExec(options: {interpreter: Interpreter, src: string, pyScriptTag: PyScriptTag}) {
|
||||
for (const p of this._plugins) p.beforePyScriptExec(options);
|
||||
for (const p of this._plugins) p.beforePyScriptExec?.(options);
|
||||
|
||||
for (const p of this._pythonPlugins) p.beforePyScriptExec?.callKwargs(options);
|
||||
}
|
||||
|
||||
afterPyScriptExec(options: {interpreter: Interpreter, src: string, pyScriptTag: PyScriptTag, result: any}) {
|
||||
for (const p of this._plugins) p.afterPyScriptExec(options);
|
||||
for (const p of this._plugins) p.afterPyScriptExec?.(options);
|
||||
|
||||
for (const p of this._pythonPlugins) p.afterPyScriptExec?.callKwargs(options);
|
||||
}
|
||||
|
||||
onUserError(error: UserError) {
|
||||
for (const p of this._plugins) p.onUserError(error);
|
||||
for (const p of this._plugins) p.onUserError?.(error);
|
||||
|
||||
for (const p of this._pythonPlugins) p.onUserError?.(error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user