@soundworks/core 4.0.0-alpha.9 → 5.0.0

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.
Files changed (144) hide show
  1. package/CHANGELOG.md +102 -0
  2. package/README.md +14 -29
  3. package/package.json +24 -42
  4. package/src/client/Client.js +306 -256
  5. package/src/client/{Context.js → ClientContext.js} +63 -54
  6. package/src/client/ClientContextManager.js +115 -0
  7. package/src/client/{Plugin.js → ClientPlugin.js} +23 -19
  8. package/src/client/{PluginManager.js → ClientPluginManager.js} +43 -40
  9. package/src/client/ClientSocket.js +270 -0
  10. package/src/client/{StateManager.js → ClientStateManager.js} +13 -13
  11. package/src/client/index.js +36 -39
  12. package/src/common/BasePlugin.js +63 -58
  13. package/src/common/BasePluginManager.js +191 -142
  14. package/src/common/BaseStateManager.js +635 -145
  15. package/src/common/BatchedTransport.js +73 -0
  16. package/src/common/ParameterBag.js +90 -93
  17. package/src/common/PromiseStore.js +54 -0
  18. package/src/common/SharedState.js +821 -0
  19. package/src/common/SharedStateCollection.js +492 -0
  20. package/src/common/constants.js +14 -2
  21. package/src/common/logger.js +64 -68
  22. package/src/common/shared-state-types.js +82 -0
  23. package/src/common/sockets-utils.js +0 -64
  24. package/src/common/version.js +2 -0
  25. package/src/server/Server.js +552 -824
  26. package/src/server/ServerClient.js +74 -0
  27. package/src/server/{Context.js → ServerContext.js} +96 -77
  28. package/src/server/{ContextManager.js → ServerContextManager.js} +107 -87
  29. package/src/server/{Plugin.js → ServerPlugin.js} +30 -32
  30. package/src/server/ServerPluginManager.js +177 -0
  31. package/src/server/ServerSocket.js +256 -0
  32. package/src/server/ServerSockets.js +150 -0
  33. package/src/server/ServerStateManager.js +706 -0
  34. package/src/server/SharedStatePrivate.js +277 -0
  35. package/src/server/audit-network-latency.worker.js +19 -4
  36. package/src/server/{audit-schema.js → audit-state-class-description.js} +3 -2
  37. package/src/server/create-http-server.js +127 -0
  38. package/src/server/crypto.js +8 -7
  39. package/src/server/index.js +60 -17
  40. package/types/client/Client.d.ts +95 -70
  41. package/types/client/{Context.d.ts → ClientContext.d.ts} +59 -34
  42. package/types/client/{ContextManager.d.ts → ClientContextManager.d.ts} +19 -22
  43. package/types/client/ClientPlugin.d.ts +51 -0
  44. package/types/client/ClientPluginManager.d.ts +112 -0
  45. package/types/client/ClientSocket.d.ts +76 -0
  46. package/types/client/{StateManager.d.ts → ClientStateManager.d.ts} +18 -21
  47. package/types/client/index.d.ts +33 -6
  48. package/types/common/BasePlugin.d.ts +64 -53
  49. package/types/common/BasePluginManager.d.ts +84 -39
  50. package/types/common/BaseStateManager.d.ts +227 -49
  51. package/types/common/BatchedTransport.d.ts +17 -0
  52. package/types/common/ParameterBag.d.ts +73 -76
  53. package/types/common/PromiseStore.d.ts +11 -0
  54. package/types/common/SharedState.d.ts +291 -0
  55. package/types/common/SharedStateCollection.d.ts +311 -0
  56. package/types/common/constants.d.ts +10 -3
  57. package/types/common/logger.d.ts +8 -6
  58. package/types/common/shared-state-types.d.ts +95 -0
  59. package/types/common/sockets-utils.d.ts +0 -5
  60. package/types/common/version.d.ts +2 -0
  61. package/types/server/Server.d.ts +215 -136
  62. package/types/server/ServerClient.d.ts +45 -0
  63. package/types/server/ServerContext.d.ts +209 -0
  64. package/types/server/ServerContextManager.d.ts +75 -0
  65. package/types/server/ServerPlugin.d.ts +57 -0
  66. package/types/server/ServerPluginManager.d.ts +115 -0
  67. package/types/server/ServerSocket.d.ts +69 -0
  68. package/types/server/ServerSockets.d.ts +42 -0
  69. package/types/server/ServerStateManager.d.ts +284 -0
  70. package/types/server/SharedStatePrivate.d.ts +23 -0
  71. package/types/server/audit-network-latency.worker.d.ts +2 -2
  72. package/types/server/{audit-schema.d.ts → audit-state-class-description.d.ts} +8 -9
  73. package/types/server/create-http-server.d.ts +5 -0
  74. package/types/server/crypto.d.ts +4 -0
  75. package/types/server/index.d.ts +51 -3
  76. package/.eslintrc +0 -15
  77. package/.github/workflows/gh-pages.yml +0 -31
  78. package/.github/workflows/unit-tests.yml +0 -18
  79. package/src/client/ContextManager.js +0 -111
  80. package/src/client/SharedState.js +0 -84
  81. package/src/client/SharedStateCollection.js +0 -35
  82. package/src/client/Socket.js +0 -402
  83. package/src/common/BaseSharedState.js +0 -583
  84. package/src/common/BaseSharedStateCollection.js +0 -179
  85. package/src/common/SharedStatePrivate.js +0 -192
  86. package/src/common/promise-store.js +0 -25
  87. package/src/server/Client.js +0 -55
  88. package/src/server/PluginManager.js +0 -170
  89. package/src/server/SharedState.js +0 -85
  90. package/src/server/SharedStateCollection.js +0 -36
  91. package/src/server/Socket.js +0 -396
  92. package/src/server/Sockets.js +0 -224
  93. package/src/server/StateManager.js +0 -620
  94. package/tsconfig.json +0 -21
  95. package/types/client/Client.d.ts.map +0 -1
  96. package/types/client/Context.d.ts.map +0 -1
  97. package/types/client/ContextManager.d.ts.map +0 -1
  98. package/types/client/Plugin.d.ts +0 -53
  99. package/types/client/Plugin.d.ts.map +0 -1
  100. package/types/client/PluginManager.d.ts +0 -96
  101. package/types/client/PluginManager.d.ts.map +0 -1
  102. package/types/client/SharedState.d.ts +0 -85
  103. package/types/client/SharedState.d.ts.map +0 -1
  104. package/types/client/Socket.d.ts +0 -138
  105. package/types/client/Socket.d.ts.map +0 -1
  106. package/types/client/StateManager.d.ts.map +0 -1
  107. package/types/client/index.d.ts.map +0 -1
  108. package/types/common/BasePlugin.d.ts.map +0 -1
  109. package/types/common/BasePluginManager.d.ts.map +0 -1
  110. package/types/common/BaseSharedState.d.ts +0 -173
  111. package/types/common/BaseSharedState.d.ts.map +0 -1
  112. package/types/common/BaseStateManager.d.ts.map +0 -1
  113. package/types/common/ParameterBag.d.ts.map +0 -1
  114. package/types/common/SharedStatePrivate.d.ts +0 -20
  115. package/types/common/SharedStatePrivate.d.ts.map +0 -1
  116. package/types/common/constants.d.ts.map +0 -1
  117. package/types/common/logger.d.ts.map +0 -1
  118. package/types/common/promise-store.d.ts +0 -4
  119. package/types/common/promise-store.d.ts.map +0 -1
  120. package/types/common/sockets-utils.d.ts.map +0 -1
  121. package/types/common/utils.d.ts +0 -5
  122. package/types/common/utils.d.ts.map +0 -1
  123. package/types/server/Client.d.ts +0 -40
  124. package/types/server/Client.d.ts.map +0 -1
  125. package/types/server/Context.d.ts +0 -59
  126. package/types/server/Context.d.ts.map +0 -1
  127. package/types/server/ContextManager.d.ts +0 -69
  128. package/types/server/ContextManager.d.ts.map +0 -1
  129. package/types/server/Plugin.d.ts +0 -70
  130. package/types/server/Plugin.d.ts.map +0 -1
  131. package/types/server/PluginManager.d.ts +0 -91
  132. package/types/server/PluginManager.d.ts.map +0 -1
  133. package/types/server/Server.d.ts.map +0 -1
  134. package/types/server/SharedState.d.ts +0 -86
  135. package/types/server/SharedState.d.ts.map +0 -1
  136. package/types/server/Socket.d.ts +0 -128
  137. package/types/server/Socket.d.ts.map +0 -1
  138. package/types/server/Sockets.d.ts +0 -73
  139. package/types/server/Sockets.d.ts.map +0 -1
  140. package/types/server/StateManager.d.ts +0 -190
  141. package/types/server/StateManager.d.ts.map +0 -1
  142. package/types/server/audit-network-latency.worker.d.ts.map +0 -1
  143. package/types/server/audit-schema.d.ts.map +0 -1
  144. package/types/server/index.d.ts.map +0 -1
