From 513324a8e3e57da2669b6bfbd6c2326a6aeb8126 Mon Sep 17 00:00:00 2001 From: panaaj <38519157+panaaj@users.noreply.github.com> Date: Wed, 21 Aug 2024 14:00:02 +0930 Subject: [PATCH 1/2] Test for availability of getPluginsList() (#1777) Throw on error. --- src/index.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 574a7d519..d053bb48c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -132,9 +132,16 @@ class Server { // feature detection app.getFeatures = async (enabled?: boolean) => { - return { - apis: enabled === false ? [] : app.apis, - plugins: await app.getPluginsList(enabled) + 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! + `) } } From b37041542455e10dc793064b2a82c7d2e43820c0 Mon Sep 17 00:00:00 2001 From: panaaj <38519157+panaaj@users.noreply.github.com> Date: Fri, 23 Aug 2024 13:50:34 +0930 Subject: [PATCH 2/2] Wait for PluginManager to start. Return emtpy plugins array after maxTries has been reached. --- docs/src/develop/plugins/server_plugin_api.md | 4 +-- src/index.ts | 29 ++++++++++++------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/docs/src/develop/plugins/server_plugin_api.md b/docs/src/develop/plugins/server_plugin_api.md index 7a12a4cc0..e0d3e5742 100644 --- a/docs/src/develop/plugins/server_plugin_api.md +++ b/docs/src/develop/plugins/server_plugin_api.md @@ -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 @@ -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": [ diff --git a/src/index.ts b/src/index.ts index d053bb48c..19e508907 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 => { + 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 } }