From 57c8722c3c0ec49100dc9e1c23e6cc08e2365ef4 Mon Sep 17 00:00:00 2001
From: Zeke Zhang <958414905@qq.com>
Date: Mon, 24 Mar 2025 20:38:44 +0800
Subject: [PATCH] fix: remove caching logic (#6530)
---
.../association-field/AssociationSelect.tsx | 9 +++----
.../antd/association-field/hooks.tsx | 1 +
.../src/variables/VariablesProvider.tsx | 19 ++------------
.../variables/utils/cacheLazyLoadedValues.ts | 25 -------------------
4 files changed, 7 insertions(+), 47 deletions(-)
delete mode 100644 packages/core/client/src/variables/utils/cacheLazyLoadedValues.ts
diff --git a/packages/core/client/src/schema-component/antd/association-field/AssociationSelect.tsx b/packages/core/client/src/schema-component/antd/association-field/AssociationSelect.tsx
index 57debf2402..708a2cff30 100644
--- a/packages/core/client/src/schema-component/antd/association-field/AssociationSelect.tsx
+++ b/packages/core/client/src/schema-component/antd/association-field/AssociationSelect.tsx
@@ -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(
);
};
- console.log(fieldSchema);
return (
diff --git a/packages/core/client/src/schema-component/antd/association-field/hooks.tsx b/packages/core/client/src/schema-component/antd/association-field/hooks.tsx
index f44f67906f..2c1b2e4183 100644
--- a/packages/core/client/src/schema-component/antd/association-field/hooks.tsx
+++ b/packages/core/client/src/schema-component/antd/association-field/hooks.tsx
@@ -53,6 +53,7 @@ export function useAssociationFieldContext() {
};
}
+// 用于获取关系字段请求数据时所需的一些参数
export default function useServiceOptions(props) {
const { action = 'list', service, useOriginalFilter } = props;
const fieldSchema = useFieldSchema();
diff --git a/packages/core/client/src/variables/VariablesProvider.tsx b/packages/core/client/src/variables/VariablesProvider.tsx
index 85c14a4a18..c7ffc83da5 100644
--- a/packages/core/client/src/variables/VariablesProvider.tsx
+++ b/packages/core/client/src/variables/VariablesProvider.tsx
@@ -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, 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;
diff --git a/packages/core/client/src/variables/utils/cacheLazyLoadedValues.ts b/packages/core/client/src/variables/utils/cacheLazyLoadedValues.ts
deleted file mode 100644
index 9d07c2959d..0000000000
--- a/packages/core/client/src/variables/utils/cacheLazyLoadedValues.ts
+++ /dev/null
@@ -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, any>();
-
-export const cacheLazyLoadedValues = (variableCtx: Record, 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, variablePath: string) => {
- const cachedValue = cache.get(variableCtx);
- return cachedValue?.[variablePath];
-};