@soundworks/core 4.0.0-alpha.28 → 4.0.0-alpha.29

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 (127) hide show
  1. package/.jsdoc +3 -1
  2. package/CHANGELOG.md +11 -5
  3. package/package.json +5 -5
  4. package/src/client/Client.js +78 -83
  5. package/src/client/{Context.js → ClientContext.js} +48 -38
  6. package/src/client/ClientContextManager.js +115 -0
  7. package/src/client/{Plugin.js → ClientPlugin.js} +22 -18
  8. package/src/client/{PluginManager.js → ClientPluginManager.js} +39 -35
  9. package/src/client/{Socket.js → ClientSocket.js} +54 -65
  10. package/src/client/{StateManager.js → ClientStateManager.js} +7 -8
  11. package/src/client/index.js +17 -41
  12. package/src/common/BasePlugin.js +56 -51
  13. package/src/common/BasePluginManager.js +175 -139
  14. package/src/common/BaseStateManager.js +316 -120
  15. package/src/common/BatchedTransport.js +24 -24
  16. package/src/common/ParameterBag.js +18 -6
  17. package/src/common/PromiseStore.js +1 -0
  18. package/src/common/{BaseSharedState.js → SharedState.js} +271 -199
  19. package/src/common/{BaseSharedStateCollection.js → SharedStateCollection.js} +92 -61
  20. package/src/common/logger.js +5 -0
  21. package/src/common/shared-state-types.js +76 -0
  22. package/src/common/version.js +1 -1
  23. package/src/server/Server.js +391 -354
  24. package/src/server/ServerClient.js +73 -0
  25. package/src/server/{Context.js → ServerContext.js} +85 -66
  26. package/src/server/{ContextManager.js → ServerContextManager.js} +98 -85
  27. package/src/server/{Plugin.js → ServerPlugin.js} +10 -24
  28. package/src/server/{PluginManager.js → ServerPluginManager.js} +76 -69
  29. package/src/server/{Socket.js → ServerSocket.js} +19 -18
  30. package/src/server/{Sockets.js → ServerSockets.js} +36 -27
  31. package/src/server/ServerStateManager.js +515 -0
  32. package/src/{common → server}/SharedStatePrivate.js +61 -26
  33. package/src/server/audit-schema.js +1 -1
  34. package/src/server/crypto.js +2 -0
  35. package/src/server/index.js +18 -21
  36. package/types/client/Client.d.ts +74 -95
  37. package/types/client/{Context.d.ts → ClientContext.d.ts} +25 -26
  38. package/types/client/{ContextManager.d.ts → ClientContextManager.d.ts} +19 -22
  39. package/types/client/{Plugin.d.ts → ClientPlugin.d.ts} +17 -20
  40. package/types/client/{PluginManager.d.ts → ClientPluginManager.d.ts} +13 -36
  41. package/types/client/ClientSocket.d.ts +74 -0
  42. package/types/client/{StateManager.d.ts → ClientStateManager.d.ts} +11 -15
  43. package/types/client/index.d.ts +26 -2
  44. package/types/common/BasePlugin.d.ts +52 -46
  45. package/types/common/BasePluginManager.d.ts +68 -44
  46. package/types/common/BaseStateManager.d.ts +207 -97
  47. package/types/common/BatchedTransport.d.ts +1 -5
  48. package/types/common/ParameterBag.d.ts +16 -18
  49. package/types/common/PromiseStore.d.ts +1 -1
  50. package/types/common/SharedState.d.ts +297 -0
  51. package/types/common/{BaseSharedStateCollection.d.ts → SharedStateCollection.d.ts} +34 -18
  52. package/types/common/constants.d.ts +0 -2
  53. package/types/common/logger.d.ts +2 -1
  54. package/types/common/shared-state-types.d.ts +83 -0
  55. package/types/common/sockets-utils.d.ts +0 -5
  56. package/types/common/version.d.ts +1 -2
  57. package/types/server/Server.d.ts +78 -132
  58. package/types/server/ServerClient.d.ts +45 -0
  59. package/types/server/{Context.d.ts → ServerContext.d.ts} +53 -43
  60. package/types/server/ServerContextManager.d.ts +80 -0
  61. package/types/server/{Plugin.d.ts → ServerPlugin.d.ts} +19 -33
  62. package/types/server/{PluginManager.d.ts → ServerPluginManager.d.ts} +39 -37
  63. package/types/server/ServerSocket.d.ts +85 -0
  64. package/types/server/ServerSockets.d.ts +50 -0
  65. package/types/server/ServerStateManager.d.ts +189 -0
  66. package/types/server/SharedStatePrivate.d.ts +21 -0
  67. package/types/server/audit-network-latency.worker.d.ts +1 -2
  68. package/types/server/audit-schema.d.ts +0 -1
  69. package/types/server/crypto.d.ts +2 -1
  70. package/types/server/index.d.ts +24 -2
  71. package/src/client/ContextManager.js +0 -111
  72. package/src/client/SharedState.js +0 -84
  73. package/src/client/SharedStateCollection.js +0 -38
  74. package/src/server/Client.js +0 -55
  75. package/src/server/SharedState.js +0 -85
  76. package/src/server/SharedStateCollection.js +0 -39
  77. package/src/server/StateManager.js +0 -696
  78. package/types/client/Client.d.ts.map +0 -1
  79. package/types/client/Context.d.ts.map +0 -1
  80. package/types/client/ContextManager.d.ts.map +0 -1
  81. package/types/client/Plugin.d.ts.map +0 -1
  82. package/types/client/PluginManager.d.ts.map +0 -1
  83. package/types/client/SharedState.d.ts +0 -85
  84. package/types/client/SharedState.d.ts.map +0 -1
  85. package/types/client/SharedStateCollection.d.ts +0 -41
  86. package/types/client/SharedStateCollection.d.ts.map +0 -1
  87. package/types/client/Socket.d.ts +0 -149
  88. package/types/client/Socket.d.ts.map +0 -1
  89. package/types/client/StateManager.d.ts.map +0 -1
  90. package/types/client/index.d.ts.map +0 -1
  91. package/types/common/BasePlugin.d.ts.map +0 -1
  92. package/types/common/BasePluginManager.d.ts.map +0 -1
  93. package/types/common/BaseSharedState.d.ts +0 -244
  94. package/types/common/BaseSharedState.d.ts.map +0 -1
  95. package/types/common/BaseSharedStateCollection.d.ts.map +0 -1
  96. package/types/common/BaseStateManager.d.ts.map +0 -1
  97. package/types/common/BatchedTransport.d.ts.map +0 -1
  98. package/types/common/ParameterBag.d.ts.map +0 -1
  99. package/types/common/PromiseStore.d.ts.map +0 -1
  100. package/types/common/SharedStatePrivate.d.ts +0 -21
  101. package/types/common/SharedStatePrivate.d.ts.map +0 -1
  102. package/types/common/constants.d.ts.map +0 -1
  103. package/types/common/logger.d.ts.map +0 -1
  104. package/types/common/sockets-utils.d.ts.map +0 -1
  105. package/types/common/version.d.ts.map +0 -1
  106. package/types/server/Client.d.ts +0 -45
  107. package/types/server/Client.d.ts.map +0 -1
  108. package/types/server/Context.d.ts.map +0 -1
  109. package/types/server/ContextManager.d.ts +0 -69
  110. package/types/server/ContextManager.d.ts.map +0 -1
  111. package/types/server/Plugin.d.ts.map +0 -1
  112. package/types/server/PluginManager.d.ts.map +0 -1
  113. package/types/server/Server.d.ts.map +0 -1
  114. package/types/server/SharedState.d.ts +0 -86
  115. package/types/server/SharedState.d.ts.map +0 -1
  116. package/types/server/SharedStateCollection.d.ts +0 -41
  117. package/types/server/SharedStateCollection.d.ts.map +0 -1
  118. package/types/server/Socket.d.ts +0 -164
  119. package/types/server/Socket.d.ts.map +0 -1
  120. package/types/server/Sockets.d.ts +0 -80
  121. package/types/server/Sockets.d.ts.map +0 -1
  122. package/types/server/StateManager.d.ts +0 -405
  123. package/types/server/StateManager.d.ts.map +0 -1
  124. package/types/server/audit-network-latency.worker.d.ts.map +0 -1
  125. package/types/server/audit-schema.d.ts.map +0 -1
  126. package/types/server/crypto.d.ts.map +0 -1
  127. package/types/server/index.d.ts.map +0 -1
