Source: widget/companionCore/State.js

(function() {
    /** widget state functionality */
    Help4.widget.companionCore.State = class {
        static #STATE = {};

        /**
         * sets the state for a certain widget
         * @param {string} name - widget name/id
         * @param {*} state - widget state
         * @param {Help4.widget.Widget.Context} context - widget context
         * @returns {Promise<void>}
         */
        static async set(name, state, context) {
            this.#STATE[name] = Help4.cloneValue(state);
            await _fireStatusEvent.call(this, context);
        }

        /**
         * delivers the current state
         * @param {string} [name] - widget name/id
         * @returns {*}
         */
        static get(name) {
            return Help4.cloneValue(name ? this.#STATE[name] : this.#STATE);
        }

        /**
         * restores a previous state
         * @param {Object} state
         */
        static restore(state) {
            Object.keys(this.#STATE).forEach(key => delete this.#STATE[key]);
            Object.entries(state).forEach(([name, state]) => this.#STATE[name] = Help4.cloneValue(state));
        }
    }

    /**
     * fires status update events
     * @memberof Help4.widget.companionCore.State
     * @private
     * @param {Help4.widget.Widget.Context} context
     * @returns {Promise<void>}
     */
    async function _fireStatusEvent(context) {
        const {TYPES} = Help4.EventBus;
        const {eventBus} = context;
        const data = this.get();
        eventBus.fire({type: TYPES.widgetStatus, data});
    }
})();