fix: remove caching logic (#6530)

This commit is contained in:
Zeke Zhang 2025-03-24 20:38:44 +08:00 committed by GitHub
parent 2fefcfbcf2
commit 57c8722c3c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 7 additions and 47 deletions

View File

@ -14,22 +14,22 @@ import { uid } from '@formily/shared';
import { Space, message } from 'antd'; import { Space, message } from 'antd';
import { isEqual } from 'lodash'; import { isEqual } from 'lodash';
import { isFunction } from 'mathjs'; import { isFunction } from 'mathjs';
import React, { useEffect, useState, useContext } from 'react'; import React, { useContext, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { import {
ClearCollectionFieldContext, ClearCollectionFieldContext,
NocoBaseRecursionField, NocoBaseRecursionField,
RecordProvider, RecordProvider,
SchemaComponentContext,
useAPIClient, useAPIClient,
useCollectionRecordData, useCollectionRecordData,
SchemaComponentContext,
} from '../../../'; } from '../../../';
import { Action } from '../action'; import { VariablePopupRecordProvider } from '../../../modules/variable/variablesProvider/VariablePopupRecordProvider';
import { isVariable } from '../../../variables/utils/isVariable'; import { isVariable } from '../../../variables/utils/isVariable';
import { getInnermostKeyAndValue } from '../../common/utils/uitls'; import { getInnermostKeyAndValue } from '../../common/utils/uitls';
import { Action } from '../action';
import { RemoteSelect, RemoteSelectProps } from '../remote-select'; import { RemoteSelect, RemoteSelectProps } from '../remote-select';
import useServiceOptions, { useAssociationFieldContext } from './hooks'; import useServiceOptions, { useAssociationFieldContext } from './hooks';
import { VariablePopupRecordProvider } from '../../../modules/variable/variablesProvider/VariablePopupRecordProvider';
export const AssociationFieldAddNewer = (props) => { export const AssociationFieldAddNewer = (props) => {
const schemaComponentCtxValue = useContext(SchemaComponentContext); const schemaComponentCtxValue = useContext(SchemaComponentContext);
@ -151,7 +151,6 @@ const InternalAssociationSelect = observer(
</div> </div>
); );
}; };
console.log(fieldSchema);
return ( return (
<div key={fieldSchema.name}> <div key={fieldSchema.name}>
<Space.Compact style={{ display: 'flex' }}> <Space.Compact style={{ display: 'flex' }}>

View File

@ -53,6 +53,7 @@ export function useAssociationFieldContext<F extends GeneralField>() {
}; };
} }
// 用于获取关系字段请求数据时所需的一些参数
export default function useServiceOptions(props) { export default function useServiceOptions(props) {
const { action = 'list', service, useOriginalFilter } = props; const { action = 'list', service, useOriginalFilter } = props;
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();

View File

@ -18,7 +18,6 @@ import { getDataSourceHeaders } from '../data-source/utils';
import { useCompile } from '../schema-component'; import { useCompile } from '../schema-component';
import useBuiltInVariables from './hooks/useBuiltinVariables'; import useBuiltInVariables from './hooks/useBuiltinVariables';
import { VariableOption, VariablesContextType } from './types'; import { VariableOption, VariablesContextType } from './types';
import { cacheLazyLoadedValues, getCachedLazyLoadedValues } from './utils/cacheLazyLoadedValues';
import { filterEmptyValues } from './utils/filterEmptyValues'; import { filterEmptyValues } from './utils/filterEmptyValues';
import { getAction } from './utils/getAction'; import { getAction } from './utils/getAction';
import { getPath } from './utils/getPath'; import { getPath } from './utils/getPath';
@ -144,14 +143,13 @@ const VariablesProvider = ({ children, filterVariables }: any) => {
.then((data) => { .then((data) => {
clearRequested(url); clearRequested(url);
const value = data.data.data; const value = data.data.data;
cacheLazyLoadedValues(item, currentVariablePath, value);
return value; return value;
}); });
stashRequested(url, result); stashRequested(url, result);
return result; return result;
} }
} }
return getCachedLazyLoadedValues(item, currentVariablePath) || item?.[key]; return item?.[key];
}); });
current = removeThroughCollectionFields(_.flatten(await Promise.all(result)), associationField); current = removeThroughCollectionFields(_.flatten(await Promise.all(result)), associationField);
} else if ( } else if (
@ -180,17 +178,9 @@ const VariablesProvider = ({ children, filterVariables }: any) => {
} }
const value = data.data.data; const value = data.data.data;
if (!getCachedLazyLoadedValues(current, currentVariablePath)) {
// Cache the API response data to avoid repeated requests
cacheLazyLoadedValues(current, currentVariablePath, value);
}
current = removeThroughCollectionFields(value, associationField); current = removeThroughCollectionFields(value, associationField);
} else { } else {
current = removeThroughCollectionFields( current = removeThroughCollectionFields(getValuesByPath(current, key), associationField);
getCachedLazyLoadedValues(current, currentVariablePath) || getValuesByPath(current, key),
associationField,
);
} }
if (associationField?.target) { if (associationField?.target) {
@ -359,13 +349,8 @@ export default VariablesProvider;
function shouldToRequest(value, variableCtx: Record<string, any>, variablePath: string) { function shouldToRequest(value, variableCtx: Record<string, any>, variablePath: string) {
let result = false; let result = false;
if (getCachedLazyLoadedValues(variableCtx, variablePath)) {
return false;
}
// value may be a reactive object, using untracked to avoid unexpected autorun // value may be a reactive object, using untracked to avoid unexpected autorun
untracked(() => { untracked(() => {
// fix https://nocobase.height.app/T-2502
// Compatible with `xxx to many` and `xxx to one` subform fields and subtable fields // Compatible with `xxx to many` and `xxx to one` subform fields and subtable fields
if (JSON.stringify(value) === '[{}]' || JSON.stringify(value) === '{}') { if (JSON.stringify(value) === '[{}]' || JSON.stringify(value) === '{}') {
result = true; result = true;

View File

@ -1,25 +0,0 @@
/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
const cache = new Map<Record<string, any>, any>();
export const cacheLazyLoadedValues = (variableCtx: Record<string, any>, variablePath: string, value: any) => {
const cachedValue = cache.get(variableCtx);
if (cachedValue) {
cachedValue[variablePath] = value;
} else {
cache.set(variableCtx, { [variablePath]: value });
}
};
export const getCachedLazyLoadedValues = (variableCtx: Record<string, any>, variablePath: string) => {
const cachedValue = cache.get(variableCtx);
return cachedValue?.[variablePath];
};