UNPKG

@gravity-ui/graph

Version:

Modern graph editor component

120 lines (119 loc) 3.87 kB
import { Component } from "../../../lib"; export declare const IS_PORT_TYPE: "Port"; export type TPortId = string | number | symbol; /** * Port data structure * Represents a connection point that can be attached to blocks, anchors, or custom components */ export type TPort = { /** Unique identifier for the port */ id: TPortId; /** X coordinate of the port */ x: number; /** Y coordinate of the port */ y: number; /** Component that owns this port (block, anchor, etc.) */ component?: Component; /** Whether the port is waiting for position data from its component */ lookup?: boolean; }; /** * PortState - Reactive state container for a connection port * * Manages the lifecycle and state of a port, including position updates, * component ownership, and listener management for connections that use this port. * * ## Key Concepts: * * ### Lazy Creation * Ports are created on-demand when connections need them, even if the target * component doesn't exist yet. This solves initialization order problems. * * ### Lookup State * When `lookup: true`, the port is waiting for its component to provide coordinates. * When `lookup: false`, the port has valid coordinates and can be used for rendering. * * ### Listener Management * Tracks which components are listening to this port's changes. When no listeners * remain and no component owns the port, it can be safely garbage collected. */ export declare class PortState { $state: import("@preact/signals-core").Signal<TPort>; owner?: Component; /** * Set of references observing this port's changes * * Used for reference counting to determine when the port can be safely deleted. * Stores actual object references to ensure accurate counting and prevent duplicates. */ observers: Set<unknown>; /** * Get the port's unique identifier * * @returns {TPortId} The port's ID */ get id(): TPortId; /** * Get the port's X coordinate * * @returns {number} The X coordinate */ get x(): number; /** * Get the port's Y coordinate * * @returns {number} The Y coordinate */ get y(): number; /** * Get the component that owns this port * * @returns {Component | undefined} The owning component, if any */ get component(): Component | undefined; /** * Get whether the port is in lookup state (waiting for coordinates) * * @returns {boolean | undefined} True if waiting for coordinates, false if resolved */ get lookup(): boolean | undefined; constructor(port: TPort); /** * Set the component that owns this port * @param owner Component that will own this port (block, anchor, etc.) * @returns void */ setOwner(owner: Component): void; /** * Remove the current owner from this port */ removeOwner(): void; /** * Add an observer reference to this port * Stores the actual reference for accurate counting * @param observer The object observing this port */ addObserver(observer: unknown): void; /** * Remove an observer reference from this port * Removes the actual reference from the set * @param observer The object to stop observing this port */ removeObserver(observer: unknown): void; /** * Update the port's position coordinates * @param x New X coordinate * @param y New Y coordinate */ setPoint(x: number, y: number): void; /** * Update port state with partial data * @param port Partial port data to merge with current state */ updatePort(port: Partial<TPort>): void; /** * Check if this port can be safely deleted * @returns true if port has no owner and no observers */ canBeDeleted(): boolean; }