@soundworks/core 3.7.0 → 4.0.0-alpha.1
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/.eslintrc +16 -0
- package/LICENSE +1 -1
- package/README.md +32 -30
- package/package.json +48 -28
- package/src/client/Client.js +391 -0
- package/src/client/Context.js +225 -0
- package/src/client/ContextManager.js +111 -0
- package/src/client/Plugin.js +66 -0
- package/src/client/PluginManager.js +123 -0
- package/src/client/SharedState.js +84 -0
- package/src/client/Socket.js +389 -0
- package/src/client/StateManager.js +67 -0
- package/src/client/index.js +77 -0
- package/src/common/BasePlugin.js +163 -0
- package/src/common/BasePluginManager.js +227 -0
- package/{common/SharedState.js → src/common/BaseSharedState.js} +198 -234
- package/src/common/BaseStateManager.js +200 -0
- package/{common → src/common}/ParameterBag.js +74 -112
- package/{common → src/common}/SharedStatePrivate.js +56 -62
- package/src/common/constants.js +51 -0
- package/src/common/logger.js +154 -0
- package/src/common/promise-store.js +25 -0
- package/src/common/sockets-utils.js +73 -0
- package/src/common/utils.js +22 -0
- package/src/server/Client.js +50 -0
- package/src/server/Context.js +99 -0
- package/src/server/ContextManager.js +266 -0
- package/src/server/Plugin.js +77 -0
- package/src/server/PluginManager.js +150 -0
- package/src/server/Server.js +849 -0
- package/src/server/SharedState.js +86 -0
- package/{server → src/server}/Socket.js +114 -88
- package/{server → src/server}/Sockets.js +73 -60
- package/{common/SharedStateManagerServer.js → src/server/StateManager.js} +181 -129
- package/src/server/audit-network-latency.worker.js +57 -0
- package/src/server/audit-schema.js +42 -0
- package/src/server/index.js +45 -0
- package/tsconfig.json +21 -0
- package/types/client/Client.d.ts +194 -0
- package/types/client/Client.d.ts.map +1 -0
- package/types/client/Context.d.ts +139 -0
- package/types/client/Context.d.ts.map +1 -0
- package/types/client/ContextManager.d.ts +45 -0
- package/types/client/ContextManager.d.ts.map +1 -0
- package/types/client/Plugin.d.ts +53 -0
- package/types/client/Plugin.d.ts.map +1 -0
- package/types/client/PluginManager.d.ts +96 -0
- package/types/client/PluginManager.d.ts.map +1 -0
- package/types/client/SharedState.d.ts +85 -0
- package/types/client/SharedState.d.ts.map +1 -0
- package/types/client/Socket.d.ts +138 -0
- package/types/client/Socket.d.ts.map +1 -0
- package/types/client/StateManager.d.ts +72 -0
- package/types/client/StateManager.d.ts.map +1 -0
- package/types/client/index.d.ts +11 -0
- package/types/client/index.d.ts.map +1 -0
- package/types/common/BasePlugin.d.ts +145 -0
- package/types/common/BasePlugin.d.ts.map +1 -0
- package/types/common/BasePluginManager.d.ts +88 -0
- package/types/common/BasePluginManager.d.ts.map +1 -0
- package/types/common/BaseSharedState.d.ts +173 -0
- package/types/common/BaseSharedState.d.ts.map +1 -0
- package/types/common/BaseStateManager.d.ts +70 -0
- package/types/common/BaseStateManager.d.ts.map +1 -0
- package/types/common/ParameterBag.d.ts +143 -0
- package/types/common/ParameterBag.d.ts.map +1 -0
- package/types/common/SharedStatePrivate.d.ts +20 -0
- package/types/common/SharedStatePrivate.d.ts.map +1 -0
- package/types/common/constants.d.ts +35 -0
- package/types/common/constants.d.ts.map +1 -0
- package/types/common/logger.d.ts +16 -0
- package/types/common/logger.d.ts.map +1 -0
- package/types/common/promise-store.d.ts +4 -0
- package/types/common/promise-store.d.ts.map +1 -0
- package/types/common/sockets-utils.d.ts +9 -0
- package/types/common/sockets-utils.d.ts.map +1 -0
- package/types/common/utils.d.ts +5 -0
- package/types/common/utils.d.ts.map +1 -0
- package/types/server/Client.d.ts +40 -0
- package/types/server/Client.d.ts.map +1 -0
- package/types/server/Context.d.ts +59 -0
- package/types/server/Context.d.ts.map +1 -0
- package/types/server/ContextManager.d.ts +69 -0
- package/types/server/ContextManager.d.ts.map +1 -0
- package/types/server/Plugin.d.ts +70 -0
- package/types/server/Plugin.d.ts.map +1 -0
- package/types/server/PluginManager.d.ts +91 -0
- package/types/server/PluginManager.d.ts.map +1 -0
- package/types/server/Server.d.ts +223 -0
- package/types/server/Server.d.ts.map +1 -0
- package/types/server/SharedState.d.ts +86 -0
- package/types/server/SharedState.d.ts.map +1 -0
- package/types/server/Socket.d.ts +128 -0
- package/types/server/Socket.d.ts.map +1 -0
- package/types/server/Sockets.d.ts +73 -0
- package/types/server/Sockets.d.ts.map +1 -0
- package/types/server/StateManager.d.ts +190 -0
- package/types/server/StateManager.d.ts.map +1 -0
- package/types/server/audit-network-latency.worker.d.ts +2 -0
- package/types/server/audit-network-latency.worker.d.ts.map +1 -0
- package/types/server/audit-schema.d.ts +27 -0
- package/types/server/audit-schema.d.ts.map +1 -0
- package/types/server/index.d.ts +7 -0
- package/types/server/index.d.ts.map +1 -0
- package/client/AbstractExperience.js +0 -54
- package/client/AbstractPlugin.js +0 -230
- package/client/Client.js +0 -193
- package/client/PluginManager.js +0 -279
- package/client/Socket.js +0 -346
- package/client/index.js +0 -48
- package/client.js +0 -28
- package/common/SharedStateManagerClient.js +0 -194
- package/common/Signal.js +0 -40
- package/common/SignalAll.js +0 -44
- package/common/logger.js +0 -162
- package/common/shared-state-utils.js +0 -89
- package/common/sockets-encoder-decoder.js +0 -73
- package/server/AbstractExperience.js +0 -126
- package/server/AbstractPlugin.js +0 -214
- package/server/Client.js +0 -70
- package/server/PluginManager.js +0 -273
- package/server/Server.js +0 -785
- package/server/index.js +0 -49
- package/server.js +0 -28
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
export default Socket;
|
|
2
|
+
/**
|
|
3
|
+
* The Socket class is a simple publish / subscribe wrapper built on top of the
|
|
4
|
+
* [isomorphic-ws](https://github.com/heineiuo/isomorphic-ws) library.
|
|
5
|
+
* An instance of `Socket` is automatically created by the `soundworks.Client`.
|
|
6
|
+
*
|
|
7
|
+
* _Important: In most cases, you should consider using a {@link client.SharedState} rather than
|
|
8
|
+
* sending messages directly through the sockets._
|
|
9
|
+
*
|
|
10
|
+
* The Socket class concurrently opens two different WebSockets:
|
|
11
|
+
* - a socket configured with `binaryType = 'string'` for JSON compatible string
|
|
12
|
+
* messages.
|
|
13
|
+
* - a socket configured with `binaryType=arraybuffer` for efficient streaming
|
|
14
|
+
* of binary data.
|
|
15
|
+
*
|
|
16
|
+
* Both sockets re-emits all "native" ws events ('open', 'upgrade', 'close', 'error'
|
|
17
|
+
* and 'message'.
|
|
18
|
+
*
|
|
19
|
+
* See {@link client.Client#socket}
|
|
20
|
+
*
|
|
21
|
+
* @memberof client
|
|
22
|
+
* @hideconstructor
|
|
23
|
+
*/
|
|
24
|
+
declare class Socket {
|
|
25
|
+
/**
|
|
26
|
+
* WebSocket instance w/ string protocol, i.e. `binaryType = 'string'`.
|
|
27
|
+
*
|
|
28
|
+
* @private
|
|
29
|
+
*/
|
|
30
|
+
private ws;
|
|
31
|
+
/**
|
|
32
|
+
* WebSocket instance w/ binary protocol, i.e. `binaryType = 'arraybuffer'`.
|
|
33
|
+
*
|
|
34
|
+
* @private
|
|
35
|
+
*/
|
|
36
|
+
private binaryWs;
|
|
37
|
+
/** @private */
|
|
38
|
+
private _stringListeners;
|
|
39
|
+
/** @private */
|
|
40
|
+
private _binaryListeners;
|
|
41
|
+
/**
|
|
42
|
+
* Initialize a websocket connection with the server. Automatically called
|
|
43
|
+
* during `client.init()`
|
|
44
|
+
*
|
|
45
|
+
* @param {string} role - Role of the client (see {@link client.Client#role})
|
|
46
|
+
* @param {object} config - Configuration of the sockets
|
|
47
|
+
* @protected
|
|
48
|
+
* @ignore
|
|
49
|
+
*/
|
|
50
|
+
protected init(role: string, config: object): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* @param {boolean} binary - Emit to either the string or binary socket.
|
|
53
|
+
* @param {string} channel - Channel name.
|
|
54
|
+
* @param {...*} args - Content of the message.
|
|
55
|
+
* @private
|
|
56
|
+
*/
|
|
57
|
+
private _emit;
|
|
58
|
+
/**
|
|
59
|
+
* @param {Function[]} listeners - List of listeners, either for the string or binary socket.
|
|
60
|
+
* @param {string} channel - Channel name.
|
|
61
|
+
* @param {Function} callback - The function to be added to the listeners.
|
|
62
|
+
* @private
|
|
63
|
+
*/
|
|
64
|
+
private _addListener;
|
|
65
|
+
/**
|
|
66
|
+
* @param {Function[]} listeners - List of listeners, either for the string or binary socket.
|
|
67
|
+
* @param {string} channel - Channel name.
|
|
68
|
+
* @param {Function} callback - The function to be removed from the listeners.
|
|
69
|
+
* @private
|
|
70
|
+
*/
|
|
71
|
+
private _removeListener;
|
|
72
|
+
/**
|
|
73
|
+
* @param {Function[]} listeners - List of listeners, either for the string or binary socket.
|
|
74
|
+
* @param {string} [channel=null] - Channel name of the listeners to remove. If null
|
|
75
|
+
* all the listeners are cleared.
|
|
76
|
+
* @private
|
|
77
|
+
*/
|
|
78
|
+
private _removeAllListeners;
|
|
79
|
+
/**
|
|
80
|
+
* Send JSON compatible messages on a given channel.
|
|
81
|
+
*
|
|
82
|
+
* @param {string} channel - Channel name of the message.
|
|
83
|
+
* @param {...*} args - Message list, as many as needed, of any serializable type).
|
|
84
|
+
*/
|
|
85
|
+
send(channel: string, ...args: any[]): void;
|
|
86
|
+
/**
|
|
87
|
+
* Listen to JSON compatible messages on a given channel.
|
|
88
|
+
*
|
|
89
|
+
* @param {string} channel - Channel name of the message.
|
|
90
|
+
* @param {Function} callback - Callback to execute when a message is received.
|
|
91
|
+
*/
|
|
92
|
+
addListener(channel: string, callback: Function): void;
|
|
93
|
+
/**
|
|
94
|
+
* Remove a listener from JSON compatible messages on a given channel.
|
|
95
|
+
*
|
|
96
|
+
* @param {string} channel - Channel name of the message.
|
|
97
|
+
* @param {Function} callback - Callback to remove.
|
|
98
|
+
*/
|
|
99
|
+
removeListener(channel: string, callback: Function): void;
|
|
100
|
+
/**
|
|
101
|
+
* Remove all listeners from JSON compatible messages on a given channel.
|
|
102
|
+
*
|
|
103
|
+
* @param {string} channel - Channel name of the message.
|
|
104
|
+
*/
|
|
105
|
+
removeAllListeners(channel?: string): void;
|
|
106
|
+
/**
|
|
107
|
+
* Send binary messages on a given channel.
|
|
108
|
+
*
|
|
109
|
+
* @param {string} channel - Channel name of the message.
|
|
110
|
+
* @param {TypedArray} typedArray - Binary data to be sent.
|
|
111
|
+
*/
|
|
112
|
+
sendBinary(channel: string, typedArray: TypedArray): void;
|
|
113
|
+
/**
|
|
114
|
+
* Listen binary messages on a given channel.
|
|
115
|
+
*
|
|
116
|
+
* @param {string} channel - Channel name of the message.
|
|
117
|
+
* @param {Function} callback - Callback to execute when a message is received.
|
|
118
|
+
*/
|
|
119
|
+
addBinaryListener(channel: string, callback: Function): void;
|
|
120
|
+
/**
|
|
121
|
+
* Remove a listener from binary compatible messages on a given channel
|
|
122
|
+
*
|
|
123
|
+
* @param {string} channel - Channel of the message.
|
|
124
|
+
* @param {Function} callback - Callback to cancel.
|
|
125
|
+
*/
|
|
126
|
+
removeBinaryListener(channel: string, callback: Function): void;
|
|
127
|
+
/**
|
|
128
|
+
* Remove all listeners from binary compatible messages on a given channel
|
|
129
|
+
*
|
|
130
|
+
* @param {string} channel - Channel of the message.
|
|
131
|
+
*/
|
|
132
|
+
removeAllBinaryListeners(channel?: string): void;
|
|
133
|
+
/**
|
|
134
|
+
* Removes all listeners and immediately close the two sockets.
|
|
135
|
+
*/
|
|
136
|
+
terminate(): Promise<void>;
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=Socket.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Socket.d.ts","sourceRoot":"","sources":["../../src/client/Socket.js"],"names":[],"mappings":";AA2BA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH;IAEI;;;;OAIG;IACH,WAAc;IACd;;;;OAIG;IACH,iBAAoB;IACpB,eAAe;IACf,yBAAiC;IACjC,eAAe;IACf,yBAAiC;IAGnC;;;;;;;;OAQG;IACH,qBALW,MAAM,UACN,MAAM,iBA6JhB;IAED;;;;;OAKG;IACH,cAOC;IAED;;;;;OAKG;IACH,qBAOC;IAED;;;;;OAKG;IACH,wBASC;IAED;;;;;OAKG;IACH,4BAMC;IAED;;;;;OAKG;IACH,cAHW,MAAM,wBAMhB;IAED;;;;;OAKG;IACH,qBAHW,MAAM,4BAKhB;IAED;;;;;OAKG;IACH,wBAHW,MAAM,4BAKhB;IAED;;;;OAIG;IACH,6BAFW,MAAM,QAIhB;IAED;;;;;OAKG;IACH,oBAHW,MAAM,gCAMhB;IAED;;;;;OAKG;IACH,2BAHW,MAAM,4BAKhB;IAED;;;;;OAKG;IACH,8BAHW,MAAM,4BAKhB;IAED;;;;OAIG;IACH,mCAFW,MAAM,QAIhB;IAED;;OAEG;IACH,2BAQC;CACF"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export default StateManager;
|
|
2
|
+
export namespace client {
|
|
3
|
+
/**
|
|
4
|
+
* ~ObserveCallback
|
|
5
|
+
*/
|
|
6
|
+
type StateManager = () => any;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* @callback client.StateManager~ObserveCallback
|
|
10
|
+
* @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
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
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.
|
|
19
|
+
*
|
|
20
|
+
* An instance of `StateManager` is automatically created by the `soundworks.Client`
|
|
21
|
+
* at initialization (cf. {@link client.Client#stateManager}).
|
|
22
|
+
*
|
|
23
|
+
* See {@link client.Client#stateManager}
|
|
24
|
+
*
|
|
25
|
+
* Tutorial: {@link https://soundworks.dev/guide/state-manager.html}
|
|
26
|
+
*
|
|
27
|
+
* ```
|
|
28
|
+
* // server-side
|
|
29
|
+
* import { Server } from '@soundworks/server/index.js';
|
|
30
|
+
*
|
|
31
|
+
* const server = new Server(config);
|
|
32
|
+
* // declare and register the schema of a shared state.
|
|
33
|
+
* server.stateManager.registerSchema('some-global-state', {
|
|
34
|
+
* myRandom: {
|
|
35
|
+
* type: 'float',
|
|
36
|
+
* default: 0,
|
|
37
|
+
* }
|
|
38
|
+
* });
|
|
39
|
+
*
|
|
40
|
+
* await server.start();
|
|
41
|
+
*
|
|
42
|
+
* // create a global state server-side
|
|
43
|
+
* const globalState = await server.stateManager.create('some-global-state');
|
|
44
|
+
* // listen and react to the changes made by the clients
|
|
45
|
+
* globalState.onUpdate(updates => console.log(updates));
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* ```
|
|
49
|
+
* // client-side
|
|
50
|
+
* import { Client } from '@soundworks/client.index.js';
|
|
51
|
+
*
|
|
52
|
+
* const client = new Client(config);
|
|
53
|
+
* await client.start();
|
|
54
|
+
*
|
|
55
|
+
* // attach to the global state created by the server
|
|
56
|
+
* const globalState = await client.stateManager.attach('some-global-state');
|
|
57
|
+
*
|
|
58
|
+
* // update the value of a `myRandom` parameter every seconds
|
|
59
|
+
* setInterval(() => {
|
|
60
|
+
* globalState.set({ myRandom: Math.random() });
|
|
61
|
+
* }, 1000);
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* @memberof client
|
|
65
|
+
* @extends BaseStateManager
|
|
66
|
+
* @inheritdoc
|
|
67
|
+
* @hideconstructor
|
|
68
|
+
*/
|
|
69
|
+
declare class StateManager extends BaseStateManager {
|
|
70
|
+
}
|
|
71
|
+
import BaseStateManager from "../common/BaseStateManager.js";
|
|
72
|
+
//# sourceMappingURL=StateManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"StateManager.d.ts","sourceRoot":"","sources":["../../src/client/StateManager.js"],"names":[],"mappings":";;;;;;;AAEA;;;;;;GAMG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AACH;CAA8C"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { default as Context } from "./Context.js";
|
|
2
|
+
export { default as Client } from "./Client.js";
|
|
3
|
+
/**
|
|
4
|
+
* Configuration object for a client running in a browser runtime.
|
|
5
|
+
*/
|
|
6
|
+
export type BrowserClientConfig = any;
|
|
7
|
+
/**
|
|
8
|
+
* Configuration object for a client running in a node runtime.
|
|
9
|
+
*/
|
|
10
|
+
export type NodeClientConfig = any;
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.js"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
export default BasePlugin;
|
|
2
|
+
/**
|
|
3
|
+
* @private
|
|
4
|
+
*/
|
|
5
|
+
declare class BasePlugin {
|
|
6
|
+
constructor(id: any);
|
|
7
|
+
/**
|
|
8
|
+
* User defined ID of the plugin.
|
|
9
|
+
*
|
|
10
|
+
* @type {string}
|
|
11
|
+
* @see {@link client.PluginManager#register}
|
|
12
|
+
* @see {@link server.PluginManager#register}
|
|
13
|
+
*/
|
|
14
|
+
id: string;
|
|
15
|
+
/**
|
|
16
|
+
* Type of the plugin, i.e. the ClassName.
|
|
17
|
+
*
|
|
18
|
+
* Usefull to do perform some logic based on certain types of plugins without
|
|
19
|
+
* knowing under which `id` they have been registered. (e.g. creating some generic
|
|
20
|
+
* views, etc.)
|
|
21
|
+
*
|
|
22
|
+
* @type {string}
|
|
23
|
+
* @readonly
|
|
24
|
+
*/
|
|
25
|
+
readonly type: string;
|
|
26
|
+
/**
|
|
27
|
+
* Options of the plugin.
|
|
28
|
+
*
|
|
29
|
+
* @type {object}
|
|
30
|
+
*/
|
|
31
|
+
options: object;
|
|
32
|
+
/**
|
|
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'
|
|
47
|
+
*
|
|
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.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* // server-side couterpart of a plugin that creates a dedicated global shared
|
|
60
|
+
* // state on which the server-side part can attach.
|
|
61
|
+
* class MyPlugin extends Plugin {
|
|
62
|
+
* constructor(server, id) {
|
|
63
|
+
* super(server, id);
|
|
64
|
+
*
|
|
65
|
+
* this.server.stateManager.registerSchema(`my-plugin:${this.id}`, {
|
|
66
|
+
* someParam: {
|
|
67
|
+
* type: 'boolean',
|
|
68
|
+
* default: false,
|
|
69
|
+
* },
|
|
70
|
+
* // ...
|
|
71
|
+
* });
|
|
72
|
+
* }
|
|
73
|
+
*
|
|
74
|
+
* async start() {
|
|
75
|
+
* await super.start()
|
|
76
|
+
* this.sharedState = await this.server.stateManager.create(`my-plugin:${this.id}`);
|
|
77
|
+
* }
|
|
78
|
+
*
|
|
79
|
+
* async stop() {
|
|
80
|
+
* await this.sharedState.delete();
|
|
81
|
+
* }
|
|
82
|
+
* }
|
|
83
|
+
*/
|
|
84
|
+
start(): Promise<void>;
|
|
85
|
+
/**
|
|
86
|
+
* Stop the plugin. This method is automatically called during the client or server
|
|
87
|
+
* `stop()` lifecyle step.
|
|
88
|
+
*
|
|
89
|
+
* @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 {
|
|
93
|
+
* constructor(server, id) {
|
|
94
|
+
* super(server, id);
|
|
95
|
+
*
|
|
96
|
+
* this.server.stateManager.registerSchema(`my-plugin:${this.id}`, {
|
|
97
|
+
* someParam: {
|
|
98
|
+
* type: 'boolean',
|
|
99
|
+
* default: false,
|
|
100
|
+
* },
|
|
101
|
+
* // ...
|
|
102
|
+
* });
|
|
103
|
+
* }
|
|
104
|
+
*
|
|
105
|
+
* async start() {
|
|
106
|
+
* await super.start()
|
|
107
|
+
* this.sharedState = await this.server.stateManager.create(`my-plugin:${this.id}`);
|
|
108
|
+
* this.sharedState.onUpdate(updates => this.doSomething(updates));
|
|
109
|
+
* }
|
|
110
|
+
*
|
|
111
|
+
* async stop() {
|
|
112
|
+
* await this.sharedState.delete();
|
|
113
|
+
* }
|
|
114
|
+
* }
|
|
115
|
+
*/
|
|
116
|
+
stop(): Promise<void>;
|
|
117
|
+
/**
|
|
118
|
+
* Listen to the state changes propagated by {@link BasePlugin.propagateStateChange}
|
|
119
|
+
*
|
|
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}
|
|
126
|
+
* @example
|
|
127
|
+
* const unsubscribe = plugin.onStateChange(pluginState => console.log(pluginState));
|
|
128
|
+
* // stop listening state changes
|
|
129
|
+
* unsubscribe();
|
|
130
|
+
*/
|
|
131
|
+
onStateChange(callback: any): client.Plugin;
|
|
132
|
+
/**
|
|
133
|
+
* Apply updates to the plugin state and propagate the updated state to the
|
|
134
|
+
* `onStateChange` listeners. The state changes will also be propagated
|
|
135
|
+
* through the `PluginManager#onStateChange` listeners.
|
|
136
|
+
*
|
|
137
|
+
* @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}
|
|
142
|
+
*/
|
|
143
|
+
propagateStateChange(updates: object): void;
|
|
144
|
+
}
|
|
145
|
+
//# sourceMappingURL=BasePlugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BasePlugin.d.ts","sourceRoot":"","sources":["../../src/common/BasePlugin.js"],"names":[],"mappings":";AAEA;;GAEG;AACH;IACE,qBAoDC;IAnDC;;;;;;OAMG;IACH,IAJU,MAAM,CAIJ;IAEZ;;;;;;;;;OASG;IACH,eAHU,MAAM,CAGiB;IAEjC;;;;OAIG;IACH,SAFU,MAAM,CAEC;IAEjB;;;;;;;;;;;OAWG;IACH,iBAPU,MAAM,CAOD;IAEf;;;;OAIG;IACH,QAFU,MAAM,CAEI;IAEpB,eAAe;IACf,gCAAwC;IAG1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACH,uBAAgB;IAEhB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACH,sBAAe;IAEf;;;;;;;;;;;;;OAaG;IACH,4CAGC;IAED;;;;;;;;;;OAUG;IACH,8BANW,MAAM,QAShB;CACF"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
export default BasePluginManager;
|
|
2
|
+
/**
|
|
3
|
+
* Shared functionnality between server-side and client-size plugin manager
|
|
4
|
+
*
|
|
5
|
+
* @private
|
|
6
|
+
*/
|
|
7
|
+
declare class BasePluginManager {
|
|
8
|
+
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
|
+
/**
|
|
21
|
+
* Register a plugin into soundworks.
|
|
22
|
+
*
|
|
23
|
+
* _A plugin must always be registered both on client-side and on server-side_
|
|
24
|
+
*
|
|
25
|
+
* Refer to the plugin documentation to check its options and proper way of
|
|
26
|
+
* registering it.
|
|
27
|
+
*
|
|
28
|
+
* @param {string} id - Unique id of the plugin. Enables the registration of the
|
|
29
|
+
* same plugin factory under different ids.
|
|
30
|
+
* @param {Function} factory - Factory function that returns the Plugin class.
|
|
31
|
+
* @param {object} [options={}] - Options to configure the plugin.
|
|
32
|
+
* @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
|
|
34
|
+
* fully started themselves.
|
|
35
|
+
* @see {@link client.PluginManager#register}
|
|
36
|
+
* @see {@link server.PluginManager#register}
|
|
37
|
+
* @example
|
|
38
|
+
* // client-side
|
|
39
|
+
* client.pluginManager.register('user-defined-id', pluginFactory);
|
|
40
|
+
* // server-side
|
|
41
|
+
* server.pluginManager.register('user-defined-id', pluginFactory);
|
|
42
|
+
*/
|
|
43
|
+
register(id: string, ctor: any, options?: object, deps?: any[]): void;
|
|
44
|
+
/**
|
|
45
|
+
* Initialize all the registered plugin. Executed during the `Client.init()` or
|
|
46
|
+
* `Server.init()` initialization step.
|
|
47
|
+
* @private
|
|
48
|
+
*/
|
|
49
|
+
private start;
|
|
50
|
+
stop(): Promise<void>;
|
|
51
|
+
/**
|
|
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
|
|
57
|
+
*/
|
|
58
|
+
private getUnsafe;
|
|
59
|
+
/**
|
|
60
|
+
* Propagate a notification each time a plugin is updated (status or inner state).
|
|
61
|
+
* The callback will receive the list of all plugins as first parameter, and the
|
|
62
|
+
* plugin instance that initiated the state change event as second parameter.
|
|
63
|
+
*
|
|
64
|
+
* _In most cases, you should not have to rely on this method._
|
|
65
|
+
*
|
|
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.
|
|
70
|
+
* @example
|
|
71
|
+
* const unsubscribe = client.pluginManager.onStateChange(pluginList, initiator => {
|
|
72
|
+
* // log the current status of all plugins
|
|
73
|
+
* for (let name in pluginList) {
|
|
74
|
+
* console.log(name, pluginList[name].status);
|
|
75
|
+
* }
|
|
76
|
+
* // if the change was initiated by a plugin, log its status and state
|
|
77
|
+
* if (initiator !== null) {
|
|
78
|
+
*. console.log(initiator.name, initiator.status, initiator.state);
|
|
79
|
+
* }
|
|
80
|
+
* });
|
|
81
|
+
* // stop listening for updates later
|
|
82
|
+
* unsubscribe();
|
|
83
|
+
*/
|
|
84
|
+
onStateChange(callback: any): () => boolean;
|
|
85
|
+
/** @private */
|
|
86
|
+
private _propagateStateChange;
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=BasePluginManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BasePluginManager.d.ts","sourceRoot":"","sources":["../../src/common/BasePluginManager.js"],"names":[],"mappings":";AAKA;;;;GAIG;AACH;IACE,uBAcC;IAZC,eAAe;IACf,cAAiB;IACjB,eAAe;IACf,2BAAmC;IACnC,eAAe;IACf,mBAA2B;IAC3B,eAAe;IACf,+BAAuC;IACvC,eAAe;IACf,gCAAwC;IAExC,eAAoB;IAGtB;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,aAfW,MAAM,uBAGN,MAAM,sBAqChB;IAED;;;;OAIG;IACH,cA2BC;IAED,sBAIC;IAED;;;;;;OAMG;IACH,kBAqDC;IAED;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACH,4CAGC;IAED,eAAe;IACf,8BAaC;CACF"}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
export default BaseSharedState;
|
|
2
|
+
declare class BaseSharedState {
|
|
3
|
+
constructor(id: any, remoteId: any, schemaName: any, schema: any, client: any, isOwner: any, manager: any, initValues?: {});
|
|
4
|
+
id: any;
|
|
5
|
+
remoteId: any;
|
|
6
|
+
schemaName: any;
|
|
7
|
+
/** @private */
|
|
8
|
+
private _isOwner;
|
|
9
|
+
/** @private */
|
|
10
|
+
private _client;
|
|
11
|
+
/** @private */
|
|
12
|
+
private _manager;
|
|
13
|
+
/** @private */
|
|
14
|
+
private _parameters;
|
|
15
|
+
/** @private */
|
|
16
|
+
private _onUpdateCallbacks;
|
|
17
|
+
/** @private */
|
|
18
|
+
private _onDetachCallbacks;
|
|
19
|
+
/** @private */
|
|
20
|
+
private _onDeleteCallbacks;
|
|
21
|
+
/** @private */
|
|
22
|
+
private _clearDetach;
|
|
23
|
+
/**
|
|
24
|
+
* Detach from the state. If the client is the creator of the state, the state
|
|
25
|
+
* is deleted and all attached nodes get notified
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* const state = await client.state.attach('globals');
|
|
29
|
+
* // later
|
|
30
|
+
* await state.detach();
|
|
31
|
+
*/
|
|
32
|
+
detach(): Promise<any>;
|
|
33
|
+
/** @private */
|
|
34
|
+
private _clearTransport;
|
|
35
|
+
/** @private */
|
|
36
|
+
private _commit;
|
|
37
|
+
/**
|
|
38
|
+
* Update values of the state.
|
|
39
|
+
*
|
|
40
|
+
* The returned `Promise` resolves on the applied updates, when all the `onUpdate`
|
|
41
|
+
* callbacks have resolved themselves, i.e.:
|
|
42
|
+
*
|
|
43
|
+
* ```js
|
|
44
|
+
* server.stateManager.registerSchema('test', {
|
|
45
|
+
* myBool: { type: 'boolean', default: false },
|
|
46
|
+
* });
|
|
47
|
+
* const a = await server.stateManager.create('a');
|
|
48
|
+
* let asyncCallbackCalled = false;
|
|
49
|
+
*
|
|
50
|
+
* a.onUpdate(updates => {
|
|
51
|
+
* return new Promise(resolve => {
|
|
52
|
+
* setTimeout(() => {
|
|
53
|
+
* asyncCallbackCalled = true;
|
|
54
|
+
* resolve();
|
|
55
|
+
* }, 100);
|
|
56
|
+
* });
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* const updates = await a.set({ myBool: true });
|
|
60
|
+
* assert.equal(asyncCallbackCalled, true);
|
|
61
|
+
* assert.deepEqual(updates, { myBool: true });
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* @param {object} updates - key / value pairs of updates to apply to the state.
|
|
65
|
+
* @param {mixed} [context=null] - optionnal contextual object that will be propagated
|
|
66
|
+
* alongside the updates of the state. The context is valid only for the
|
|
67
|
+
* current call and will be passed as third argument to all update listeners.
|
|
68
|
+
* @returns {Promise<Object>} A promise to the (coerced) updates.
|
|
69
|
+
* @example
|
|
70
|
+
* const state = await client.state.attach('globals');
|
|
71
|
+
* const updates = await state.set({ myParam: Math.random() });
|
|
72
|
+
*/
|
|
73
|
+
set(updates: object, context?: mixed): Promise<any>;
|
|
74
|
+
/**
|
|
75
|
+
* Get the value of a paramter of the state.
|
|
76
|
+
*
|
|
77
|
+
* @param {string} name - Name of the param.
|
|
78
|
+
* @throws Throws if `name` does not correspond to an existing field
|
|
79
|
+
* of the state.
|
|
80
|
+
* @return {mixed}
|
|
81
|
+
* @example
|
|
82
|
+
* const value = state.get('name');
|
|
83
|
+
*/
|
|
84
|
+
get(name: string): mixed;
|
|
85
|
+
/**
|
|
86
|
+
* Get all the key / value pairs of the state.
|
|
87
|
+
*
|
|
88
|
+
* @return {object}
|
|
89
|
+
* @example
|
|
90
|
+
* const values = state.getValues();
|
|
91
|
+
*/
|
|
92
|
+
getValues(): object;
|
|
93
|
+
/**
|
|
94
|
+
* Get the schema from which the state has been created.
|
|
95
|
+
*
|
|
96
|
+
* @param {string} [name=null] - If given, returns only the definition corresponding
|
|
97
|
+
* to the given param name.
|
|
98
|
+
* @throws Throws if `name` does not correspond to an existing field
|
|
99
|
+
* of the state.
|
|
100
|
+
* @return {object}
|
|
101
|
+
* @example
|
|
102
|
+
* const schema = state.getSchema();
|
|
103
|
+
*/
|
|
104
|
+
getSchema(name?: string): object;
|
|
105
|
+
/**
|
|
106
|
+
* Get the values with which the state has been created. May defer from the
|
|
107
|
+
* default values declared in the schema.
|
|
108
|
+
*
|
|
109
|
+
* @return {object}
|
|
110
|
+
* @example
|
|
111
|
+
* const initValues = state.getInitValues();
|
|
112
|
+
*/
|
|
113
|
+
getInitValues(): object;
|
|
114
|
+
/**
|
|
115
|
+
* Get the default values as declared in the schema.
|
|
116
|
+
*
|
|
117
|
+
* @return {object}
|
|
118
|
+
* @example
|
|
119
|
+
* const defaults = state.getDefaults();
|
|
120
|
+
*/
|
|
121
|
+
getDefaults(): object;
|
|
122
|
+
/**
|
|
123
|
+
* Delete the state. Only the creator/owner of the state can use this method.
|
|
124
|
+
* All nodes attached to the state will be deteched, triggering the `onDetach`
|
|
125
|
+
* callback. The creator of the state will also have its `onDelete` callback
|
|
126
|
+
* triggered.
|
|
127
|
+
*
|
|
128
|
+
* @throws Throws if the method is called by a node which is not the owner of
|
|
129
|
+
* the state.
|
|
130
|
+
* @example
|
|
131
|
+
* const state = await client.state.create('my-schema-name');
|
|
132
|
+
* // later
|
|
133
|
+
* await state.delete();
|
|
134
|
+
*/
|
|
135
|
+
delete(): Promise<any>;
|
|
136
|
+
/**
|
|
137
|
+
* Subscribe to state updates.
|
|
138
|
+
*
|
|
139
|
+
* @param {client.SharedState~onUpdateCallback|client.SharedState~onUpdateCallback} callback
|
|
140
|
+
* Callback to execute when an update is applied on the state.
|
|
141
|
+
* @param {Boolean} [executeListener=false] - Execute the callback immediately
|
|
142
|
+
* with current state values. (`oldValues` will be set to `{}`, and `context` to `null`)
|
|
143
|
+
* @returns {client.SharedState~deleteOnUpdateCallback|server.SharedState~deleteOnUpdateCallback}
|
|
144
|
+
* @example
|
|
145
|
+
* const unsubscribe = state.onUpdate(async (newValues, oldValues, context) => {
|
|
146
|
+
* for (let [key, value] of Object.entries(newValues)) {
|
|
147
|
+
* switch (key) {
|
|
148
|
+
* // do something
|
|
149
|
+
* }
|
|
150
|
+
* }
|
|
151
|
+
* });
|
|
152
|
+
*
|
|
153
|
+
* // later
|
|
154
|
+
* unsubscribe();
|
|
155
|
+
*/
|
|
156
|
+
onUpdate(listener: any, executeListener?: boolean): client.SharedState;
|
|
157
|
+
/**
|
|
158
|
+
* Register a function to execute when detaching from the state
|
|
159
|
+
*
|
|
160
|
+
* @param {Function} callback - callback to execute when detaching from the state.
|
|
161
|
+
* wether the client as called `detach`, or the state has been deleted by its
|
|
162
|
+
* creator.
|
|
163
|
+
*/
|
|
164
|
+
onDetach(callback: Function): () => boolean;
|
|
165
|
+
/**
|
|
166
|
+
* Register a function to execute when the state is deleted. Only called if the
|
|
167
|
+
* node was the creator of the state. Is called after `onDetach`
|
|
168
|
+
*
|
|
169
|
+
* @param {Function} callback - callback to execute when the state is deleted.
|
|
170
|
+
*/
|
|
171
|
+
onDelete(callback: Function): () => boolean;
|
|
172
|
+
}
|
|
173
|
+
//# sourceMappingURL=BaseSharedState.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseSharedState.d.ts","sourceRoot":"","sources":["../../src/common/BaseSharedState.js"],"names":[],"mappings":";AAoBA;IACE,4HAwHC;IAvHC,QAAY;IACZ,cAAwB;IACxB,gBAA4B;IAE5B,eAAe;IACf,iBAAuB;IACvB,eAAe;IACf,gBAAqB;IACrB,eAAe;IACf,iBAAuB;IAGrB,eAAe;IACf,oBAAuD;IAQzD,eAAe;IACf,2BAAmC;IACnC,eAAe;IACf,2BAAmC;IACnC,eAAe;IACf,2BAAmC;IA+FrC,eAAe;IACf,qBASC;IAsND;;;;;;;;OAQG;IACH,uBAcC;IA3OD,eAAe;IACf,wBAOC;IAED,eAAe;IACf,gBAoDC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAmCG;IACH,aATW,MAAM,oBAIJ,YAAe,CAmD3B;IAED;;;;;;;;;OASG;IACH,UAPW,MAAM,SAShB;IAED;;;;;;OAMG;IACH,aAJY,MAAM,CAMjB;IAED;;;;;;;;;;OAUG;IACH,iBARW,MAAM,GAIL,MAAM,CAMjB;IAED;;;;;;;OAOG;IACH,iBAJY,MAAM,CAMjB;IAED;;;;;;OAMG;IACH,eAJY,MAAM,CAMjB;IA2BD;;;;;;;;;;;;OAYG;IACH,uBAMC;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,uEAaC;IAED;;;;;;OAMG;IACH,4CAGC;IAED;;;;;OAKG;IACH,4CAGC;CACF"}
|