@@ -0,0 +1,112 @@
1
+ export default ClientPluginManager;
2
+ /**
3
+ * The `PluginManager` allows to register and retrieve `soundworks` plugins.
4
+ *
5
+ * Plugins should always be registered both client-side and server-side,
6
+ * and before {@link Client#start} or {@link Server#start}
7
+ * to be properly initialized.
8
+ *
9
+ * In some situations, you might want to register the same plugin factory several times
10
+ * using different ids (e.g. for watching several parts of the file system, etc.).
11
+ *
12
+ * Refer to the plugins' documentation for more precise examples, and the specific API
13
+ * they expose. See [https://soundworks.dev/guide/ecosystem](https://soundworks.dev/guide/ecosystem)
14
+ * for more information on the available plugins.
15
+ *
16
+ * See {@link Client#pluginManager}
17
+ *
18
+ * ```
19
+ * // client-side
20
+ * import { Client } from '@soundworks/core/client.js';
21
+ * import syncPlugin from '@soundworks/plugin-sync/client.js';
22
+ *
23
+ * const client = new Client(config);
24
+ * // register the plugin before `client.start()`
25
+ * client.pluginManager.register('sync', syncPlugin);
26
+ *
27
+ * await client.start();
28
+ *
29
+ * const sync = await client.pluginManager.get('sync');
30
+ *
31
+ * setInterval(() => {
32
+ * // log the estimated global synced clock alongside the local clock.
33
+ * console.log(sync.getSyncTime(), sync.getLocalTime());
34
+ * }, 1000);
35
+ * ```
36
+ *
37
+ * ```
38
+ * // server-side
39
+ * import { Server } from '@soundworks/core/server.js';
40
+ * import syncPlugin from '@soundworks/plugin-sync/server.js';
41
+ *
42
+ * const server = new Server(config);
43
+ * // register the plugin before `server.start()`
44
+ * server.pluginManager.register('sync', syncPlugin);
45
+ *
46
+ * await server.start();
47
+ *
48
+ * const sync = await server.pluginManager.get('sync');
49
+ *
50
+ * setInterval(() => {
51
+ * // log the estimated global synced clock alongside the local clock.
52
+ * console.log(sync.getSyncTime(), sync.getLocalTime());
53
+ * }, 1000);
54
+ * ```
55
+ *
56
+ * @extends BasePluginManager
57
+ * @inheritdoc
58
+ * @template T
59
+ */
60
+ declare class ClientPluginManager<T> extends BasePluginManager {
61
+ /**
62
+ * @param {Client} client - The soundworks client instance.
63
+ * @hideconstructor
64
+ */
65
+ constructor(client: Client);
66
+ /**
67
+ * Register a plugin into the manager.
68
+ *
69
+ * _A plugin must always be registered both on client-side and on server-side_
70
+ *
71
+ * Refer to the plugin documentation to check its options and proper way of
72
+ * registering it.
73
+ *
74
+ * @param {string} id - Unique id of the plugin. Enables the registration of the
75
+ * same plugin factory under different ids.
76
+ * @param {T<ClientPlugin>} ctor - The client-side Class of the plugin.
77
+ * @param {object} [options={}] - Options to configure the plugin.
78
+ * @param {array} [deps=[]] - List of plugins' names the plugin depends on, i.e.
79
+ * the plugin initialization will start only after the plugins it depends on are
80
+ * fully started themselves.
81
+ * @see {@link ClientPluginManager#register}
82
+ * @see {@link ServerPluginManager#register}
83
+ * @example
84
+ * // client-side
85
+ * client.pluginManager.register('user-defined-id', pluginFactory);
86
+ * // server-side
87
+ * server.pluginManager.register('user-defined-id', pluginFactory);
88
+ */
89
+ register(id: string, ctor: T<ClientPlugin>, options?: object, deps?: any[]): void;
90
+ /**
91
+ * Retrieve an fully started instance of a registered plugin.
92
+ *
93
+ * Be aware that the `get` method resolves only when the plugin is fully 'started',
94
+ * which is what we want 99.99% of the time. As such, and to prevent the application
95
+ * to be stuck in some kind of Promise dead lock, this method will throw if
96
+ * used before `client.init()` (or `client.start()`).
97
+ *
98
+ * To handle the remaining 0.01% cases and access plugin instances during their
99
+ * initialization (e.g. to display initialization screen, etc.), you should rely
100
+ * on the `onStateChange` method.
101
+ *
102
+ * _Note: the async API is designed to enable the dynamic creation of plugins
103
+ * in a future release._
104
+ *
105
+ * @param {string} id - Id of the plugin as defined when registered.
106
+ * @returns {Promise<T<ClientPlugin>>}
107
+ */
108
+ get(id: string): Promise<T<ClientPlugin>>;
109
+ }
110
+ import BasePluginManager from '../common/BasePluginManager.js';
111
+ import ClientPlugin from './ClientPlugin.js';
112
+ import Client from './Client.js';
@@ -0,0 +1,76 @@
1
+ export const kSocketTerminate: unique symbol;
2
+ export default ClientSocket;
3
+ /**
4
+ * The ClientSocket class is a simple publish / subscribe wrapper built on top of the
5
+ * [isomorphic-ws](https://github.com/heineiuo/isomorphic-ws) library.
6
+ * An instance of `ClientSocket` is automatically created by the `soundworks.Client`
7
+ * (see {@link Client#socket}).
8
+ *
9
+ * _Important: In most cases, you should consider using a {@link SharedState}
10
+ * rather than directly using the sockets._
11
+ *
12
+ * The ClientSocket class concurrently opens two different WebSockets:
13
+ * - a socket configured with `binaryType = 'blob'` for JSON compatible data
14
+ * types (i.e. string, number, boolean, object, array and null).
15
+ * - a socket configured with `binaryType= 'arraybuffer'` for efficient streaming
16
+ * of binary data.
17
+ *
18
+ * Both sockets re-emits all "native" ws events ('open', 'upgrade', 'close', 'error'
19
+ * and 'message'.
20
+ *
21
+ * @hideconstructor
22
+ */
23
+ declare class ClientSocket {
24
+ constructor(role: any, config: any, socketOptions: any);
25
+ /**
26
+ * Initialize a websocket connection with the server. Automatically called
27
+ * during {@link Client#init}
28
+ *
29
+ * @param {string} role - Role of the client (see {@link Client#role})
30
+ * @param {object} config - Configuration of the sockets
31
+ * @private
32
+ */
33
+ private init;
34
+ /**
35
+ * Send messages with JSON compatible data types on a given channel.
36
+ *
37
+ * @param {string} channel - Channel name.
38
+ * @param {...*} args - Payload of the message. As many arguments as needed, of
39
+ * JSON compatible data types (i.e. string, number, boolean, object, array and null).
40
+ */
41
+ send(channel: string, ...args: any[]): void;
42
+ /**
43
+ * Listen messages with JSON compatible data types on a given channel.
44
+ *
45
+ * @param {string} channel - Channel name.
46
+ * @param {Function} callback - Callback to execute when a message is received,
47
+ * arguments of the callback function will match the arguments sent using the
48
+ * {@link ServerSocket#send} method.
49
+ */
50
+ addListener(channel: string, callback: Function): void;
51
+ /**
52
+ * Remove a listener from JSON compatible messages on a given channel.
53
+ *
54
+ * @param {string} channel - Channel name.
55
+ * @param {Function} callback - Callback to remove.
56
+ */
57
+ removeListener(channel: string, callback: Function): void;
58
+ /**
59
+ * Remove all listeners of messages with JSON compatible data types.
60
+ *
61
+ * @param {string} channel - Channel name.
62
+ */
63
+ removeAllListeners(channel?: string): void;
64
+ /**
65
+ * Removes all listeners and immediately close the two sockets. Is automatically
66
+ * called on `client.stop()`
67
+ *
68
+ * Is also called when a disconnection is detected by the heartbeat (note that
69
+ * in this case, the launcher will call `client.stop()` but the listeners are
70
+ * already cleared so the event will be triggered only once.
71
+ *
72
+ * @private
73
+ */
74
+ private [kSocketTerminate];
75
+ #private;
76
+ }
@@ -1,23 +1,22 @@
1
- export default StateManager;
2
- export namespace client {
3
- /**
4
- * ~ObserveCallback
5
- */
6
- type StateManager = () => any;
7
- }
1
+ export default ClientStateManager;
2
+ /**
3
+ * ~ObserveCallback
4
+ */
5
+ export type ClientStateManager = () => any;
8
6
  /**
9
- * @callback client.StateManager~ObserveCallback
7
+ * @callback ClientStateManager~ObserveCallback
10
8
  * @async
11
- * @param {String} schemaName - name of the schema
12
- * @param {Number} stateId - id of the state
13
- * @param {Number} nodeId - id of the node that created the state
9
+ * @param {String} className - Name of the shared state class.
10
+ * @param {Number} stateId - Id of the state.
11
+ * @param {Number} nodeId - Id of the node that created the state.
14
12
  */
