feat(ui): add addEdgeToMetadata graph helper

This commit is contained in:
psychedelicious 2024-07-23 09:48:22 +10:00
parent 071c7c7c7e
commit 7cee4e42a7
2 changed files with 30 additions and 0 deletions

View File

@ -522,6 +522,21 @@ describe('Graph', () => {
});
});
describe('addEdgeToMetadata', () => {
it('should add an edge to the metadata node', () => {
const g = new Graph();
const n1 = g.addNode({
id: 'n1',
type: 'img_resize',
});
g.upsertMetadata({ test: 'test' });
g.addEdgeToMetadata(n1, 'width', 'width');
const metadata = g._getMetadataNode();
expect(g.getEdgesFrom(n1).length).toBe(1);
expect(g.getEdgesTo(metadata as unknown as AnyInvocation).length).toBe(1);
});
});
describe('setMetadataReceivingNode', () => {
it('should set the metadata receiving node', () => {
const g = new Graph();

View File

@ -372,6 +372,21 @@ export class Graph {
return metadataNode;
}
/**
* Adds an edge from a node to a metadata field. Use this when the metadata value is dynamic depending on a node.
* @param fromNode The node to add an edge from
* @param fromField The field of the node to add an edge from
* @param metadataField The metadata field to add an edge to (will overwrite hard-coded metadata)
* @returns
*/
addEdgeToMetadata<TFrom extends AnyInvocation>(
fromNode: TFrom,
fromField: OutputFields<TFrom>,
metadataField: string
): Edge {
// @ts-expect-error `Graph` excludes `core_metadata` nodes due to its excessively wide typing
return this.addEdge(fromNode, fromField, this._getMetadataNode(), metadataField);
}
/**
* Set the node that should receive metadata. All other edges from the metadata node are deleted.
* @param node The node to set as the receiving node