node-pluginsmanager 2.3.5 → 2.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,9 @@
1
- /*
2
- eslint max-params: 0
3
- */
4
-
5
1
  "use strict";
6
2
 
3
+ // consts
4
+
5
+ const MAX_PARALLEL = 5;
6
+
7
7
  // private
8
8
 
9
9
  // methods
@@ -31,22 +31,44 @@
31
31
 
32
32
  /**
33
33
  * Load plugins with sort conditions
34
- * @param {object} toLoad : plugins to init
34
+ * @param {object} pluginsToInit : plugins to init
35
35
  * @param {function} emit : emit data function
36
36
  * @param {object} data : data to send
37
37
  * @param {number} i : stepper
38
38
  * @return {Promise} : result operation
39
39
  */
40
- function _initSortedPlugins (toLoad, emit, data, i = 0) {
40
+ function _initSortedPlugins (pluginsToInit, emit, data, i = 0) {
41
41
 
42
- return i < toLoad.length ? Promise.resolve().then(() => {
42
+ return i < pluginsToInit.length ? Promise.resolve().then(() => {
43
43
 
44
- return _initPlugin(toLoad[i], emit, data);
44
+ return _initPlugin(pluginsToInit[i], emit, data);
45
45
 
46
46
  // loop
47
47
  }).then(() => {
48
48
 
49
- return _initSortedPlugins(toLoad, emit, data, i + 1);
49
+ return _initSortedPlugins(pluginsToInit, emit, data, i + 1);
50
+
51
+ }) : Promise.resolve();
52
+
53
+ }
54
+
55
+ /**
56
+ * Load plugins without sort conditions
57
+ * @param {object} pluginsToInit : plugins to init
58
+ * @param {function} emit : emit data function
59
+ * @param {object} data : data to send
60
+ * @return {Promise} : result operation
61
+ */
62
+ function _initUnSortedPlugins (pluginsToInit, emit, data) {
63
+
64
+ return pluginsToInit.length ? Promise.all(pluginsToInit.splice(0, MAX_PARALLEL).map((p) => {
65
+
66
+ return _initPlugin(p, emit, data);
67
+
68
+ // loop
69
+ })).then(() => {
70
+
71
+ return _initUnSortedPlugins(pluginsToInit, emit, data);
50
72
 
51
73
  }) : Promise.resolve();
52
74
 
@@ -54,30 +76,34 @@
54
76
 
55
77
  // module
56
78
 
57
- module.exports = (plugins, orderedPluginsNames, emit, data) => {
79
+ module.exports = function initSortedPlugins (plugins, orderedPluginsNames, emit, data) {
58
80
 
59
81
  // if no plugins, does not run
60
82
  return !plugins.length ? Promise.resolve() : Promise.resolve().then(() => {
61
83
 
62
- // first, sorted plugins
63
- return _initSortedPlugins(orderedPluginsNames.map((pluginName) => {
64
-
65
- return plugins.find((plugin) => {
66
- return plugin.name === pluginName;
67
- });
84
+ const sortedPlugins = [
85
+ ...plugins.filter((plugin) => {
86
+ return orderedPluginsNames.includes(plugin.name);
87
+ })
88
+ ];
68
89
 
69
- }), emit, data);
90
+ // first, sorted plugins
91
+ return sortedPlugins.length ?
92
+ _initSortedPlugins(sortedPlugins, emit, data) :
93
+ Promise.resolve();
70
94
 
71
95
  }).then(() => {
72
96
 
73
- // then, all other plugins, asynchronously
74
- return Promise.all(plugins.filter((plugin) => {
75
- return !orderedPluginsNames.includes(plugin.name);
76
- }).map((plugin) => {
77
-
78
- return _initPlugin(plugin, emit, data);
97
+ const unsortedPlugin = [
98
+ ...plugins.filter((plugin) => {
99
+ return !orderedPluginsNames.includes(plugin.name);
100
+ })
101
+ ];
79
102
 
80
- }));
103
+ // then, all other plugins, asynchronously
104
+ return unsortedPlugin.length ?
105
+ _initUnSortedPlugins(unsortedPlugin, emit, data) :
106
+ Promise.resolve();
81
107
 
82
108
  });
83
109
 
@@ -12,6 +12,10 @@
12
12
  // locals
13
13
  const createPluginByDirectory = require(join(__dirname, "createPluginByDirectory.js"));
14
14
 
15
+ // consts
16
+
17
+ const MAX_PARALLEL = 5;
18
+
15
19
  // private
16
20
 
17
21
  // methods
@@ -20,26 +24,26 @@
20
24
  * Load plugins with sort conditions
21
25
  * @param {string} globalDirectory : get plugins directory
22
26
  * @param {string} externalRessourcesDirectory : get ressources directory
23
- * @param {string} pluginName : get ressources directory
24
- * @param {object} plugins : already loaded plugins
27
+ * @param {string} pluginFileName : get ressources directory
28
+ * @param {object} loadedPlugins : already loaded plugins
25
29
  * @param {function} emit : emit data function
26
30
  * @param {function|null} logger : if logger, send it to plugin
27
31
  * @param {object} data : data to send
28
32
  * @return {Promise} : result operation
29
33
  */
30
- function _loadPlugin (globalDirectory, externalRessourcesDirectory, pluginName, plugins, emit, logger, data) {
34
+ function _loadPlugin (globalDirectory, externalRessourcesDirectory, pluginFileName, loadedPlugins, emit, logger, data) {
31
35
 
32
36
  // is already loaded ?
33
- const plugin = plugins.find((p) => {
34
- return pluginName === p.name;
37
+ const plugin = loadedPlugins.find((p) => {
38
+ return pluginFileName === p.name;
35
39
  });
36
40
 
37
41
  // is already exists ?
38
42
  return plugin ? Promise.resolve() : Promise.resolve().then(() => {
39
43
 
40
- emit("loading", pluginName, data);
44
+ emit("loading", pluginFileName, data);
41
45
 
42
- const directory = join(globalDirectory, pluginName);
46
+ const directory = join(globalDirectory, pluginFileName);
43
47
 
44
48
  return createPluginByDirectory(directory, externalRessourcesDirectory, logger, data);
45
49
 
@@ -47,7 +51,7 @@
47
51
  }).then((createdPlugin) => {
48
52
 
49
53
  emit("loaded", createdPlugin, data);
50
- plugins.push(createdPlugin);
54
+ loadedPlugins.push(createdPlugin);
51
55
 
52
56
  return Promise.resolve();
53
57
 
@@ -59,24 +63,50 @@
59
63
  * Load plugins with sort conditions
60
64
  * @param {string} globalDirectory : get plugins directory
61
65
  * @param {string} externalRessourcesDirectory : get ressources directory
62
- * @param {Array} toLoad : plugins to load
63
- * @param {object} plugins : already loaded plugins
66
+ * @param {Array} pluginsToLoad : plugins to load
67
+ * @param {object} loadedPlugins : already loaded plugins
64
68
  * @param {function} emit : emit data function
65
69
  * @param {function|null} logger : if logger, send it to plugin
66
70
  * @param {object} data : data to send
67
71
  * @param {number} i : stepper
68
72
  * @return {Promise} : result operation
69
73
  */
70
- function _loadSortedPlugins (globalDirectory, externalRessourcesDirectory, toLoad, plugins, emit, logger, data, i = 0) {
74
+ function _loadSortedPlugins (globalDirectory, externalRessourcesDirectory, pluginsToLoad, loadedPlugins, emit, logger, data, i = 0) {
71
75
 
72
- return i < toLoad.length ? Promise.resolve().then(() => {
76
+ return i < pluginsToLoad.length ? Promise.resolve().then(() => {
73
77
 
74
- return _loadPlugin(globalDirectory, externalRessourcesDirectory, toLoad[i], plugins, emit, logger, data);
78
+ return _loadPlugin(globalDirectory, externalRessourcesDirectory, pluginsToLoad[i], loadedPlugins, emit, logger, data);
75
79
 
76
80
  // loop
77
81
  }).then(() => {
78
82
 
79
- return _loadSortedPlugins(globalDirectory, externalRessourcesDirectory, toLoad, plugins, emit, logger, data, i + 1);
83
+ return _loadSortedPlugins(globalDirectory, externalRessourcesDirectory, pluginsToLoad, loadedPlugins, emit, logger, data, i + 1);
84
+
85
+ }) : Promise.resolve();
86
+
87
+ }
88
+
89
+ /**
90
+ * Load plugins without sort conditions
91
+ * @param {string} globalDirectory : get plugins directory
92
+ * @param {string} externalRessourcesDirectory : get ressources directory
93
+ * @param {Array} pluginsToLoad : plugins to load
94
+ * @param {object} loadedPlugins : already loaded plugins
95
+ * @param {function} emit : emit data function
96
+ * @param {function|null} logger : if logger, send it to plugin
97
+ * @param {object} data : data to send
98
+ * @return {Promise} : result operation
99
+ */
100
+ function _loadUnSortedPlugins (globalDirectory, externalRessourcesDirectory, pluginsToLoad, loadedPlugins, emit, logger, data) {
101
+
102
+ return pluginsToLoad.length ? Promise.all(pluginsToLoad.splice(0, MAX_PARALLEL).map((p) => {
103
+
104
+ return _loadPlugin(globalDirectory, externalRessourcesDirectory, p, loadedPlugins, emit, logger, data);
105
+
106
+ // loop
107
+ })).then(() => {
108
+
109
+ return _loadUnSortedPlugins(globalDirectory, externalRessourcesDirectory, pluginsToLoad, loadedPlugins, emit, logger, data);
80
110
 
81
111
  }) : Promise.resolve();
82
112
 
@@ -84,24 +114,36 @@
84
114
 
85
115
  // module
86
116
 
87
- module.exports = (globalDirectory, externalRessourcesDirectory, files, plugins, orderedPluginsNames, emit, logger, data) => {
117
+ module.exports = function loadSortedPlugins (
118
+ globalDirectory, externalRessourcesDirectory, files, loadedPlugins, orderedPluginsNames, emit, logger, data
119
+ ) {
88
120
 
89
121
  // if no files, does not run
90
122
  return !files.length ? Promise.resolve() : Promise.resolve().then(() => {
91
123
 
124
+ const sortedPluginsNames = [
125
+ ...files.filter((pluginName) => {
126
+ return orderedPluginsNames.includes(pluginName);
127
+ })
128
+ ];
129
+
92
130
  // first, sorted plugins
93
- return _loadSortedPlugins(globalDirectory, externalRessourcesDirectory, orderedPluginsNames, plugins, emit, logger, data);
131
+ return sortedPluginsNames.length ?
132
+ _loadSortedPlugins(globalDirectory, externalRessourcesDirectory, sortedPluginsNames, loadedPlugins, emit, logger, data) :
133
+ Promise.resolve();
94
134
 
95
135
  }).then(() => {
96
136
 
97
- // then, all other plugins, asynchronously
98
- return Promise.all(files.filter((pluginName) => {
99
- return !orderedPluginsNames.includes(pluginName);
100
- }).map((file) => {
101
-
102
- return _loadPlugin(globalDirectory, externalRessourcesDirectory, file, plugins, emit, logger, data);
137
+ const unsortedPluginsNames = [
138
+ ...files.filter((pluginName) => {
139
+ return !orderedPluginsNames.includes(pluginName);
140
+ })
141
+ ];
103
142
 
104
- }));
143
+ // then, all other plugins, asynchronously
144
+ return unsortedPluginsNames.length ?
145
+ _loadUnSortedPlugins(globalDirectory, externalRessourcesDirectory, unsortedPluginsNames, loadedPlugins, emit, logger, data) :
146
+ Promise.resolve();
105
147
 
106
148
  });
107
149
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-pluginsmanager",
3
- "version": "2.3.5",
3
+ "version": "2.3.6",
4
4
  "description": "A plugins manager",
5
5
  "main": "lib/main.js",
6
6
  "typings": "lib/index.d.ts",
@@ -39,18 +39,18 @@
39
39
  "node-promfs": "3.7.0"
40
40
  },
41
41
  "devDependencies": {
42
- "@types/node": "16.11.7",
42
+ "@types/node": "16.11.10",
43
43
  "@types/socket.io": "3.0.2",
44
44
  "@types/ws": "8.2.0",
45
45
  "coveralls": "3.1.1",
46
- "eslint": "8.2.0",
46
+ "eslint": "8.3.0",
47
47
  "express": "4.17.1",
48
48
  "husky": "7.0.4",
49
49
  "mocha": "9.1.3",
50
50
  "node-pluginsmanager-plugin": "4.7.2",
51
51
  "nyc": "15.1.0",
52
- "typescript": "4.4.4",
53
- "ws": "8.2.3"
52
+ "typescript": "4.5.2",
53
+ "ws": "8.3.0"
54
54
  },
55
55
  "homepage": "https://github.com/Psychopoulet/node-pluginsmanager#readme",
56
56
  "engines": {