@soundworks/core 4.0.0-alpha.6 → 4.0.0-alpha.7

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soundworks/core",
3
- "version": "4.0.0-alpha.6",
3
+ "version": "4.0.0-alpha.7",
4
4
  "description": "Open-source creative coding framework for distributed applications based on Web technologies",
5
5
  "authors": [
6
6
  "Benjamin Matuszewski",
@@ -83,7 +83,7 @@ class PluginManager extends BasePluginManager {
83
83
  */
84
84
  constructor(client) {
85
85
  if (!(client instanceof Client)) {
86
- throw new Error(`[soundworks.PluginManager] Invalid argument, "new PluginManager(client)" should receive an instance of "soundworks.Client as argument"`);
86
+ throw new Error(`[soundworks.PluginManager] Invalid argument, "new PluginManager(client)" should receive an instance of "soundworks.Client" as argument`);
87
87
  }
88
88
 
89
89
  super(client);
@@ -5,26 +5,10 @@ import merge from 'lodash.merge';
5
5
  */
6
6
  class BasePlugin {
7
7
  constructor(id) {
8
- /**
9
- * User defined ID of the plugin.
10
- *
11
- * @type {string}
12
- * @see {@link client.PluginManager#register}
13
- * @see {@link server.PluginManager#register}
14
- */
15
- this.id = id;
16
-
17
- /**
18
- * Type of the plugin, i.e. the ClassName.
19
- *
20
- * Usefull to do perform some logic based on certain types of plugins without
21
- * knowing under which `id` they have been registered. (e.g. creating some generic
22
- * views, etc.)
23
- *
24
- * @type {string}
25
- * @readonly
26
- */
27
- this.type = this.constructor.name;
8
+ /** @private */
9
+ this._id = id;
10
+ /** @private */
11
+ this._type = this.constructor.name;
28
12
 
29
13
  /**
30
14
  * Options of the plugin.
@@ -58,6 +42,32 @@ class BasePlugin {
58
42
  this._onStateChangeCallbacks = new Set();
59
43
  }
60
44
 
45
+ /**
46
+ * User defined ID of the plugin.
47
+ *
48
+ * @type {string}
49
+ * @readonly
50
+ * @see {@link client.PluginManager#register}
51
+ * @see {@link server.PluginManager#register}
52
+ */
53
+ get id() {
54
+ return this._id;
55
+ }
56
+
57
+ /**
58
+ * Type of the plugin, i.e. the ClassName.
59
+ *
60
+ * Usefull to do perform some logic based on certain types of plugins without
61
+ * knowing under which `id` they have been registered. (e.g. creating some generic
62
+ * views, etc.)
63
+ *
64
+ * @type {string}
65
+ * @readonly
66
+ */
67
+ get type() {
68
+ return this._type;
69
+ }
70
+
61
71
  /**
62
72
  * Start the plugin. This method is automatically called during the client or
63
73
  * server `init()` lifecyle step. After `start()` is fulfilled the plugin should
@@ -13,7 +13,7 @@ class BasePluginManager {
13
13
  /** @private */
14
14
  this._node = node;
15
15
  /** @private */
16
- this._registeredPlugins = new Map();
16
+ this._dependencies = new Map();
17
17
  /** @private */
18
18
  this._instances = new Map();
19
19
  /** @private */
@@ -67,11 +67,29 @@ class BasePluginManager {
67
67
  throw new Error(`[soundworks.PluginManager] Invalid argument, "pluginManager.register" fourth optionnal argument should be an array`);
68
68
  }
69
69
 
70
- if (this._registeredPlugins.has(id)) {
71
- throw new Error(`[soundworks:PluginManager] Plugin "${id}" of type "${ctor.name}" already registered`);
70
+ if (this._instances.has(id)) {
71
+ throw new Error(`[soundworks:PluginManager] Plugin "${id}" already registered`);
72
72
  }
73
73
 
74
- this._registeredPlugins.set(id, { ctor, options, deps });
74
+ // we instanciate the plugin here, so that a plugin can register another one
75
+ // in its own constructor.
76
+ //
77
+ // the dependencies must be created first, so that the instance can call
78
+ // addDependency in its constructor
79
+ this._dependencies.set(id, deps);
80
+
81
+ const instance = new ctor(this._node, id, options);
82
+ this._instances.set(id, instance);
83
+ }
84
+
85
+ /**
86
+ * Manually add a dependency to a given plugin. Usefull to require a plugin
87
+ * within a plugin
88
+ *
89
+ */
90
+ addDependency(pluginId, dependencyId) {
91
+ const deps = this._dependencies.get(pluginId);
92
+ deps.push(dependencyId);
75
93
  }
76
94
 
77
95
  /**
@@ -79,7 +97,7 @@ class BasePluginManager {
79
97
  * @returns {string[]}
80
98
  */
81
99
  getRegisteredPlugins() {
82
- return Array.from(this._registeredPlugins.keys());
100
+ return Array.from(this._instances.keys());
83
101
  }
84
102
 
85
103
  /**
@@ -96,16 +114,14 @@ class BasePluginManager {
96
114
 
97
115
  this.status = 'inited';
98
116
  // instanciate all plugins
99
- for (let [id, { ctor, options }] of this._registeredPlugins.entries()) {
100
- const instance = new ctor(this._node, id, options);
101
- this._instances.set(id, instance);
117
+ for (let [id, instance] of this._instances.entries()) {
102
118
  instance.onStateChange(_values => this._propagateStateChange(instance));
103
119
  }
104
120
 
105
121
  // propagate all 'idle' statuses before start
106
122
  this._propagateStateChange();
107
123
 
108
- const promises = Array.from(this._registeredPlugins.keys()).map(id => this.unsafeGet(id));
124
+ const promises = Array.from(this._instances.keys()).map(id => this.unsafeGet(id));
109
125
 
110
126
  try {
111
127
  await Promise.all(promises);
@@ -125,8 +141,8 @@ class BasePluginManager {
125
141
 
126
142
  /**
127
143
  * Retrieve an fully started instance of a registered plugin, without checking
128
- * that the pluginManager is started. This is important for starting the plugin
129
- * manager itself.
144
+ * that the pluginManager has started. This is required for starting the plugin
145
+ * manager itself and to require a plugin from within another plugin
130
146
  *
131
147
  * @private
132
148
  */
@@ -135,7 +151,7 @@ class BasePluginManager {
135
151
  throw new Error(`[soundworks.PluginManager] Invalid argument, "pluginManager.get(name)" argument should be a string`);
136
152
  }
137
153
 
138
- if (!this._registeredPlugins.has(id)) {
154
+ if (!this._instances.has(id)) {
139
155
  throw new Error(`[soundworks:PluginManager] Cannot get plugin "${id}", plugin is not registered`);
140
156
  }
141
157
 
@@ -144,7 +160,7 @@ class BasePluginManager {
144
160
  // to dynamically register and launch plugins at runtime.
145
161
  //
146
162
  // if (!this._instances.has(id)) {
147
- // const { ctor, options } = this._registeredPlugins.get(id);
163
+ // const { ctor, options } = this._dependencies.get(id);
148
164
  // const instance = new ctor(this._node, id, options);
149
165
  // this._instances.set(id, instance);
150
166
  // }
@@ -152,7 +168,7 @@ class BasePluginManager {
152
168
  const instance = this._instances.get(id);
153
169
 
154
170
  // recursively get the dependency chain
155
- const { deps } = this._registeredPlugins.get(id);
171
+ const deps = this._dependencies.get(id);
156
172
  const promises = deps.map(id => this.unsafeGet(id));
157
173
 
158
174
  await Promise.all(promises);
@@ -78,7 +78,7 @@ import Server from './Server.js';
78
78
  class PluginManager extends BasePluginManager {
79
79
  constructor(server) {
80
80
  if (!(server instanceof Server)) {
81
- throw new Error(`[soundworks.PluginManager] Invalid argument, "new PluginManager(server)" should receive an instance of "soundworks.Server as argument"`);
81
+ throw new Error(`[soundworks.PluginManager] Invalid argument, "new PluginManager(server)" should receive an instance of "soundworks.Server" as argument`);
82
82
  }
83
83
 
84
84
  super(server);