Source: interfaces/model_base.js

/*   Hyrrokkin - A visual modelling tool for constructing directed graphs.

     Copyright (C) 2022-2026 Visual Topology Ltd

     Licensed under the MIT License
*/

var hyrrokkin = hyrrokkin || {};

/**
 * @callback notificationCallback
 */

/**
 * @callback loadCallback
 *
 * @param {object} api the hyrrokkin api
 */

hyrrokkin.ModelBase = class {

    /**
     * Create an engine instance.
     *
     * The engine will attach to the hyrrokkin API and respond to graph updates.
     */
    constructor(options) {
        this.workspace_id = options["workspace_id"];
        this.topology_id = options["topology_id"];
        this.event_handlers = [];
    }

    get_workspace_id() {
        return this.workspace_id;
    }

    get_topology_id() {
        return this.topology_id;
    }

    /**
     * One-time start up
     */
    async load() {
    }

    /**
     * Get a descriptive name for this engine
     *
     * @returns {string}
     */
    get_name() {
        return "engine_base";
    }

    /**
     * Register a callback for when a topology has been loaded
     * @param {loadCallback} load_callback
     */
    set_load_callback(load_callback) {
    }

    /**
     * Restart the engine, discarding any existing engine execution state
     */
    request_restart() {
    }

    /**
     * Called when a node is requested to be added to the graph
     *
     * @param {string} node_id a unique string that identifies the node within the graph, may be empty
     * @param {string} node_type_id a string of the form <package_id>:<node_type>
     * @param {object} metadata an object containing metadata keys/values
     * @param {copy_from_node_id} copy properties and data from this node
     *
     * @return the node_id that was actually added, if yet known, or empty string otherwise
     */
    request_add_node(node_id, node_type_id, metadata, copy_from_node_id) {
    }

    /**
     * Called to request an update to the node's metadata
     *
     * @param {string} node_id a unique string that identifies the node within the graph
     * @param {object} metadata an object containing the metadata keys/values
     */
    request_update_node_metadata(node_id, metadata) {
    }

    /**
     * Called when a node is removed from the graph
     *
     * @param {string} node_id a unique string that identifies the node within the graph
     */
    request_remove_node(node_id) {
    }

    /**
     * Called when a node is to be blocked / unblocked
     *
     * @param {string} node_id a unique string that identifies the node within the graph
     * @param {boolean} is_blocked whether the node is to be blocked or not
     */
    request_node_blocked(node_id, is_blocked) {
    }

    /**
     * Called when a link is added to the graph
     *
     * @param {string} link_id optional identifier, may be empty
     * @param {string} link_type_id a string of the form <package_id>:<link_type>
     * @param {string} from_node_id
     * @param {string} from_port_name
     * @param {string} to_node_id
     * @param {string} to_port_name
     */
    request_add_link(link_id, link_type_id, from_node_id, from_port_name, to_node_id, to_port_name) {
    }

    /**
     * Called when a link is removed from the graph
     *
     * @param {string} link_id
     */
    request_remove_link(link_id) {
    }

    /**
     * Called when the graph is loaded from an object
     */
    request_load(from_obj) {
    }

    /**
     * Called when the graph is cleared of all nodes and links
     */
    request_clear() {
    }

    /**
     * Called when the design metadata is updated
     */
    request_update_design_metadata(updated_metadata) {
    }

    /**
     * Called when execution is paused - no new node executions should start
     */
    request_pause() {
    }

    /**
     * Called when execution is resumed - new node executions can re-commence
     */
    request_resume() {
    }

    /**
     * Called when the graph is closed
     */
    request_close() {
    }

    /**
     * bind this engine to a hyrrokkin instance
     *
     * @param hyrrokkin the instance to bind to
     */
    async bind(hyrrokkin) {
        if (hyrrokkin.add_node_event_handler) {

            // hook up to hyrrokkin events
            this.event_handlers.push(hyrrokkin.add_node_event_handler("request_add", (event_details) => {
                this.request_add_node(undefined, event_details.node_type_id, event_details.metadata, event_details.copy_from_node_id);
            }));

            this.event_handlers.push(hyrrokkin.add_node_event_handler("request_remove", (event_details) => {
                this.request_remove_node(event_details.node_id);
            }));

            this.event_handlers.push(hyrrokkin.add_node_event_handler("request_update_metadata", (event_details) => {
                this.request_update_node_metadata(event_details.node_id, event_details.metadata);
            }));

            this.event_handlers.push(hyrrokkin.add_node_event_handler("request_blocked", (event_details) => {
                this.request_node_blocked(event_details.node_id, event_details.is_blocked, event_details.to_here);
            }));

            this.event_handlers.push(hyrrokkin.add_link_event_handler("request_add", (event_details) => {
                this.request_add_link("",event_details.link_type_id, event_details.from_node_id, event_details.from_port_name,
                    event_details.to_node_id, event_details.to_port_name);
            }));

            this.event_handlers.push(hyrrokkin.add_link_event_handler("request_remove", (event_details) => {
                this.request_remove_link(event_details.link_id);
            }));

            this.event_handlers.push(hyrrokkin.add_design_event_handler("request_clear", (event_details) => {
                this.request_clear(event_details);
            }));

            this.event_handlers.push(hyrrokkin.add_design_event_handler("request_load", (event_details) => {
                this.request_load(event_details);
            }));

            this.event_handlers.push(hyrrokkin.add_design_event_handler("request_update_metadata", (event_details) => {
                this.request_update_design_metadata(event_details.metadata);
            }));

            this.event_handlers.push(hyrrokkin.add_design_event_handler("request_pause", (event_details) => {
                this.request_pause();
            }));

            this.event_handlers.push(hyrrokkin.add_design_event_handler("request_resume", (event_details) => {
                this.request_resume();
            }));

            this.event_handlers.push(hyrrokkin.add_design_event_handler("request_restart", (event_details) => {
                this.request_restart();
            }));
        }
    }

    /**
     * Load the topology from storage
     *
     * @returns {Promise<void>}
     */
    async load() {
    }

    async save() {
    }

    async get_save_link() {
    }

    async load_from_file(file) {
    }

    async load_from_url(url) {
    }

    /**
     * Open a client of a node or configuration
     *
     * @param target_id the id of the node (node_id) or configuration (package_id)
     * @param target_type the type of client ("node" or "configuration")
     * @param client_id a unique identifier for the client
     * @param client_options options for opening the client connection
     * @param client_service a client service instance that will receive messages from the node/configuration
     */
    open_client(target_id, target_type, client_id, client_options, client_service) {
    }

    /**
     * Close an open client
     *
     * @param target_id the id of the node (node_id) or configuration (package_id)
     * @param target_type the type of client ("node" or "configuration")
     * @param client_id a unique identifier for the client
     */
    close_client(target_id, target_type, client_id) {
    }

    /**
     * Forward a message to the client
     *
     * @param target_id the id of the node (node_id) or configuration (package_id)
     * @param target_type the type of client ("node" or "configuration")
     * @param client_id a unique identifier for the client
     * @param msg the message to send, one or more arguments that are string, arraybuffer or JSON-serialisable
     */
    forward_client_message(target_id, target_type, client_id, ...msg) {
    }

}