Object Primitive


Object primitive represents states that can be modelled in a single JSON object. An example usage could be storing the score of a game or score of a live match

Create state

To create the state, you call the method createObject(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.

let score = {player1: 2, player2: 6};
hamoni.createObject("score", score)
      .then(primitive => {
        objectPrimitive = 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.

score.player1 = 3;
objectPrimitive.update(score)

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

objectPrimitive.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 = objectPrimitive.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("score")
  .then(primitive => {
    objectPrimitive = primitive;
    objectPrimitive.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("score")
  .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 ""