SharedStateCollection

SharedStateCollection

The SharedStateCollection interface represent a collection of all states created from a given schema name on the network.

It can optionnaly exclude the states created by the current node.

See ClientStateManager#getCollection and ServerStateManager#getCollection for factory methods API

const collection = await client.stateManager.getCollection('my-schema');
const allValues = collection.getValues();
collection.onUpdate((state, newValues, oldValues, context) => {
  // do something
});

Members

(readonly) length :number

Description:
  • Size of the collection, alias size

Source:

Size of the collection, alias size

Type:
  • number

(readonly) schemaName :String

Description:
  • Name of the schema from which the collection has been created.

Source:

Name of the schema from which the collection has been created.

Type:
  • String

(readonly) size :number

Description:
  • Size of the collection, , alias length

Source:

Size of the collection, , alias length

Type:
  • number

Methods

(async) detach()

Description:
  • Detach from the collection, i.e. detach all underlying shared states.

Source:

filter(func)

Description:
  • Creates a shallow copy of a portion of the collection, filtered down to just the estates that pass the test implemented by the provided function (see Array.filter).

Source:
Parameters:
Name Type Description
func function

A function to execute for each element in the array. It should return a truthy to keep the element in the resulting array, and a f alsy value otherwise.

find(func)

Description:
  • Returns the first element of the collection that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

Source:
Parameters:
Name Type Description
func function

Function to execute for each element in the array. It should return a truthy value to indicate a matching element has been found.

Returns:

forEach(func)

Description:
  • Execute the given function once for each states of the collection (see Array.forEach).

Source:
Parameters:
Name Type Description
func function

A function to execute for each element in the array. Its return value is discarded.

get(name) → {Array.<any>}

Description:
  • Return the current param value of all the states in the collection.

Source:
Parameters:
Name Type Description
name String

Name of the parameter

Returns:
Type
Array.<any>

getDefaults() → {object}

Description:
  • Get the default values as declared in the schema.

Source:
Example
const defaults = state.getDefaults();
Returns:
Type
object

getSchema(nameopt) → {object}

Description:
  • Definition of schema from which the collection has been created.

Source:
Example
const schema = collection.getSchema();
Parameters:
Name Type Attributes Default Description
name string <optional>
null

If given, returns only the definition corresponding to the given param name.

Throws:

Throws if name does not correspond to an existing field of the schema.

Returns:
Type
object

getUnsafe(name) → {Array.<any>}

Description:
  • Similar to get but returns a reference to the underlying value in case of any type. May be usefull if the underlying value is big (e.g. sensors recordings, etc.) and deep cloning expensive. Be aware that if changes are made on the returned object, the state of your application will become inconsistent.

Source:
Parameters:
Name Type Description
name String

Name of the parameter

Returns:
Type
Array.<any>

getValues() → {Array.<Object>}

Description:
  • Return the current values of all the states in the collection.

Source:
Returns:
Type
Array.<Object>

getValuesUnsafe() → {Array.<Object>}

Description:
  • Return the current values of all the states in the collection.

    Similar to getValues but returns a reference to the underlying value in case of any type. May be usefull if the underlying value is big (e.g. sensors recordings, etc.) and deep cloning expensive. Be aware that if changes are made on the returned object, the state of your application will become inconsistent.

Source:
Returns:
Type
Array.<Object>

map(func)

Description:
  • Creates a new array populated with the results of calling a provided function on every state of the collection (see Array.map).

Source:
Parameters:
Name Type Description
func function

A function to execute for each element in the array. Its return value is added as a single element in the new array.

onAttach(callback, executeListener) → {function}

Description:
  • Register a function to execute when a state is added to the collection.

Source:
Parameters:
Name Type Default Description
callback function

callback to execute when a state is added to the collection.

executeListener function false

execute the callback with the states already present in the collection.

Returns:
  • Function that delete the registered listener.
Type
function

onDetach(callback) → {function}

Description:
  • Register a function to execute when a state is removed from the collection.

Source:
Parameters:
Name Type Description
callback function

callback to execute when a state is removed from the collection.

Returns:
  • Function that delete the registered listener.
Type
function

onUpdate(callback, executeListeneropt) → {function}

Description:
  • Subscribe to any state update of the collection.

Source:
Parameters:
Name Type Attributes Default Description
callback sharedStateCollectionOnUpdateCallback

Callback to execute when an update is applied on a state.

executeListener Boolean <optional>
false

Execute the callback immediately for all underlying states with current state values. (oldValues will be set to {}, and context to null)

Returns:
  • Function that delete the registered listener.
Type
function

(async) set(updates, contextopt)

Description:
  • Update all states of the collection with given values.

Source:
Parameters:
Name Type Attributes Default Description
updates object

key / value pairs of updates to apply to the state.

context mixed <optional>
null

optionnal contextual object that will be propagated alongside the updates of the state. The context is valid only for the current call and will be passed as third argument to all update listeners.

sort(func)

Description:
  • Sort the elements of the collection in place (see Array.sort).

Source:
Parameters:
Name Type Description
func function

Function that defines the sort order.