@@ -1,14 +1,15 @@
1
+ export const kServerOnSocketConnection: unique symbol;
2
+ export const kServerIsProtectedRole: unique symbol;
3
+ export const kServerIsValidConnectionToken: unique symbol;
4
+ export const kServerOnStatusChangeCallbacks: unique symbol;
5
+ export const kServerApplicationTemplateOptions: unique symbol;
1
6
  export default Server;
2
- /**
3
- * Configuration object for the server.
4
- */
5
- export type ServerConfig = any;
6
7
  /**
7
8
  * The `Server` class is the main entry point for the server-side of a soundworks
8
9
  * application.
9
10
  *
10
- * The `Server` instance allows to access soundworks components such as {@link server.StateManager},
11
- * {@link server.PluginManager},{@link server.Socket} or {@link server.ContextManager}.
11
+ * The `Server` instance allows to access soundworks components such as {@link ServerStateManager},
12
+ * {@link ServerPluginManager}, {@link ServerSocket} or {@link ServerContextManager}.
12
13
  * Its is also responsible for handling the initialization lifecycles of the different
13
14
  * soundworks components.
14
15
  *
@@ -47,12 +48,10 @@ export type ServerConfig = any;
47
48
  * The server will listen to the following URLs:
48
49
  * - `http://127.0.0.1:8000/` for the `player` role, which is defined as the default client.
49
50
  * - `http://127.0.0.1:8000/controller` for the `controller` role.
50
- *
51
- * @memberof server
52
51
  */
