Value Primitive


Value primitive is a kind of state that is used to store simple information represented with datatypes like string, boolean or numbers.

Create state

To create the state, you call the method createValue(stateName, stateValue) which is available from the object initialised from the Hamoni class. It takes the name of the state and the value to store as parameters to the method.

hamoni.createValue("message-count", 0)
      .then(primitive => {
        valuePrimitive = primitive;
      })
      .catch(error => console.log(error));

Swift SDK coming soon

Java SDK coming soon

Update state

If the state is created, you get an object that represents that state. In it you have function to update the state.

valuePrimitive.update(25)

This updates the state and notifies all clients about the changes

Swift SDK coming soon

Java SDK coming soon

Receiving state change notification

If the server completes persisting the new state, it notifies connected clients including the one that made the update. To get this update you need to tell Sync which method to call when there's an update

valuePrimitive.onUpdated( updatedState => console.log(updatedState));

It calls the method with the updated state value

Swift SDK coming soon

Java SDK coming soon

Getting state value

You can get the value of the state by calling get() on the state primitive object

let state = valuePrimitive.get();
console.log(state)

Swift SDK coming soon

Java SDK coming soon

Getting existing state primitive

If you already created a state primitive you can get it by calling get(stateName) with the name of the state as parameter. This gets the state from the server and returns an object of that primitive

hamoni
  .get("message-count")
  .then(primitive => {
    valuePrimitive = primitive;
    valuePrimitive.onUpdated( state => console.log(state))
  })
  .catch(error => console.log(error));

Swift SDK coming soon

Java SDK coming soon

Reconnection

If a client reconnects after losing connection, the sdk fetches the state from the server. If it has been updated, it updates the local state and calls the callback passed in onSync() method

hamoni
  .get("message-count")
  .then(primitive => {
    primitive.onSync( state => console.log(state))
  })
  .catch(error => console.log(error));

Swift SDK coming soon

Java SDK coming soon

results matching ""

    No results matching ""