fix(plugin-workflow): fix cycling association stackoverflow (#2892)

This commit is contained in:
Junyi 2023-10-23 00:05:15 +08:00 committed by GitHub
parent f51c06e7b8
commit b18822aaa9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 4 deletions

View File

@ -29,6 +29,7 @@ function useAssociatedFields() {
.useOptions({ .useOptions({
types: [matchToManyField], types: [matchToManyField],
appends: null, appends: null,
depth: 4,
}) })
?.filter(Boolean); ?.filter(Boolean);
return { return {

View File

@ -30,6 +30,7 @@ export type OptionsOfUseVariableOptions = {
children?: string; children?: string;
}; };
appends?: string[] | null; appends?: string[] | null;
depth?: number;
}; };
export const defaultFieldNames = { label: 'label', value: 'value', children: 'children' } as const; export const defaultFieldNames = { label: 'label', value: 'value', children: 'children' } as const;
@ -167,17 +168,20 @@ function getNextAppends(field, appends: string[] | null): string[] | null {
return appends.filter((item) => item.startsWith(fieldPrefix)).map((item) => item.replace(fieldPrefix, '')); return appends.filter((item) => item.startsWith(fieldPrefix)).map((item) => item.replace(fieldPrefix, ''));
} }
function filterTypedFields({ fields, types, appends, compile, getCollectionFields }) { function filterTypedFields({ fields, types, appends, depth = 1, compile, getCollectionFields }) {
return fields.filter((field) => { return fields.filter((field) => {
const match = types?.length ? types.some((type) => matchFieldType(field, type)) : true; const match = types?.length ? types.some((type) => matchFieldType(field, type)) : true;
if (isAssociationField(field)) { if (isAssociationField(field)) {
if (appends === null) { if (appends === null) {
if (!depth) {
return false;
}
return ( return (
match || match ||
filterTypedFields({ filterTypedFields({
fields: getNormalizedFields(field.target, { compile, getCollectionFields }), fields: getNormalizedFields(field.target, { compile, getCollectionFields }),
types, types,
// depth: depth - 1, depth: depth - 1,
appends, appends,
compile, compile,
getCollectionFields, getCollectionFields,
@ -286,6 +290,7 @@ function loadChildren(option) {
collection: option.field.target, collection: option.field.target,
types: option.types, types: option.types,
appends, appends,
depth: option.depth - 1,
...this, ...this,
}); });
option.loadChildren = null; option.loadChildren = null;
@ -306,6 +311,7 @@ export function getCollectionFieldOptions(options): VariableOption[] {
collection, collection,
types, types,
appends = [], appends = [],
depth = 1,
compile, compile,
getCollectionFields, getCollectionFields,
fieldNames = defaultFieldNames, fieldNames = defaultFieldNames,
@ -317,7 +323,7 @@ export function getCollectionFieldOptions(options): VariableOption[] {
const result: VariableOption[] = filterTypedFields({ const result: VariableOption[] = filterTypedFields({
fields: computedFields, fields: computedFields,
types, types,
// depth, depth,
appends, appends,
compile, compile,
getCollectionFields, getCollectionFields,
@ -335,7 +341,7 @@ export function getCollectionFieldOptions(options): VariableOption[] {
isLeaf, isLeaf,
loadChildren: isLeaf ? null : boundLoadChildren, loadChildren: isLeaf ? null : boundLoadChildren,
field, field,
// depth, depth,
appends, appends,
types, types,
}; };