53
52
  declare class Server {
54
53
  /**
55
- * @param {server.ServerConfig} config - Configuration object for the server.
54
+ * @param {ServerConfig} config - Configuration object for the server.
56
55
  * @throws
57
56
  * - If `config.app.clients` is empty.
58
57
  * - If a `node` client is defined but `config.env.serverAddress` is not defined.
@@ -60,9 +59,9 @@ declare class Server {
60
59
  * (which generates self signed certificated), `config.env.httpsInfos.cert` and
61
60
  * `config.env.httpsInfos.key` should point to valid cert files.
62
61
  */
63
- constructor(config: server.ServerConfig);
62
+ constructor(config: ServerConfig);
64
63
  /**
65
- * @description Given config object merged with the following defaults:
64
+ * Given config object merged with the following defaults:
66
65
  * @example
67
66
  * {
68
67
  * env: {
@@ -70,10 +69,6 @@ declare class Server {
70
69
  * port: 8000,
71
70
  * serverAddress: null,
72
71
  * subpath: '',
73
- * websockets: {
74
- * path: 'socket',
75
- * pingInterval: 5000,
76
- * },
77
72
  * useHttps: false,
78
73
  * httpsInfos: null,
79
74
  * crossOriginIsolated: true,
@@ -84,9 +79,27 @@ declare class Server {
84
79
  * clients: {},
85
80
  * }
86
81
  * }
82
+ * @type {ServerConfig}
83
+ */
84
+ get config(): ServerConfig;
85
+ /**
86
+ * Package version.
87
+ *
88
+ * @type {string}
87
89
  */
88
- config: any;
89
- version: string;
90
+ get version(): string;
91
+ /**
92
+ * Id of the server, a constant set to -1
93
+ * @type {number}
94
+ * @readonly
95
+ */
96
+ readonly get id(): number;
97
+ /**
98
+ * Status of the server.
99
+ *
100
+ * @type {'idle'|'inited'|'started'|'errored'}
101
+ */
102
+ get status(): "idle" | "inited" | "started" | "errored";
90
103
  /**
91
104
  * Instance of the express router.
92
105
  *
@@ -104,112 +117,74 @@ declare class Server {
104
117
  * // expose assets located in the `soundfiles` directory on the network
105
118
  * server.router.use('/soundfiles', express.static('soundfiles')));
106
119
  */
107
- router: any;
120
+ get router(): any;
108
121
  /**
109
122
  * Raw Node.js `http` or `https` instance
110
123
  *
111
124
  * @see {@link https://nodejs.org/api/http.html}
112
125
  * @see {@link https://nodejs.org/api/https.html}
113
126
  */
114
- httpServer: any;
127
+ get httpServer(): any;
115
128
  /**
116
- * Instance of the {@link server.Sockets} class.
129
+ * Simple key / value filesystem database with Promise based Map API.
117
130
  *
118
- * @see {@link server.Sockets}
119
- * @type {server.Sockets}
131
+ * Basically a tiny wrapper around the {@link https://github.com/lukechilds/keyv} package.
120
132
  */
121
- sockets: server.Sockets;
133
+ get db(): any;
122
134
  /**
123
- * Instance of the {@link server.PluginManager} class.
135
+ * Instance of the {@link ServerSockets} class.
124
136
  *
125
- * @see {@link server.PluginManager}
126
- * @type {server.PluginManager}
137
+ * @type {ServerSockets}
127
138
  */
128
- pluginManager: server.PluginManager;
139
+ get sockets(): ServerSockets;
129
140
  /**
130
- * Instance of the {@link server.StateManager} class.
141
+ * Instance of the {@link ServerPluginManager} class.
131
142
  *
132
- * @see {@link server.StateManager}
133
- * @type {server.StateManager}
143
+ * @type {ServerPluginManager}
134
144
  */
135
- stateManager: server.StateManager;
145
+ get pluginManager(): ServerPluginManager;
136
146
  /**
137
- * Instance of the {@link server.ContextManager} class.
147
+ * Instance of the {@link ServerStateManager} class.
138
148
  *
139
- * @see {@link server.ContextManager}
140
- * @type {server.ContextManager}
141
- */
142
- contextManager: server.ContextManager;
143
- /**
144
- * If `https` is required, hold informations about the certificates, e.g. if
145
- * self-signed, the dates of validity of the certificates, etc.
149
+ * @type {ServerStateManager}
146
150
  */
147
- httpsInfos: {
148
- selfSigned: boolean;
149
- CN: string;
150
- altNames: string[];
151
- validFrom: string;
152
- validTo: string;
153
- isValid: boolean;
154
- daysRemaining: number;
155
- } | {
156
- selfSigned: boolean;
157
- CN?: undefined;
158
- altNames?: undefined;
159
- validFrom?: undefined;
160
- validTo?: undefined;
161
- isValid?: undefined;
162
- daysRemaining?: undefined;
163
- } | {
164
- selfSigned: boolean;
165
- CN?: undefined;
166
- altNames?: undefined;
167
- validFrom?: undefined;
168
- validTo?: undefined;
169
- isValid?: undefined;
170
- daysRemaining?: undefined;
171
- };
151
+ get stateManager(): ServerStateManager;
172
152
  /**
173
- * Status of the server, 'idle', 'inited', 'started' or 'errored'.
153
+ * Instance of the {@link ServerContextManager} class.
174
154
  *
175
- * @type {string}
155
+ * @type {ServerContextManager}
176
156
  */
177
- status: string;
157
+ get contextManager(): ServerContextManager;
178
158
  /**
179
- * Simple key / value database with Promise based Map API store on filesystem,
180
- * basically a tiny wrapper around the `kvey` package.
159
+ * Register a callback to execute when status change
181
160
  *
182
- * @private
183
- * @see {@link https://github.com/lukechilds/keyv}
161
+ * @param {function} callback
184
162
  */
185
- private db;
186
- /** @private */
187
- private _applicationTemplateOptions;
188
- /** @private */
189
- private _onStatusChangeCallbacks;
190
- /** @private */
191
- private _onClientConnectCallbacks;
192
- /** @private */
193
- private _onClientDisconnectCallbacks;
194
- /** @private */
195
- private _auditState;
196
- /** @private */
197
- private _pendingConnectionTokens;
198
- /** @private */
199
- private _trustedClients;
163
+ onStatusChange(callback: Function): () => any;
200
164
  /**
201
- * Id of the server, a constant set to -1
202
- * @type {Number}
203
- * @readonly
165
+ * Attach and retrieve the global audit state of the application.
166
+ *
167
+ * The audit state is a {@link SharedState} instance that keeps track of
168
+ * global informations about the application such as, the number of connected
169
+ * clients, network latency estimation, etc.
170
+ *
171
+ * The audit state is created by the server on start up.
172
+ *
173
+ * @returns {Promise<SharedState>}
174
+ * @throws Will throw if called before `server.init()`
175
+ *
176
+ * @example
177
+ * const auditState = await server.getAuditState();
178
+ * auditState.onUpdate(() => console.log(auditState.getValues()), true);
204
179
  */
205
- readonly get id(): number;
180
+ getAuditState(): Promise<SharedState>;
206
181
  /**
207
182
  * The `init` method is part of the initialization lifecycle of the `soundworks`
208
183
  * server. Most of the time, the `init` method will be implicitly called by the
209
- * {@link server.Server#start} method.
184
+ * {@link Server#start} method.
210
185
  *
211
186
  * In some situations you might want to call this method manually, in such cases
212
- * the method should be called before the {@link server.Server#start} method.
187
+ * the method should be called before the {@link Server#start} method.
213
188
  *
214
189
  * What it does:
215
190
  * - create the audit state
@@ -217,7 +192,7 @@ declare class Server {
217
192
  * declared in `config.app.clients`
218
193
  * - initialize all registered plugins
219
194
  *
220
- * After `await server.init()` is fulfilled, the {@link server.Server#stateManager}
195
+ * After `await server.init()` is fulfilled, the {@link Server#stateManager}
221
196
  * and all registered plugins can be safely used.
222
197
  *
223
198
  * @example
@@ -231,11 +206,11 @@ declare class Server {
231
206
  init(): Promise<void>;
232
207
  /**
233
208
  * The `start` method is part of the initialization lifecycle of the `soundworks`
234
- * server. The `start` method will implicitly call the {@link server.Server#init}
209
+ * server. The `start` method will implicitly call the {@link Server#init}
235
210
  * method if it has not been called manually.
236
211
  *
237
212
  * What it does:
238
- * - implicitely call {@link server.Server#init} if not done manually
213
+ * - implicitely call {@link Server#init} if not done manually
239
214
  * - launch the HTTP and WebSocket servers
240
215
  * - start all created contexts. To this end, you will have to call `server.init`
241
216
  * manually and instantiate the contexts between `server.init()` and `server.start()`
@@ -267,18 +242,8 @@ declare class Server {
267
242
  * await server.stop();
268
243
  */
269
244
  stop(): Promise<void>;
270
- /**
271
- * Open the route for a given client.
272
- * @private
273
- */
274
- private _openClientRoute;
275
245
  onClientConnect(callback: any): () => boolean;
276
246
  onClientDisconnect(callback: any): () => boolean;
277
- /**
278
- * Socket connection callback.
279
- * @private
280
- */
281
- private _onSocketConnection;
282
247
  /**
283
248
  * Create namespaced databases for core and plugins
284
249
  * (kind of experimental API do not expose in doc for now)
@@ -288,9 +253,6 @@ declare class Server {
288
253
  * @private
289
254
  */
290
255
  private createNamespacedDb;
291
- onStatusChange(callback: any): () => boolean;
292
- /** @private */
293
- private _dispatchStatus;
294
256
  /**
295
257
  * Configure the server to work _out-of-the-box_ within the soundworks application
296
258
  * template provided by `@soundworks/create.
@@ -317,35 +279,14 @@ declare class Server {
317
279
  * first to explain your use-case :)
318
280
  */
319
281
  setCustomApplicationTemplateOptions(options: any): void;
320
- /**
321
- * Attach and retrieve the global audit state of the application.
322
- *
323
- * The audit state is a {@link server.SharedState} instance that keeps track of
324
- * global informations about the application such as, the number of connected
325
- * clients, network latency estimation, etc.
326
- *
327
- * The audit state is created by the server on start up.
328
- *
329
- * @returns {Promise<server.SharedState>}
330
- * @throws Will throw if called before `server.init()`
331
- * @see {@link server.SharedState}
332
- * @example
333
- * const auditState = await server.getAuditState();
334
- * auditState.onUpdate(() => console.log(auditState.getValues()), true);
335
- */
336
- getAuditState(): Promise<server.SharedState>;
337
- /** @private */
338
- private isProtected;
339
- /** @private */
340
- private isValidConnectionToken;
341
282
  /**
342
283
  * Check if the given client is trusted, i.e. config.env.type == 'production'
343
284
  * and the client is protected behind a password.
344
285
  *
345
- * @param {server.Client} client - Client to be tested
286
+ * @param {ServerClient} client - Client to be tested
346
287
  * @returns {Boolean}
347
288
  */
348
- isTrustedClient(client: server.Client): boolean;
289
+ isTrustedClient(client: ServerClient): boolean;
349
290
  /**
350
291
  * Check if the token from a client is trusted, i.e. config.env.type == 'production'
351
292
  * and the client is protected behind a password.
@@ -356,5 +297,10 @@ declare class Server {
356
297
  * @returns {Boolean}
357
298
  */
358
299
  isTrustedToken(clientId: number, clientIp: number, token: string): boolean;
300
+ #private;
359
301
  }
