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.
- Saves run one at a time, in order, each with its own
AbortSignal. A save that resolves firesvn-diagram-savedwith the change. - A save that rejects drops that entry and every later one from the
queue. The diagram cancels any gesture or nudge burst, then reverts them,
newest first: an executed or redone command is reverted, an undone one is
applied again. It restores both history stacks from the failed entry's
copy, and fires
vn-diagram-save-failedwith{ error, code: 'manager-rejected', reverted }(the reverted changes, newest first), thenvn-diagram-history-changedwhen the restored stacks changecanUndo()orcanRedo(), thenvn-diagram-selection-changedif the reverts removed a selected item. Connect mode whose source the reverts removed ends, a removed target is dropped, and an armed port whose node they removed is disarmed, as for any removal (06-keyboard-and-focus ยง Connect mode). The model and the history return together to the state just before the failed change, so a failed undo can be made again. The reverts store nothing and fire novn-diagram-changed: they bring the diagram back to what the manager holds. - Each entry keeps its own reference to its command, so a command trimmed
from the history by
historyLimitwhile its save is pending is still reverted.clearHistory()empties the stack copies in the queue too, so a later failure restores an empty history. hasPendingSaves()andwhenSaved()let a host show a saving state and wait before leaving the page.setDataManager()(another manager or null) anddestroy()abort the save in flight and drop the queue: a save that settles after that is ignored, and the model keeps the changes until the next load.- Without a manager nothing is queued: edits apply and stay in the page, and
toJSON()returns them.
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.