Class State#

Class Documentation#

class State#

State shared on the client.

Public Functions

State(Router &router, const nlohmann::json &init_value = {}, bool sync = true)#

State constructor.

Parameters:
  • router – The Router to use

  • init_value – The initial state

  • sync – Sync among server and clients

virtual ~State()#

State destructor.

nlohmann::json get() const#

Get the current state value.

Note that the state value is managed by the UI thread, so this will block until the UI thread can be synced.

Returns:

The state value

void set(const nlohmann::json &new_value)#

Set a new state value.

Parameters:

new_value – The new state value

Karana::Core::id_t onChange(std::function<void(const nlohmann::json &state)> on_change)#

Add a callback invoked when the state changes.

Parameters:

on_change – The callback

Returns:

A unique id for the callback

void cancelOnChange(Karana::Core::id_t id)#

Remove a callback invoked when the state changes.

Parameters:

id – Id of the callback returned by a previous onChange

const Karana::Core::ks_ptr<Channel> &channel() const#

Get a handle to the state’s channel.

Returns:

The channel