mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-05 05:29:26 +08:00
fix: remove caching logic (#6530)
This commit is contained in:
parent
2fefcfbcf2
commit
57c8722c3c
@ -14,22 +14,22 @@ import { uid } from '@formily/shared';
|
||||
import { Space, message } from 'antd';
|
||||
import { isEqual } from 'lodash';
|
||||
import { isFunction } from 'mathjs';
|
||||
import React, { useEffect, useState, useContext } from 'react';
|
||||
import React, { useContext, useEffect, useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import {
|
||||
ClearCollectionFieldContext,
|
||||
NocoBaseRecursionField,
|
||||
RecordProvider,
|
||||
SchemaComponentContext,
|
||||
useAPIClient,
|
||||
useCollectionRecordData,
|
||||
SchemaComponentContext,
|
||||
} from '../../../';
|
||||
import { Action } from '../action';
|
||||
import { VariablePopupRecordProvider } from '../../../modules/variable/variablesProvider/VariablePopupRecordProvider';
|
||||
import { isVariable } from '../../../variables/utils/isVariable';
|
||||
import { getInnermostKeyAndValue } from '../../common/utils/uitls';
|
||||
import { Action } from '../action';
|
||||
import { RemoteSelect, RemoteSelectProps } from '../remote-select';
|
||||
import useServiceOptions, { useAssociationFieldContext } from './hooks';
|
||||
import { VariablePopupRecordProvider } from '../../../modules/variable/variablesProvider/VariablePopupRecordProvider';
|
||||
|
||||
export const AssociationFieldAddNewer = (props) => {
|
||||
const schemaComponentCtxValue = useContext(SchemaComponentContext);
|
||||
@ -151,7 +151,6 @@ const InternalAssociationSelect = observer(
|
||||
</div>
|
||||
);
|
||||
};
|
||||
console.log(fieldSchema);
|
||||
return (
|
||||
<div key={fieldSchema.name}>
|
||||
<Space.Compact style={{ display: 'flex' }}>
|
||||
|
@ -53,6 +53,7 @@ export function useAssociationFieldContext<F extends GeneralField>() {
|
||||
};
|
||||
}
|
||||
|
||||
// 用于获取关系字段请求数据时所需的一些参数
|
||||
export default function useServiceOptions(props) {
|
||||
const { action = 'list', service, useOriginalFilter } = props;
|
||||
const fieldSchema = useFieldSchema();
|
||||
|
@ -18,7 +18,6 @@ import { getDataSourceHeaders } from '../data-source/utils';
|
||||
import { useCompile } from '../schema-component';
|
||||
import useBuiltInVariables from './hooks/useBuiltinVariables';
|
||||
import { VariableOption, VariablesContextType } from './types';
|
||||
import { cacheLazyLoadedValues, getCachedLazyLoadedValues } from './utils/cacheLazyLoadedValues';
|
||||
import { filterEmptyValues } from './utils/filterEmptyValues';
|
||||
import { getAction } from './utils/getAction';
|
||||
import { getPath } from './utils/getPath';
|
||||
@ -144,14 +143,13 @@ const VariablesProvider = ({ children, filterVariables }: any) => {
|
||||
.then((data) => {
|
||||
clearRequested(url);
|
||||
const value = data.data.data;
|
||||
cacheLazyLoadedValues(item, currentVariablePath, value);
|
||||
return value;
|
||||
});
|
||||
stashRequested(url, result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return getCachedLazyLoadedValues(item, currentVariablePath) || item?.[key];
|
||||
return item?.[key];
|
||||
});
|
||||
current = removeThroughCollectionFields(_.flatten(await Promise.all(result)), associationField);
|
||||
} else if (
|
||||
@ -180,17 +178,9 @@ const VariablesProvider = ({ children, filterVariables }: any) => {
|
||||
}
|
||||
|
||||
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);
|
||||
} else {
|
||||
current = removeThroughCollectionFields(
|
||||
getCachedLazyLoadedValues(current, currentVariablePath) || getValuesByPath(current, key),
|
||||
associationField,
|
||||
);
|
||||
current = removeThroughCollectionFields(getValuesByPath(current, key), associationField);
|
||||
}
|
||||
|
||||
if (associationField?.target) {
|
||||
@ -359,13 +349,8 @@ export default VariablesProvider;
|
||||
function shouldToRequest(value, variableCtx: Record<string, any>, variablePath: string) {
|
||||
let result = false;
|
||||
|
||||
if (getCachedLazyLoadedValues(variableCtx, variablePath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// value may be a reactive object, using untracked to avoid unexpected autorun
|
||||
untracked(() => {
|
||||
// fix https://nocobase.height.app/T-2502
|
||||
// Compatible with `xxx to many` and `xxx to one` subform fields and subtable fields
|
||||
if (JSON.stringify(value) === '[{}]' || JSON.stringify(value) === '{}') {
|
||||
result = true;
|
||||
|
@ -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];
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user