SharedState

SharedState

Representation of a shared state. The class and its instances are the same client-side and server-side.

Methods

(async) delete()

Source:
See:

Delete the state. Only the creator/owner of the state (i.e. a state created using create) can use this method. If a non-owner call this method (i.e. a state created using attach), an error will be thrown.

(async) detach()

Source:
See:

Detach from the state. If the client is the creator of the state, the state is deleted and all attached nodes get notified

get(name) → {Mixed}

Source:

Get a value of the state by its name

Parameters:
Name Type Description
name String

Name of the param. Throws an error if the name is invalid.

Returns:
Type
Mixed

getDefaults() → {Object}

Source:

Get the default values that has been declared in the schema.

Returns:
Type
Object

getInitValues() → {Object}

Source:

Get the values with which the state has been initialized.

Returns:
Type
Object

getSchema(nameopt) → {Object}

Source:

Get the schema that describes the state.

Parameters:
Name Type Attributes Default Description
name String <optional>
null

if given, returns only the definition of the given param name. Throws an error if the name is invalid.

Returns:
Type
Object

getValues() → {Object}

Source:

Get a all the key / value pairs of the state.

Returns:
Type
Object

onDelete(callback)

Source:

Register a function to execute when the state is deleted. Only called if the node was the creator of the state. Is called after onDetach

Parameters:
Name Type Description
callback function

callback to execute when the state is deleted.

onDetach(callback)

Source:

Register a function to execute when detaching from the state

Parameters:
Name Type Description
callback function

callback to execute when detaching from the state. wether the client as called detach, or the state has been deleted by its creator.

(async) set(updates, contextopt) → {Promise.<Object>}

Source:
See:

Updates values of the state. Wait for all subscriptions to be resolved before resolving itself, i.e.:

const a = await server.stateManager.create('a');
let asyncCallbackCalled = false;

a.subscribe(updates => {
  return new Promise(resolve => {
    setTimeout(() => {
      asyncCallbackCalled = true;
      resolve();
    }, 100);
  });
});

await a.set({ bool: true });
assert.equal(asyncCallbackCalled, true);
Parameters:
Name Type Attributes Default Description
updates Object

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

context Mixed <optional>
null

optionnal context 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 any subscribe listeners.

Returns:

A promise to the (coerced) updates.

Type
Promise.<Object>

subscribe(callback, executeListeneropt)

Source:

Subscribe to state updates

Example
state.subscribe(async (newValues, oldValues) =>  {
  for (let [key, value] of Object.entries(newValues)) {
     switch (key) {
       // do something
     }
  }
});
Parameters:
Name Type Attributes Default Description
callback common.SharedState~subscribeCallback

callback to execute when an update is applied on the state.

executeListener Boolean <optional>
false

execute the given listener with current state values. (oldValues will be set to {}, and context to null)

Type Definitions

subscribeCallback(newValues, oldValues, contextopt)

Source:
See:
Example
state.subscribe(async (newValues, oldValues[, context=null]) =>  {
  for (let [key, value] of Object.entries(newValues)) {
     switch (key) {
       // do something
     }
  }
});
Parameters:
Name Type Attributes Default Description
newValues Object

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

oldValues Object

key / value pairs of the related params before the updates has been applied to the state.

context Mixed <optional>
null

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