360
- //# sourceMappingURL=Server.d.ts.map
302
+ import ServerSockets from './ServerSockets.js';
303
+ import ServerPluginManager from './ServerPluginManager.js';
304
+ import ServerStateManager from './ServerStateManager.js';
305
+ import ServerContextManager from './ServerContextManager.js';
306
+ import ServerClient from './ServerClient.js';
@@ -0,0 +1,45 @@
1
+ export const kServerClientToken: unique symbol;
2
+ export default ServerClient;
3
+ /**
4
+ * Server-side representation of a `soundworks` client.
5
+ *
6
+ * @hideconstructor
7
+ * @see {@link Client}
8
+ */
9
+ declare class ServerClient {
10
+ /**
11
+ * @param {String} role - Role of the client
12
+ * @param {ServerSocket} socket - Socket connection with the client
13
+ */
14
+ constructor(role: string, socket: ServerSocket);
15
+ /**
16
+ * Client role, as specified in client side config {@link Client}.
17
+ *
18
+ * @type {String}
19
+ */
20
+ get role(): string;
21
+ /**
22
+ * Session Id (incremented positive number).
23
+ *
24
+ * @type {Number}
25
+ */
26
+ get id(): number;
27
+ /**
28
+ * Unique session Id (uuidv4).
29
+ *
30
+ * @type {String}
31
+ */
32
+ get uuid(): string;
33
+ /**
34
+ * Socket connection with the remote {@link Client}.
35
+ *
36
+ * @type {ServerSocket}
37
+ */
38
+ get socket(): ServerSocket;
39
+ /**
40
+ * Is set in server[kServerOnSocketConnection]
41
+ * @private
42
+ */
43
+ private [kServerClientToken];
44
+ #private;
45
+ }
@@ -1,29 +1,31 @@
1
- export default Context;
1
+ export const kServerContextStatus: unique symbol;
2
+ export default ServerContext;
2
3
  /**
3
4
  * Base class to extend in order to implment the optionnal server-side counterpart
4
- * of a {@link client.Context}. If not defined, a default context will be created
5
+ * of a {@link ClientContext}. If not defined, a default context will be created
5
6
  * and used by the server.
6
7
  *
7
8
  * In the `soundworks` paradigm, a client has a "role" (e.g. _player_, _controller_)
8
- * see {@link client.Client#role}) and can be in different "contexts" (e.g. different
9
+ * see {@link Client#role}) and can be in different "contexts" (e.g. different
9
10
  * part of the experience such as sections of a music piece, etc.). The
10
- * {@link client.Context} and optionnal {@link serverContext} abstractions provide
11
+ * {@link ClientContext} and optionnal {@link ServerContext} abstractions provide
11
12
  * a simple and unified way to model these reccuring aspects of an application.
12
13
  *
13
- * If a `server.Context` is recognized as the server-side counterpart of a
14
- * {@link client.Context}, based on their respective `name` (see {@link client.Context#name}
15
- * and {@link server.Context#name}), `soundworks` will ensure the logic defined
16
- * by the server-side Context will be executed at the beginning of the
17
- * {@link client.Client#enter} and {@link client.Client#exit} methods. The example
18
- * show how soundwords handles (and guarantees) the order of the `enter()` steps
19
- * between the client-side and the server-side parts of the context. The same goes
20
- * for the `exit()` method.
14
+ * If a `ServerContext` is recognized as the server-side counterpart of a
15
+ * {@link ClientContext}, based on their respective `name` (see {@link ClientContext#name}
16
+ * and {@link ServerContext#name}), `soundworks` will ensure the logic defined
17
+ * by the ServerContext will be executed at the beginning of the
18
+ * {@link ClientContext#enter} and {@link ClientContext#exit} methods.
21
19
  *
22
- * ```
20
+ * The example above shows how soundwords handles (and guarantees) the order of
21
+ * the `enter()` steps between the client-side and the server-side parts of the
22
+ * context. The same goes for the `exit()` method.
23
+ *
24
+ * ```js
23
25
  * // client-side
24
- * import { Context } from '@soundworks/core/client.js';
26
+ * import { ClientContext } from '@soundworks/core/client.js';
25
27
  *
26
- * class MyContext extends Context {
28
+ * class MyContext extends ClientContext {
27
29
  * async enter() {
28
30
  * // 1. client side context enter() starts
29
31
  * // server-side logic is triggered first
@@ -44,11 +46,11 @@ export default Context;
44
46
  * await myContext.enter();
45
47
  * ```
46
48
  *
47
- * ```
49
+ * ```js
48
50
  * // server-side
49
- * import { Context } from '@soundworks/core/server.js';
51
+ * import { ServerContext } from '@soundworks/core/server.js';
50
52
  *
51
- * class MyContext extends Context {
53
+ * class MyContext extends ServerContext {
52
54
  * async enter(client) {
53
55
  * // 2. server-side context enter() starts
54
56
  * await super.enter(client);
@@ -59,14 +61,12 @@ export default Context;
59
61
  * }
60
62
  *
61
63
  * // Instantiate the context
62
- * const myContext = new Context(server);
64
+ * const myContext = new MyContext(server);
63
65
  * ```
64
- *
65
- * @memberof server
66
66
  */
