feat(ui): use unique id for metadata in Graph class

This commit is contained in:
psychedelicious 2024-08-23 14:48:42 +10:00
parent 96f6e9e683
commit d9f4266630
2 changed files with 5 additions and 4 deletions

View File

@ -549,7 +549,7 @@ describe('Graph', () => {
it("should get the metadata node, creating it if it doesn't exist", () => {
const g = new Graph();
const metadata = g.getMetadataNode();
expect(metadata.id).toBe('core_metadata');
expect(metadata.id).toBe(g._metadataNodeId);
expect(metadata.type).toBe('core_metadata');
g.upsertMetadata({ test: 'test' });
const metadata2 = g.getMetadataNode();

View File

@ -1,5 +1,5 @@
import { getPrefixedId } from 'features/controlLayers/konva/util';
import { type ModelIdentifierField, zModelIdentifierField } from 'features/nodes/types/common';
import { METADATA } from 'features/nodes/util/graph/constants';
import { forEach, groupBy, isEqual, unset, values } from 'lodash-es';
import type {
AnyInvocation,
@ -31,6 +31,7 @@ export type GraphType = { id: string; nodes: Record<string, AnyInvocation>; edge
export class Graph {
_graph: GraphType;
_metadataNodeId = getPrefixedId('core_metadata');
constructor(id?: string) {
this._graph = {
@ -361,11 +362,11 @@ export class Graph {
*/
getMetadataNode(): S['CoreMetadataInvocation'] {
try {
const node = this.getNode(METADATA) as AnyInvocationIncMetadata;
const node = this.getNode(this._metadataNodeId) as AnyInvocationIncMetadata;
assert(node.type === 'core_metadata');
return node;
} catch {
const node: S['CoreMetadataInvocation'] = { id: METADATA, type: 'core_metadata' };
const node: S['CoreMetadataInvocation'] = { id: this._metadataNodeId, type: 'core_metadata' };
// @ts-expect-error `Graph` excludes `core_metadata` nodes due to its excessively wide typing
return this.addNode(node);
}