Constructor
new SharedStateManagerServer()
- Source:
- See:
-
- client.SharedStateManagerClient
- server.SharedState
- server.Server#stateManager
Extends
Methods
(async) attach(schemaName, stateIdopt)
- Source:
- Overrides:
- See:
Attach to an existing common.SharedState instance.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
schemaName |
String | Name of the schema as given on registration (cf. ServerStateManager) |
||
stateId |
Object |
<optional> |
null
|
Id of the state to attach to. If |
Returns:
(async) create(schemaName, initValuesopt)
- Source:
- Overrides:
- See:
Create a common.SharedState instance from a previsouly registered schema.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
schemaName |
String | Name of the schema as given on registration (cf. ServerStateManager) |
||
initValues |
Object |
<optional> |
{}
|
Default values for the state. |
Returns:
deleteSchema(schemaName)
Delete a schema and all associated states.
When a schema is deleted, all states created from this schema are deleted
as well, therefore all attached clients are detached and the onDetach
and onDelete
callbacks are called on the related states.
Parameters:
Name | Type | Description |
---|---|---|
schemaName |
String | Name of the schema. |
observe(callback)
- Source:
- Overrides:
Observe all the common.SharedState instances that are created on the network.
Example
this.client.stateManager.observe(async (schemaName, stateId, nodeId) => {
if (schemaName === 'something') {
const state = await this.client.stateManager.attach(schemaName, stateId);
console.log(state.getValues());
}
});
Parameters:
Name | Type | Description |
---|---|---|
callback |
client.SharedStateManagerClient~observeCallback | Function to be called when a new state is created on the network. |
registerSchema(schemaName, schema)
- Source:
- See:
Register a schema from which shared states (cf. common.SharedState) can be instanciated.
Example
server.stateManager.registerSchema('my-schema', {
myBoolean: {
type: 'boolean'
default: false,
},
myFloat: {
type: 'float'
default: 0.1,
min: -1,
max: 1
}
})
Parameters:
Name | Type | Description |
---|---|---|
schemaName |
String | Name of the schema. |
schema |
server.SharedStateManagerServer~schema | Data structure describing the states that will be created from this schema. |
registerUpdateHook(schemaName, updateHook)
Register a function for a given schema (e.g. will be applied on all states created from this schema) that will be executed before the update values are propagated. For example, this could be used to implement a preset system where all the values of the state are updated from e.g. some data stored in filesystem while the consumer of the state only want to update the preset name.
The hook is associated to every state of its kind (i.e. schemaName) and
executed on every update (call of set
). Note that the hooks are executed
server-side regarless the node on which set
has been called and before
the "actual" update of the state (e.g. before the call of subscribe
).
Example
server.stateManager.registerSchema('hooked', schema);
server.stateManager.registerUpdateHook('hooked', (updates, currentValues) => {
return {
...updates
numUpdates: currentValues.numUpdates + 1,
};
});
const state = await server.stateManager.create('hooked');
await state.set({ name: 'test' });
state.getValues();
// > { name: 'test', numUpdates: 1 };
Parameters:
Name | Type | Description |
---|---|---|
schemaName |
String | Kind of states on which applying the hook. |
updateHook |
server.SharedStateManagerServer~updateHook | Function
called between the |
Type Definitions
schema
- Source:
Description of a schema to be register by the server.ServerStateManagerServer A schema consists of a combinaison of key value pairs where the key is the name of the parameter, and the value is an object describing the parameter.
Available types are:
Type:
- Object
Example
{
myBoolean: {
type: 'boolean'
default: false,
},
myFloat: {
type: 'float'
default: 0.1,
min: -1,
max: 1,
event: true,
}
}
schemaAnyDef
- Source:
Properties:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
type |
String |
<optional> |
'any'
|
Parameter of any type. |
default |
Mixed | Default value of the parameter. |
||
nullable |
Boolean |
<optional> |
false
|
Define if the parameter is nullable. If
set to |
event |
Boolean |
<optional> |
false
|
Define if the parameter is a volatile, e.g.
set its value back to |
filterChange |
Boolean |
<optional> |
true
|
Setting this option to |
immediate |
Boolean |
<optional> |
false
|
Setting this option to |
metas |
Object |
<optional> |
{}
|
Optionnal metadata of the parameter. |
Note that the any
type always return a shallow copy of the state internal
value. Mutating the returned value will not modify the internal state.
Type:
- Object
schemaBooleanDef
- Source:
Properties:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
type |
String |
'boolean'
|
Define a boolean parameter. |
|
default |
Boolean | Default value of the parameter. |
||
nullable |
Boolean |
<optional> |
false
|
Define if the parameter is nullable. If
set to |
event |
Boolean |
<optional> |
false
|
Define if the parameter is a volatile, e.g.
set its value back to |
filterChange |
Boolean |
<optional> |
true
|
Setting this option to |
immediate |
Boolean |
<optional> |
false
|
Setting this option to |
metas |
Object |
<optional> |
{}
|
Optionnal metadata of the parameter. |
Type:
- Object
schemaEnumDef
- Source:
Properties:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
type |
String |
<optional> |
'enum'
|
Enum parameter. |
default |
Mixed | Default value of the parameter. |
||
list |
Array | Possible values of the parameter. |
||
nullable |
Boolean |
<optional> |
false
|
Define if the parameter is nullable. If
set to |
event |
Boolean |
<optional> |
false
|
Define if the parameter is a volatile, e.g.
set its value back to |
filterChange |
Boolean |
<optional> |
true
|
Setting this option to |
immediate |
Boolean |
<optional> |
false
|
Setting this option to |
metas |
Object |
<optional> |
{}
|
Optionnal metadata of the parameter. |
Type:
- Object
schemaFloatDef
- Source:
Properties:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
type |
String |
<optional> |
'float'
|
Float parameter. |
default |
Mixed | Default value. |
||
min |
Number |
<optional> |
-Infinity
|
Minimum value. |
max |
Number |
<optional> |
+Infinity
|
Maximum value. |
nullable |
Boolean |
<optional> |
false
|
Define if the parameter is nullable. If
set to |
event |
Boolean |
<optional> |
false
|
Define if the parameter is a volatile, e.g.
set its value back to |
filterChange |
Boolean |
<optional> |
true
|
Setting this option to |
immediate |
Boolean |
<optional> |
false
|
Setting this option to |
metas |
Object |
<optional> |
{}
|
Optionnal metadata of the parameter. |
Type:
- Object
schemaIntegerDef
- Source:
Properties:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
type |
String |
'integer'
|
Define a boolean parameter. |
|
default |
Mixed | Default value of the parameter. |
||
min |
Number |
<optional> |
-Infinity
|
Minimum value of the parameter. |
max |
Number |
<optional> |
+Infinity
|
Maximum value of the parameter. |
nullable |
Boolean |
<optional> |
false
|
Define if the parameter is nullable. If
set to |
event |
Boolean |
<optional> |
false
|
Define if the parameter is a volatile, e.g.
set its value back to |
filterChange |
Boolean |
<optional> |
true
|
Setting this option to |
immediate |
Boolean |
<optional> |
false
|
Setting this option to |
metas |
Object |
<optional> |
{}
|
Optionnal metadata of the parameter. |
Type:
- Object
schemaStringDef
- Source:
Properties:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
type |
String |
'string'
|
Define a boolean parameter. |
|
default |
Mixed | Default value of the parameter. |
||
nullable |
Boolean |
<optional> |
false
|
Define if the parameter is nullable. If
set to |
event |
Boolean |
<optional> |
false
|
Define if the parameter is a volatile, e.g.
set its value back to |
filterChange |
Boolean |
<optional> |
true
|
Setting this option to |
immediate |
Boolean |
<optional> |
false
|
Setting this option to |
metas |
Object |
<optional> |
{}
|
Optionnal metadata of the parameter. |
Type:
- Object
updateHook(updates, currentValues, contextopt) → {Object}
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
updates |
Object | Update object as given on a set callback, or result of the previous hook |
||
currentValues |
Object | Current values of the state. |
||
context |
Object |
<optional> |
null
|
Optionnal context passed by the creator of the update. |
Returns:
The "real" updates to be applied on the state.
- Type
- Object