Skip to content

Commit

Permalink
Small optimization in getNodeAtPath (#10316)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oreilles committed Dec 6, 2021
1 parent d78e45a commit 8c468ce
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions app/src/composables/use-field-tree.ts
Expand Up @@ -49,23 +49,7 @@ export function useFieldTree(
.filter(filter)
.flatMap((field) => makeNode(field, parent));

return fields.length ? fields : undefined;
}

function getRelatedCollections(field: Field): string[] {
const relation = getRelationForField(field);
if (!relation?.meta) return [];
const relationType = getRelationType({ relation, collection: field.collection, field: field.field });
switch (relationType) {
case 'o2m':
return [relation!.meta!.many_collection];
case 'm2o':
return [relation!.meta!.one_collection];
case 'm2a':
return relation!.meta!.one_allowed_collections!;
default:
return [];
}
return fields.length > 0 ? fields : undefined;
}

function makeNode(field: Field, parent?: FieldNode): FieldNode | FieldNode[] {
Expand All @@ -91,6 +75,22 @@ export function useFieldTree(
});
}

function getRelatedCollections(field: Field): string[] {
const relation = getRelationForField(field);
if (!relation?.meta) return [];
const relationType = getRelationType({ relation, collection: field.collection, field: field.field });
switch (relationType) {
case 'o2m':
return [relation!.meta!.many_collection];
case 'm2o':
return [relation!.meta!.one_collection];
case 'm2a':
return relation!.meta!.one_allowed_collections!;
default:
return [];
}
}

function getRelationForField(field: { collection: string; field: string }) {
const relations = [
...relationsStore.getRelationsForField(field.collection, field.field),
Expand All @@ -103,12 +103,11 @@ export function useFieldTree(
);
}

function getNodeAtPath(path: string, root?: FieldNode[]): FieldNode | undefined {
const [field, ...follow] = path.split('.');
function getNodeAtPath([field, ...path]: string[], root?: FieldNode[]): FieldNode | undefined {
for (const node of root || []) {
if (node.field === field) {
if (follow.length) {
return getNodeAtPath(follow.join('.'), node.children);
if (path.length) {
return getNodeAtPath(path, node.children);
} else {
return node;
}
Expand All @@ -119,7 +118,7 @@ export function useFieldTree(
function loadFieldRelations(path: string) {
if (!visitedPaths.value.has(path)) {
visitedPaths.value.add(path);
const node = getNodeAtPath(path, treeList.value);
const node = getNodeAtPath(path.split('.'), treeList.value);
for (const child of node?.children || []) {
child.children = getTree(child.relatedCollection, child);
}
Expand Down

0 comments on commit 8c468ce

Please sign in to comment.