ClientStateManager

ClientStateManager

The ClientStateManager allows to create new SharedStates, or attach to SharedStates created by other nodes (clients or server) on the network.

It can also observe all the SharedStates created on the network.

An instance of ClientStateManager is automatically created by the soundworks.Client at initialization (cf. client.Client#stateManager).

See client.Client#stateManager

Tutorial: https://soundworks.dev/guide/state-manager.html

// server-side
import { Server } from '@soundworks/server/index.js';

const server = new Server(config);
// define a class of shared state.
server.stateManager.defineClass('some-global-state', {
  myRandom: {
    type: 'float',
    default: 0,
  }
});

await server.start();

// create a global state server-side
const globalState = await server.stateManager.create('some-global-state');
// listen and react to the changes made by the clients
globalState.onUpdate(updates => console.log(updates));
// client-side
import { Client } from '@soundworks/client.index.js';

const client = new Client(config);
await client.start();

// attach to the global state created by the server
const globalState = await client.stateManager.attach('some-global-state');

// update the value of a `myRandom` parameter every seconds
setInterval(() => {
  globalState.set({ myRandom: Math.random() });
}, 1000);

Extends

Methods

(async) attach(className, stateIdOrFilteropt, filteropt) → {Promise.<SharedState>}

Description:
  • Attach to an existing SharedState instance.

    Alternative signatures:

    • stateManager.attach(className)
    • stateManager.attach(className, stateId)
    • stateManager.attach(className, filter)
    • stateManager.attach(className, stateId, filter)
Source:
Inherited From:
Example
const state = await client.stateManager.attach('my-class');
Parameters:
Name Type Attributes Default Description
className SharedStateClassName

Name of the class.

stateIdOrFilter number | Array.<string> <optional>
null

Id of the state to attach to. If null, attach to the first state found with the given class name (useful for globally shared states owned by the server).

filter Array.<string> <optional>
null

List of parameters of interest in the returned state. If set to null, no filter is applied.

Returns:
Type
Promise.<SharedState>

(async) create(className, initValuesopt) → {Promise.<SharedState>}

Description:
Source:
Inherited From:
Example
const state = await client.stateManager.create('my-class');
Parameters:
Name Type Attributes Default Description
className SharedStateClassName

Name of the class.

initValues Object.<string, any> <optional>
{}

Default values of the created shared state.

Returns:
Type
Promise.<SharedState>

(async) getClassDescription(className) → {SharedStateClassDescription}

Description:
  • Return a class description from a given class name

Source:
Inherited From:
Example
const classDescription = await client.stateManager.getClassDescription('my-class');
Parameters:
Name Type Description
className SharedStateClassName

Name of the shared state class. (cf. ServerStateManager)

Returns:
Type
SharedStateClassDescription

(async) getCollection(className, filteropt, optionsopt) → {Promise.<SharedStateCollection>}

Description:
  • Returns a collection of all the states created from a given shared state class.

    Alternative signatures:

    • stateManager.getCollection(className)
    • stateManager.getCollection(className, filter)
    • stateManager.getCollection(className, options)
    • stateManager.getCollection(className, filter, options)
Source:
Inherited From:
Example
const collection = await client.stateManager.getCollection(className);
Parameters:
Name Type Attributes Default Description
className SharedStateClassName

Name of the shared state class.

filter array | null <optional>
null

Filter parameter of interest for each state of the collection. If set to null, no filter applied.

options object <optional>
{}

Options.

Properties
Name Type Attributes Default Description
excludeLocal boolean <optional>
false

If set to true, exclude states created by the same node from the collection.

Returns:
Type
Promise.<SharedStateCollection>

(async) getSchema()

Source:
Inherited From:
Deprecated:

(async) observe(classNameopt, callback, options) → {Promise.<stateManagerDeleteObserveCallback>}

Description:
  • Observe all the SharedState instances that are created on the network.

    Notes:

    • The order of execution is not guaranteed between nodes, i.e. a state attached in the observe callback can be instantiated before the async create method resolves on the creator node.
    • Filtering, i.e. observedClassName and options.excludeLocal are handled on the node side, the server just notify all state creation activity and the node executes the given callbacks according to the different filter rules. Such strategy allows to simply share the observe notifications between all observers.

    Alternative signatures:

    • stateManager.observe(callback)
    • stateManager.observe(className, callback)
    • stateManager.observe(callback, options)
    • stateManager.observe(className, callback, options)
Source:
Inherited From:
Example
client.stateManager.observe(async (className, stateId) => {
  if (className === 'my-shared-state-class') {
    const attached = await client.stateManager.attach(className, stateId);
  }
});
Parameters:
Name Type Attributes Description
className SharedStateClassName <optional>

Optional class name to filter the observed states.

callback stateManagerObserveCallback

Function to be called when a new state is created on the network.

options object

Options.

Properties
Name Type Attributes Default Description
excludeLocal boolean <optional>
false

If set to true, exclude states created locally, i.e. by the same node, from the collection.

Returns:
  • Returns a Promise that resolves when the given callback as been executed on each existing states. The promise value is a function which allows to stop observing the states on the network.
Type
Promise.<stateManagerDeleteObserveCallback>

Type Definitions

(async) ObserveCallback(className, stateId, nodeId)

Source:
Parameters:
Name Type Description
className String

Name of the shared state class.

stateId Number

Id of the state.

nodeId Number

Id of the node that created the state.