Program Listing for File State.h

Program Listing for File State.h#

Return to documentation for file (include/Karana/WebUI/State.h)

/*
 * Copyright (c) 2024-2026 Karana Dynamics Pty Ltd. All rights reserved.
 *
 * NOTICE TO USER:
 *
 * This source code and/or documentation (the "Licensed Materials") is
 * the confidential and proprietary information of Karana Dynamics Inc.
 * Use of these Licensed Materials is governed by the terms and conditions
 * of a separate software license agreement between Karana Dynamics and the
 * Licensee ("License Agreement"). Unless expressly permitted under that
 * agreement, any reproduction, modification, distribution, or disclosure
 * of the Licensed Materials, in whole or in part, to any third party
 * without the prior written consent of Karana Dynamics is strictly prohibited.
 *
 * THE LICENSED MATERIALS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
 * KARANA DYNAMICS DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-INFRINGEMENT, AND
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * IN NO EVENT SHALL KARANA DYNAMICS BE LIABLE FOR ANY DAMAGES WHATSOEVER,
 * INCLUDING BUT NOT LIMITED TO LOSS OF PROFITS, DATA, OR USE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, WHETHER IN CONTRACT, TORT,
 * OR OTHERWISE ARISING OUT OF OR IN CONNECTION WITH THE LICENSED MATERIALS.
 *
 * U.S. Government End Users: The Licensed Materials are a "commercial item"
 * as defined at 48 C.F.R. 2.101, and are provided to the U.S. Government
 * only as a commercial end item under the terms of this license.
 *
 * Any use of the Licensed Materials in individual or commercial software must
 * include, in the user documentation and internal source code comments,
 * this Notice, Disclaimer, and U.S. Government Use Provision.
 */

#pragma once
#include "Karana/KCore/Id.h"
#include "Karana/KCore/SharedPointer.h"
#include "Karana/WebUI/Router.h"
#include <functional>
#include <memory>
#include <nlohmann/json.hpp>

namespace Karana::WebUI {
    /**
     * @class State
     * @brief State shared on the client
     */
    class State {
      public:
        /**
         * @brief State constructor
         * @param router The Router to use
         * @param init_value The initial state
         * @param sync Sync among server and clients
         */
        State(Router &router, const nlohmann::json &init_value = {}, bool sync = true);

        /**
         * @brief State destructor
         */
        virtual ~State();

        /**
         * @brief 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.
         *
         * @return The state value
         */
        nlohmann::json get() const;

        /**
         * @brief Set a new state value
         * @param new_value The new state value
         */
        void set(const nlohmann::json &new_value);

        /**
         * @brief Add a callback invoked when the state changes
         * @param on_change The callback
         * @return A unique id for the callback
         */
        Karana::Core::id_t onChange(std::function<void(const nlohmann::json &state)> on_change);

        /**
         * @brief Remove a callback invoked when the state changes
         * @param id Id of the callback returned by a previous onChange
         */
        void cancelOnChange(Karana::Core::id_t id);

        /**
         * @brief Get a handle to the state's channel
         * @return The channel
         */
        const Karana::Core::ks_ptr<Channel> &channel() const;

      private:
        std::unique_ptr<struct StateImpl> _impl;
    };

} // namespace Karana::WebUI