Skip to content

Commit

Permalink
Wait for PluginManager to start.
Browse files Browse the repository at this point in the history
Return emtpy plugins array after maxTries has been reached.
  • Loading branch information
panaaj committed Aug 23, 2024
1 parent 513324a commit b370415
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
4 changes: 2 additions & 2 deletions docs/src/develop/plugins/server_plugin_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ These functions are available via the `app` object passed to the plugin when it

#### `app.getFeatures(enabed)`

Returns an object detailing the available APIs and Plugins.
Returns a Promise containing an object detailing the available APIs and Plugins.

The `enabled` parameter is optional and has the following values:
- `undefined` (not provided): list all features
Expand All @@ -27,7 +27,7 @@ The `enabled` parameter is optional and has the following values:

_Example:_
```javascript
let features = app.getFeatures();
const features = await app.getFeatures();

{
"apis": [
Expand Down
29 changes: 19 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,25 @@ class Server {

// feature detection
app.getFeatures = async (enabled?: boolean) => {
if (typeof app.getPluginsList === 'function') {
return {
apis: enabled === false ? [] : app.apis,
plugins: await app.getPluginsList(enabled)
}
} else {
throw new Error(`
Plugin manager initialisation not complete!
Cannot call this function until all plugins have been initialised!
`)
const checkPMStarted = (): Promise<boolean> => {
return new Promise((resolve) => {
setImmediate(() => {
resolve(typeof app.getPluginsList === 'function')
})
})
}
const maxTries = 10
let tries = 0
let pmStarted = false

while (!pmStarted && tries < maxTries) {
tries++
pmStarted = await checkPMStarted()
}
const plugins = pmStarted ? await app.getPluginsList(enabled) : []
return {
apis: enabled === false ? [] : app.apis,
plugins: plugins
}
}

Expand Down

0 comments on commit b370415

Please sign in to comment.