Data Manager Implementation in Vanilla-Diagram

The diagram's data is a graph, nodes and connectors, and its data manager has the Gantt's shape (two fetches that honour an AbortSignal, one batch write, onConfigChanged, reset(), destroy()) with nodes and connectors in place of tasks and dependencies. It is not the grid's DataManager, which pages and filters rows, has no writes, and whose class name would clash on the page.

The contract

class VanillaDiagramDataManager {
    onConfigChanged: ((reason?: string) => void) | null;
    fetchNodes(options?: { signal?: AbortSignal }): Promise<VanillaDiagramNode[]>;            // base: []
    fetchConnectors(options?: { signal?: AbortSignal }): Promise<VanillaDiagramConnector[]>;  // base: []
    saveChanges(changes: VanillaDiagramChangeSet, options?: { signal?: AbortSignal }): Promise<void>;  // base: rejects
    reset(): void;
    destroy(): void;
}

A change set is one command's changes: { nodes: { added, updated, removed }, connectors: { added, updated, removed } }. updated holds whole items after the change, not patches, so a server stores them without merging; removed holds ids. An undo or a redo is a change set like any other. The base class rejects every save, so a fetch-only manager pairs with read-only.

Ids. The diagram assigns the id of each node and connector a user adds: crypto.randomUUID() where the browser offers it, else a counter with a random suffix (a page on plain http off localhost has no randomUUID()). The host may set its own in vn-diagram-node-adding and vn-diagram-connecting. A manager stores ids as given, so nothing is ever remapped. A paste or a duplicate gives every item it adds a new id the same way (11-clipboard-and-export): a pasted copy is a new item to the manager.

The static manager

VanillaDiagramStaticDataManager keeps copies: the constructor copies what it is given, its fetches and getters return copies, and saveChanges() stores copies. Unlike the Gantt's static manager, which shares its task objects with the task grid, the model and the manager stay apart, so a revert in the diagram leaves what the manager stored as it was. It accepts toJSON()'s document, and throws an Error with code: 'unsupported-version' for a version above 1. saveChanges() replaces updated items by id, removes ids and appends added ones; an unknown id in updated or removed rejects the whole change set and changes nothing. setNodes() and setConnectors() fire onConfigChanged.

Saving

Saves are optimistic: a change applies at once, and its change set joins the queue (VanillaDiagramSaveQueue) right after. The user never waits. A queue entry holds the change ({ command, source }), its change set (built from the model as it was right after the change), a copy of the undo and redo stacks from just before it, and the command.

The queue's storage of the stacks costs at most 2 x historyLimit references per entry; a change set is sized by its command, not the diagram.

Loading

01-web-component describes loadDiagramAsync(): it waits for the pending saves before it fetches, so it reads what was stored, then builds the new model in slices, swaps it in and clears the selection and the history. The manager hands over the whole diagram: there is no fetchRegion(). At 100,000 nodes and 100,000 connectors the model builds in well under a second, and only what is in view is drawn (14-large-diagrams); a partial model would break undo, toJSON(), select all, the layout and the outline.