67
- declare class Context {
67
+ declare class ServerContext {
68
68
  /**
69
- * @param {client.Client} client - The soundworks client instance.
69
+ * @param {Server} server - The soundworks server instance.
70
70
  * @param {string|string[]} [roles=[]] - Optionnal list of client roles that can
71
71
  * use this context. In large applications, this may be usefull to guarantee
72
72
  * that a context can be consumed only by specific client roles, throwing an
@@ -74,34 +74,40 @@ declare class Context {
74
74
  * will be used.
75
75
  * @throws Will throw if the first argument is not a soundworks server instance.
76
76
  */
77
- constructor(server: any, roles?: string | string[]);
77
+ constructor(server: Server, roles?: string | string[]);
78
78
  /**
79
- * soundworks server
80
- * @type {server.Server}
81
- * @readonly
79
+ * The soundworks server instance.
80
+ *
81
+ * @type {Server}
82
82
  */
83
- readonly server: server.Server;
83
+ get server(): Server;
84
84
  /**
85
- * List of clients that are currently in this context
86
- * @type {server.Client[]}
85
+ * List of clients that are currently in this context.
86
+ *
87
+ * @type {Set<ServerClient>}
87
88
  */
88
- clients: server.Client[];
89
+ get clients(): Set<ServerClient>;
90
+ /**
91
+ * List of client roles that can use this context. No access policy if empty.
92
+ *
93
+ * @type {Set<string>}
94
+ */
95
+ get roles(): Set<string>;
89
96
  /**
90
97
  * Status of the context ('idle', 'inited', 'started' or 'errored')
98
+ *
91
99
  * @type {string}
92
- * @readonly
93
100
  */
94
- readonly status: string;
95
- roles: Set<string>;
101
+ get status(): string;
96
102
  /**
97
103
  * Optionnal user-defined name of the context (defaults to the class name).
98
104
  *
99
105
  * The context manager will match the client-side and server-side contexts based
100
- * on this name. If the {@link server.ContextManager} don't find a corresponding
106
+ * on this name. If the {@link ServerContextManager} don't find a corresponding
101
107
  * user-defined context with the same name, it will use a default (noop) context.
102
108
  *
103
- * @readonly
104
109
  * @type {string}
110
+ *
105
111
  * @example
106
112
  * // server-side and client-side contexts are matched based on their respective `name`
107
113
  * class MyContext extends Context {
@@ -110,10 +116,10 @@ declare class Context {
110
116
  * }
111
117
  * }
112
118
  */
113
- readonly get name(): string;
119
+ get name(): string;
114
120
  /**
115
121
  * Start the context. This method is lazilly called when a client enters the
116
- * context for the first time (cf. ${server.Context#enter}). If you know some
122
+ * context for the first time (cf. ${ServerContext#enter}). If you know some
117
123
  * some heavy and/or potentially long job has to be done when starting the context
118
124
  * (e.g. connect to a database, parsing a long file) it may be a good practice
119
125
  * to call it explicitely.
@@ -155,7 +161,7 @@ declare class Context {
155
161
  *
156
162
  * _WARNING: this method should never be called manually._
157
163
  *
158
- * @param {server.Client} client - Server-side representation of the client
164
+ * @param {ServerClient} client - Server-side representation of the client
159
165
  * that enters the context.
160
166
  * @returns {Promise} - Promise that resolves when the context is entered.
161
167
  * @example
@@ -171,14 +177,14 @@ declare class Context {
171
177
  * }
172
178
  * }
173
179
  */
174
- enter(client: server.Client): Promise<any>;
180
+ enter(client: ServerClient): Promise<any>;
175
181
  /**
176
182
  * Exit the context. Implement this method to define the logic that should be
177
183
  * done (e.g. delete a shared state, etc.) when a client exits the context.
178
184
  *
179
185
  * * _WARNING: this method should never be called manually._
180
186
  *
181
- * @param {server.Client} client - Server-side representation of the client
187
+ * @param {ServerClient} client - Server-side representation of the client
182
188
  * that exits the context.
183
189
  * @returns {Promise} - Promise that resolves when the context is exited.
184
190
  * @example
@@ -194,6 +200,10 @@ declare class Context {
194
200
  * }
195
201
  * }
196
202
  */
197
- exit(client: server.Client): Promise<any>;
203
+ exit(client: ServerClient): Promise<any>;
204
+ /** @private */
205
+ private [kServerContextStatus];
206
+ #private;
198
207
  }
199
- //# sourceMappingURL=Context.d.ts.map
208
+ import Server from './Server.js';
209
+ import ServerClient from './ServerClient.js';
@@ -0,0 +1,80 @@
1
+ export const kServerContextManagerStart: unique symbol;
2
+ export const kServerContextManagerStop: unique symbol;
3
+ export const kServerContextManagerAddClient: unique symbol;
4
+ export const kServerContextManagerRemoveClient: unique symbol;
5
+ export const kServerContextManagerRegister: unique symbol;
6
+ export const kServerContextManagerContexts: unique symbol;
7
+ export default ServerContextManager;
8
+ /**
9
+ * Manage the different server-side contexts and their lifecycle.
10
+ *
11
+ * The `ServerContextManager` is automatically instantiated by the {@link Server}.
12
+ *
13
+ * _WARNING: Most of the time, you should not have to manipulate the context manager directly._
14
+ *
15
+ * @hideconstructor
16
+ */
17
+ declare class ServerContextManager {
18
+ /**
19
+ * @param {Server} server - Instance of the soundworks server.
20
+ */
21
+ constructor(server: Server);
22
+ /**
23
+ * Retrieve a started context from its name.
24
+ *
25
+ * _WARNING: Most of the time, you should not have to call this method manually._
26
+ *
27
+ * @param {ServerContext#name} contextName - Name of the context.
28
+ */
29
+ get(contextName: any): Promise<any>;
30
+ /**
31
+ * Register a context in the manager.
32
+ * This method is called in the {@link ServerContext} constructor
33
+ *
34
+ * @param {ServerContext} context - Context instance to register.
35
+ *
36
+ * @private
37
+ */
38
+ private [kServerContextManagerRegister];
39
+ /**
40
+ * Start all contexts registered before `await server.start()`.
41
+ * Called on {@link Server#start}
42
+ *
43
+ * @private
44
+ */
45
+ private [kServerContextManagerStart];
46
+ /**
47
+ * Stop all contexts. Called on {@link Server#stop}
48
+ *
49
+ * @private
50
+ */
51
+ private [kServerContextManagerStop];
52
+ /**
53
+ * Called when a client connects to the server (websocket handshake)
54
+ *
55
+ * @param {ServerClient} client
56
+ *
57
+ * @private
58
+ */
59
+ private [kServerContextManagerAddClient];
60
+ /**
61
+ * Called when a client connects to the server (websocket 'close' event)
62
+ *
63
+ * @param {ServerClient} client
64
+ *
65
+ * @private
66
+ */
67
+ private [kServerContextManagerRemoveClient];
68
+ [kServerContextManagerContexts]: ContextCollection;
69
+ #private;
70
+ }
71
+ /** @private */
72
+ declare class ContextCollection {
73
+ get length(): number;
74
+ add(context: any): void;
75
+ has(name: any): boolean;
76
+ get(name: any): any;
77
+ map(func: any): any[];
78
+ filter(func: any): any[];
79
+ #private;
80
+ }