List Primitive


List primitive represents states that contains a list of object. This can be used for real-time data table, charts, and as you see fit.

Create state

To create the state, you call the method createList(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.createList("score", [1, 2, 3])
      .then(primitive => {
        listPrimitive = 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. You can either update an item in a particular index or add new data to the list.

To add a new item, you use the push(newItem) method passing it the new data to add to the list

listPrimitive.push({label: March, amount: "$25.52"})

If it succeeds, it'll notify clients that new item has been added to the list

Swift SDK coming soon

Java SDK coming soon

To update an item at a particular index, you call the update(index, updatedItem) method passing it the index and the record to replace with the value at that index

listPrimitive.update(2, {label: June, amount: "$54.52"})

If it succeeds, it'll notify clients that an item was updated

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.

There are two types of update. One is called when an item is added to the list

listPrimitive.onItemAdded( newItem =>           console.log(`${newItem.index}: ${newItem.value}`));

The other is called when an item at a particular index is changed

listPrimitive.onItemUpdated( updatedItem => console.log(`${updatedItem.index}: ${updatedItem.value}`));

Swift SDK coming soon

Java SDK coming soon

Getting state value

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

To get all the items in a list primitive you use getAll()

let state = listPrimitive.getAll();

To get a a single item, you use getByIndex(index) passing it the index to of the item to retrieve

let state = listPrimitive.getByIndex(2);

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 => {
    listPrimitive = primitive;
    listPrimitive.onItemUpdated( item => console.log(item.value))
  })
  .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 " "