Constructor
new ServerContext(server, rolesopt)
- Source:
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
server |
Server | The soundworks server instance. |
||
roles |
string | Array.<string> |
<optional> |
[]
|
Optionnal list of client roles that can use this context. In large applications, this may be usefull to guarantee that a context can be consumed only by specific client roles, throwing an error if any other client role tries to use it. If empty, no access policy will be used. |
Throws:
Will throw if the first argument is not a soundworks server instance.
Members
clients :Set.<ServerClient>
- Description:
List of clients that are currently in this context.
- Source:
List of clients that are currently in this context.
Type:
- Set.<ServerClient>
name :string
- Description:
Optionnal user-defined name of the context (defaults to the class name).
The context manager will match the client-side and server-side contexts based on this name. If the ServerContextManager don't find a corresponding user-defined context with the same name, it will use a default (noop) context.
- Source:
Optionnal user-defined name of the context (defaults to the class name).
The context manager will match the client-side and server-side contexts based on this name. If the ServerContextManager don't find a corresponding user-defined context with the same name, it will use a default (noop) context.
Type:
- string
Example
// server-side and client-side contexts are matched based on their respective `name`
class MyContext extends Context {
get name() {
return 'my-user-defined-context-name';
}
}
roles :Set.<string>
- Description:
List of client roles that can use this context. No access policy if empty.
- Source:
List of client roles that can use this context. No access policy if empty.
Type:
- Set.<string>
server :Server
- Description:
The soundworks server instance.
- Source:
The soundworks server instance.
Type:
status :string
- Description:
Status of the context ('idle', 'inited', 'started' or 'errored')
- Source:
Status of the context ('idle', 'inited', 'started' or 'errored')
Type:
- string
Methods
(async) enter(client) → {Promise}
- Description:
Enter the context. Implement this method to define the logic that should be done (e.g. creating a shared state, etc.) when a client enters the context.
If the context has not been started yet, the
start
method is implicitely executed.WARNING: this method should never be called manually.
- Source:
Example
class MyContext extends Context {
async enter(client) {
await super.enter(client);
registerTheClientSomewhere(client);
}
async exit(client) {
await super.exit(client);
unregisterTheClientSomewhere(client);
}
}
Parameters:
Name | Type | Description |
---|---|---|
client |
ServerClient | Server-side representation of the client that enters the context. |
Returns:
- Promise that resolves when the context is entered.
- Type
- Promise
(async) exit(client) → {Promise}
- Description:
Exit the context. Implement this method to define the logic that should be done (e.g. delete a shared state, etc.) when a client exits the context.
- WARNING: this method should never be called manually.
- Source:
Example
class MyContext extends Context {
async enter(client) {
await super.enter(client);
this.state = await this.client.stateManager.create('my-context-state');
}
async exit(client) {
await super.exit(client);
await this.state.delete();
}
}
Parameters:
Name | Type | Description |
---|---|---|
client |
ServerClient | Server-side representation of the client that exits the context. |
Returns:
- Promise that resolves when the context is exited.
- Type
- Promise
(async) start()
- Description:
Start the context. This method is lazilly called when a client enters the context for the first time (cf. ${ServerContext#enter}). If you know some some heavy and/or potentially long job has to be done when starting the context (e.g. connect to a database, parsing a long file) it may be a good practice to call it explicitely.
This method should be implemented to perform operations that are valid for the whole lifetime of the context, regardless a client enters or exits the context.
- Source:
Example
import { Context } from '@soundworks/core/server.js';
class MyContext extends Context {
async start() {
await super.start();
await this.doSomeLongJob();
}
}
// Instantiate the context
const myContext = new Context(server, ['test']);
// manually start the context to perform the long operation before the first
// client enters the context
await myContext.start();
```
(async) stop()
- Description:
Stop the context. The method that is automatically called when the server stops. It should be used to cleanup context wise operations made in
start()
(e.g. disconnect from a database, release a file handle).WARNING: this method should never be called manually.
- Source: