SharedStateCollection

client. SharedStateCollection

The SharedStateCollection interface represent a collection of all states created from a given schema name on the network, at the execption of the ones created by the current node.

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

Extends

Members

length :number

Description:
  • Size of the collection

Source:
Inherited From:

Size of the collection

Type:
  • number

Methods

(async) detach()

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

Source:
Inherited From:

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:
Inherited From:
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:
Inherited From:
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:
Inherited From:
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:
Inherited From:
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:
Inherited From:
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:
Inherited From:
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:
Inherited From:
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:
Inherited From:
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:
Inherited From:
Parameters:
Name Type Attributes Default Description
callback server.SharedStateCollection~onUpdateCallback | client.SharedStateCollection~onUpdateCallback

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:
Inherited From:
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:
Inherited From:
Parameters:
Name Type Description
func function

Function that defines the sort order.

Type Definitions

onUpdateCallback(state, newValues, oldValues, contextopt)

Source:
Parameters:
Name Type Attributes Default Description
state client.SharedState

State that triggered the update.

newValues Object

Key / value pairs of the updates that have been applied to the state.

oldValues Object

Key / value pairs of the updated params before the updates has been applied to the state.

context Mixed <optional>
null

Optionnal context object that has been passed with the values updates in the set call.