15
13
  /**
16
- * The `StateManager` allows to create new {@link client.SharedState}s, or attach
17
- * to {@link client.SharedState}s created by other nodes (clients or server). It
18
- * can also track all the {@link client.SharedState}s created by other nodes.
14
+ * The `ClientStateManager` allows to create new {@link SharedState}s, or attach
15
+ * to {@link SharedState}s created by other nodes (clients or server) on the network.
16
+ *
17
+ * It can also observe all the {@link SharedState}s created on the network.
19
18
  *
20
- * An instance of `StateManager` is automatically created by the `soundworks.Client`
19
+ * An instance of `ClientStateManager` is automatically created by the `soundworks.Client`
21
20
  * at initialization (cf. {@link client.Client#stateManager}).
22
21
  *
23
22
  * See {@link client.Client#stateManager}
@@ -29,8 +28,8 @@ export namespace client {
29
28
  * import { Server } from '@soundworks/server/index.js';
30
29
  *
31
30
  * const server = new Server(config);
32
- * // declare and register the schema of a shared state.
33
- * server.stateManager.registerSchema('some-global-state', {
31
+ * // define a class of shared state.
32
+ * server.stateManager.defineClass('some-global-state', {
34
33
  * myRandom: {
35
34
  * type: 'float',
36
35
  * default: 0,
@@ -61,12 +60,10 @@ export namespace client {
61
60
  * }, 1000);
62
61
  * ```
63
62
  *
64
- * @memberof client
65
63
  * @extends BaseStateManager
66
64
  * @inheritdoc
67
65
  * @hideconstructor
68
66
  */
69
- declare class StateManager extends BaseStateManager {
67
+ declare class ClientStateManager extends BaseStateManager {
70
68
  }
71
- import BaseStateManager from "../common/BaseStateManager.js";
72
- //# sourceMappingURL=StateManager.d.ts.map
69
+ import BaseStateManager from '../common/BaseStateManager.js';
@@ -1,11 +1,38 @@
1
- export { default as Context } from "./Context.js";
2
1
  export { default as Client } from "./Client.js";
2
+ export { default as ClientPlugin } from "./ClientPlugin.js";
3
+ export { default as ClientContext } from "./ClientContext.js";
4
+ export { default as version } from "../common/version.js";
3
5
  /**
4
- * Configuration object for a client running in a browser runtime.
6
+ * Application configuration object.
7
+ */
8
+ export type ClientAppConfig = {
9
+ /**
10
+ * - Name of the application.
11
+ */
12
+ name?: string;
13
+ /**
14
+ * - Name of the author.
15
+ */
16
+ author?: string;
17
+ };
18
+ /**
19
+ * Environment configuration object.
5
20
  */
6
- export type BrowserClientConfig = any;
21
+ export type ClientEnvConfig = objecy;
7
22
  /**
8
- * Configuration object for a client running in a node runtime.
23
+ * Configuration object for a client running in a browser runtime.
9
24
  */
10
- export type NodeClientConfig = any;
11
- //# sourceMappingURL=index.d.ts.map
25
+ export type ClientConfig = {
26
+ /**
27
+ * - Role of the client in the application (e.g. 'player', 'controller').
28
+ */
29
+ role: string;
30
+ /**
31
+ * - Application configuration object.
32
+ */
33
+ app: ClientAppConfig;
34
+ /**
35
+ * - Environment configuration object.
36
+ */
37
+ env: ClientEnvConfig;
38
+ };
@@ -1,68 +1,80 @@
1
+ export const kBasePluginStatus: unique symbol;
1
2
  export default BasePlugin;
2
3
  /**
3
- * @private
4
+ * Callback executed when the plugin state is updated.
4
5
  */
6
+ export type pluginOnStateChangeCallback = (#state: BasePlugin) => any;
7
+ /**
8
+ * Delete the registered {@link pluginOnStateChangeCallback}.
9
+ */
10
+ export type pluginDeleteOnStateChangeCallback = () => any;
11
+ /**
12
+ * Callback executed when the plugin state is updated.
13
+ *
14
+ * @callback pluginOnStateChangeCallback
15
+ * @param {BasePlugin#state} state - Current state of the plugin.
16
+ */
17
+ /**
18
+ * Delete the registered {@link pluginOnStateChangeCallback}.
19
+ *
20
+ * @callback pluginDeleteOnStateChangeCallback
21
+ */
22
+ /** @private */
5
23
  declare class BasePlugin {
6
24
  constructor(id: any);
25
+ /**
26
+ * Placeholder that stores internal (local) state of the plugin. The state
27
+ * should be modified through the `propagateStateChange` method to ensure
28
+ * the change to be properly propagated to manager `onStateChange` callbacks.
29
+ *
30
+ * @type {object}
31
+ * @protected
32
+ * @see {@link ClientPlugin#onStateChange}
33
+ * @see {@link ServerPlugin#onStateChange}
34
+ * @see {@link ClientPlugin#propagateStateChange}
35
+ * @see {@link ServerPlugin#propagateStateChange}
36
+ */
37
+ protected state: object;
7
38
  /**
8
39
  * User defined ID of the plugin.
9
40
  *
10
41
  * @type {string}
11
- * @see {@link client.PluginManager#register}
12
- * @see {@link server.PluginManager#register}
42
+ * @readonly
43
+ * @see {@link ClientPluginManager#register}
44
+ * @see {@link ServerPluginManager#register}
13
45
  */
14
- id: string;
46
+ readonly get id(): string;
15
47
  /**
16
48
  * Type of the plugin, i.e. the ClassName.
17
49
  *
18
- * Usefull to do perform some logic based on certain types of plugins without
50
+ * Useful to do perform some logic based on certain types of plugins without
19
51
  * knowing under which `id` they have been registered. (e.g. creating some generic
20
52
  * views, etc.)
21
53
  *
22
54
  * @type {string}
23
55
  * @readonly
24
56
  */
25
- readonly type: string;
57
+ readonly get type(): string;
26
58
  /**
27
- * Options of the plugin.
59
+ * Current status of the plugin.
28
60
  *
29
- * @type {object}
61
+ * @type {'idle'|'inited'|'started'|'errored'}
30
62
  */
31
- options: object;
63
+ get status(): "idle" | "inited" | "started" | "errored";
32
64
  /**
33
- * Placeholder that stores internal (local) state of the plugin. The state
34
- * should be modified through the `propagateStateChange` method to ensure
35
- * the change to be properly propagated to `onStateChange` callbacks.
36
- *
37
- * @type {object}
38
- * @protected
39
- * @see {@link client.Plugin#onStateChange}
40
- * @see {@link server.Plugin#onStateChange}
41
- * @see {@link client.Plugin#propagateStateChange}
42
- * @see {@link server.Plugin#propagateStateChange}
43
- */
44
- protected state: object;
45
- /**
46
- * Current status of the plugin, i.e. 'idle', 'inited', 'started', 'errored'
65
+ * Start the plugin.
47
66
  *
48
- * @type {string}
49
- */
50
- status: string;
51
- /** @private */
52
- private _onStateChangeCallbacks;
53
- /**
54
- * Start the plugin. This method is automatically called during the client or
55
- * server `init()` lifecyle step. After `start()` is fulfilled the plugin should
56
- * be ready to use.
67
+ * This method is automatically called during the client or server `init()` lifecycle
68
+ * step. After `start()` is fulfilled the plugin should be ready to use.
57
69
  *
58
70
  * @example
59
- * // server-side couterpart of a plugin that creates a dedicated global shared
71
+ * // server-side counterpart of a plugin that creates a dedicated global shared
60
72
  * // state on which the server-side part can attach.
61
- * class MyPlugin extends Plugin {
73
+ * class MyPlugin extends ServerPlugin {
62
74
  * constructor(server, id) {
63
75
  * super(server, id);
64
76
  *
65
- * this.server.stateManager.registerSchema(`my-plugin:${this.id}`, {
77
+ * this.server.stateManager.defineClass(`my-plugin:${this.id}`, {
66
78
  * someParam: {
67
79
  * type: 'boolean',
68
80
  * default: false,
@@ -83,17 +95,18 @@ declare class BasePlugin {
83
95
  */
84
96
  start(): Promise<void>;
85
97
  /**
86
- * Stop the plugin. This method is automatically called during the client or server
87
- * `stop()` lifecyle step.
98
+ * Stop the plugin.
99
+ *
100
+ * This method is automatically called during the client or server `stop()` lifecycle step.
88
101
  *
89
102
  * @example
90
- * // server-side couterpart of a plugin that creates a dedicated global shared
91
- * // state on which the server-side part can attach.
92
- * class MyPlugin extends Plugin {
103
+ * // server-side counterpart of a plugin that creates a dedicated global shared
104
+ * // state on which the client-side part can attach.
105
+ * class MyPlugin extends ServerPlugin {
93
106
  * constructor(server, id) {
94
107
  * super(server, id);
95
108
  *
96
- * this.server.stateManager.registerSchema(`my-plugin:${this.id}`, {
109
+ * this.server.stateManager.defineClass(`my-plugin:${this.id}`, {
97
110
  * someParam: {
98
111
  * type: 'boolean',
99
112
  * default: false,
@@ -117,29 +130,27 @@ declare class BasePlugin {
117
130
  /**
118
131
  * Listen to the state changes propagated by {@link BasePlugin.propagateStateChange}
119
132
  *
120
- * @param {client.Plugin~onStateChangeCallback|server.Plugin~onStateChangeCallback} callback -
121
- * Callback to execute when a state change is propagated.
122
- * @returns {client.Plugin~deleteOnStateChangeCallback|server.Plugin~deleteOnStateChangeCallback}
123
- * Execute the function to delete the listener from the callback list.
124
- * @see {@link client.Plugin#propagateStateChange}
125
- * @see {@link server.Plugin#propagateStateChange}
133
+ * @param {pluginOnStateChangeCallback} callback - Callback to execute when a state change is propagated.
134
+ * @returns {pluginDeleteOnStateChangeCallback}
135
+ *
126
136
  * @example
127
137
  * const unsubscribe = plugin.onStateChange(pluginState => console.log(pluginState));
128
138
  * // stop listening state changes
129
139
  * unsubscribe();
130
140
  */
131
- onStateChange(callback: any): client.Plugin;
141
+ onStateChange(callback: pluginOnStateChangeCallback): pluginDeleteOnStateChangeCallback;
132
142
  /**
133
143
  * Apply updates to the plugin state and propagate the updated state to the
134
144
  * `onStateChange` listeners. The state changes will also be propagated
135
145
  * through the `PluginManager#onStateChange` listeners.
136
146
  *
137
147
  * @param {object} updates - Updates to be merged in the plugin state.
138
- * @see {@link client.Plugin#onStateChange}
139
- * @see {@link server.Plugin#onStateChange}
140
- * @see {@link client.PluginManager#onStateChange}
141
- * @see {@link server.PluginManager#onStateChange}
148
+ *
149
+ * @see {@link BasePlugin#onStateChange}
150
+ * @see {@link BasePluginManager#onStateChange}
142
151
  */
143
152
  propagateStateChange(updates: object): void;
153
+ /** @private */
154
+ private [kBasePluginStatus];
155
+ #private;
144
156
  }
145
- //# sourceMappingURL=BasePlugin.d.ts.map
@@ -1,61 +1,98 @@
1
+ export const kPluginManagerStart: unique symbol;
2
+ export const kPluginManagerStop: unique symbol;
3
+ export const kPluginManagerInstances: unique symbol;
1
4
  export default BasePluginManager;
2
5
  /**
3
- * Shared functionnality between server-side and client-size plugin manager
6
+ * Callback executed when a plugin internal state is updated.
7
+ */
8
+ export type pluginManagerOnStateChangeCallback = (: object, initiator: ClientPlugin | ServerPlugin | null) => any;
9
+ /**
10
+ * Delete the registered {@link pluginManagerOnStateChangeCallback}.
11
+ */
12
+ export type pluginManagerDeleteOnStateChangeCallback = () => any;
13
+ /**
14
+ * Callback executed when a plugin internal state is updated.
4
15
  *
5
- * @private
16
+ * @callback pluginManagerOnStateChangeCallback
17
+ * @param {object<string, ClientPlugin|ServerPlugin>} fullState - List of all plugins.
18
+ * @param {ClientPlugin|ServerPlugin|null} initiator - Plugin that initiated the
19
+ * update. The value is `null` if the change was initiated by the state manager
20
+ * (e.g. when the initialization of the plugins starts).
6
21
  */
22
+ /**
23
+ * Delete the registered {@link pluginManagerOnStateChangeCallback}.
24
+ *
25
+ * @callback pluginManagerDeleteOnStateChangeCallback
26
+ */
27
+ /** @private */
7
28
  declare class BasePluginManager {
8
29
  constructor(node: any);
9
- /** @private */
10
- private _node;
11
- /** @private */
12
- private _registeredPlugins;
13
- /** @private */
14
- private _instances;
15
- /** @private */
16
- private _instanceStartPromises;
17
- /** @private */
18
- private _onStateChangeCallbacks;
19
- status: string;
20
30
  /**
21
- * Register a plugin into soundworks.
31
+ * Status of the plugin manager
32
+ *
33
+ * @type {'idle'|'inited'|'started'|'errored'}
34
+ */
35
+ get status(): "idle" | "inited" | "started" | "errored";
36
+ /**
37
+ * Alias for existing plugins (i.e. plugin-scripting), remove once updated
38
+ * @private
39
+ * @deprecated
40
+ */
41
+ private unsafeGet;
42
+ /**
43
+ * Retrieve an fully started instance of a registered plugin without checking
44
+ * that the pluginManager is started.
45
+ *
46
+ * This method is required for starting the plugin manager itself and to require
47
+ * a plugin from within another plugin.
48
+ *
49
+ * _Warning: Unless you are developing your own plugins, you should not have to use
50
+ * this method_
51
+ */
52
+ getUnsafe(id: any): Promise<any>;
53
+ /**
54
+ * Register a plugin into the manager.
22
55
  *
23
56
  * _A plugin must always be registered both on client-side and on server-side_
24
57
  *
25
- * Refer to the plugin documentation to check its options and proper way of
26
- * registering it.
58
+ * Plugins must be registered between the instantiation of {@link Client} and {@link Server},
59
+ * and their respective initialization, i.e.:
60
+ *
61
+ * ```js
62
+ * const client = new Client(config);
63
+ * client.pluginManager.register('my-plugin', plugin);
64
+ * await client.start();
65
+ * ```
66
+ *
67
+ * Refer to the plugins documentation to check their configuration options and API.
27
68
  *
28
69
  * @param {string} id - Unique id of the plugin. Enables the registration of the
29
70
  * same plugin factory under different ids.
30
- * @param {Function} factory - Factory function that returns the Plugin class.
71
+ * @param {Function} ctor - The class returned by the plugin factory method.
31
72
  * @param {object} [options={}] - Options to configure the plugin.
32
73
  * @param {array} [deps=[]] - List of plugins' names the plugin depends on, i.e.
33
- * the plugin initialization will start only after the plugins it depends on are
74
+ * the plugin initialization will begin only after the plugins it depends on are
34
75
  * fully started themselves.
35
- * @see {@link client.PluginManager#register}
36
- * @see {@link server.PluginManager#register}
76
+ * @see {@link ClientPluginManager#register}
77
+ * @see {@link ServerPluginManager#register}
37
78
  * @example
38
79
  * // client-side
39
80
  * client.pluginManager.register('user-defined-id', pluginFactory);
40
81
  * // server-side
41
82
  * server.pluginManager.register('user-defined-id', pluginFactory);
42
83
  */
43
- register(id: string, ctor: any, options?: object, deps?: any[]): void;
84
+ register(id: string, ctor: Function, options?: object, deps?: any[]): void;
44
85
  /**
45
- * Initialize all the registered plugin. Executed during the `Client.init()` or
46
- * `Server.init()` initialization step.
47
- * @private
86
+ * Manually add a dependency to a given plugin.
87
+ *
88
+ * Useful to require a plugin within a plugin
48
89
  */
49
- private start;
50
- stop(): Promise<void>;
90
+ addDependency(pluginId: any, dependencyId: any): void;
51
91
  /**
52
- * Retrieve an fully started instance of a registered plugin, without checking
53
- * that the pluginManager is started. This is important for starting the plugin
54
- * manager itself.
55
- *
56
- * @private
92
+ * Returns the list of the registered plugins ids
93
+ * @returns {string[]}
57
94
  */
58
- private getUnsafe;
95
+ getRegisteredPlugins(): string[];
59
96
  /**
60
97
  * Propagate a notification each time a plugin is updated (status or inner state).
61
98
  * The callback will receive the list of all plugins as first parameter, and the
@@ -63,10 +100,8 @@ declare class BasePluginManager {
63
100
  *
64
101
  * _In most cases, you should not have to rely on this method._
65
102
  *
66
- * @param {client.PluginManager~onStateChangeCallback|server.PluginManager~onStateChangeCallback} callback
67
- * Callback to be executed on state change
68
- * @param {client.PluginManager~deleteOnStateChangeCallback|client.PluginManager~deleteOnStateChangeCallback}
69
- * Function to execute to listening for changes.
103
+ * @param {pluginManagerOnStateChangeCallback} callback - Callback to execute on state change
104
+ * @returns {pluginManagerDeleteOnStateChangeCallback} - Clear the subscription when executed
70
105
  * @example
71
106
  * const unsubscribe = client.pluginManager.onStateChange(pluginList, initiator => {
72
107
  * // log the current status of all plugins
@@ -81,8 +116,18 @@ declare class BasePluginManager {
81
116
  * // stop listening for updates later
82
117
  * unsubscribe();
83
118
  */
84
- onStateChange(callback: any): () => boolean;
119
+ onStateChange(callback: pluginManagerOnStateChangeCallback): pluginManagerDeleteOnStateChangeCallback;
120
+ /**
121
+ * Initialize all registered plugins.
122
+ *
123
+ * Executed during the {@link Client#init} or {@link Client#stop} initialization step.
124
+ *
125
+ * @private
126
+ */
127
+ private [kPluginManagerStart];
128
+ /** @private */
129
+ private [kPluginManagerStop];
85
130
  /** @private */
86
- private _propagateStateChange;
131
+ private [kPluginManagerInstances];
132
+ #private;
87
133
  }
88
- //# sourceMappingURL=BasePluginManager.d.ts.map