From 292d8b53e47bad310d3ea0a959311ab89e33db84 Mon Sep 17 00:00:00 2001 From: Jacob Fletcher Date: Sun, 6 Oct 2024 22:53:44 -0400 Subject: [PATCH] thrads abort controller to onchange --- .../next/src/views/Edit/Default/index.tsx | 25 +++++++++++++++++-- test/versions/e2e.spec.ts | 5 ++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/packages/next/src/views/Edit/Default/index.tsx b/packages/next/src/views/Edit/Default/index.tsx index 3ea8b32d925..2af5b3fbad9 100644 --- a/packages/next/src/views/Edit/Default/index.tsx +++ b/packages/next/src/views/Edit/Default/index.tsx @@ -79,8 +79,6 @@ export const DefaultEditView: React.FC = () => { const { refreshCookieAsync, user } = useAuth() - // const { getFormState } = useServerFunctions() - const { config, config: { @@ -97,6 +95,8 @@ export const DefaultEditView: React.FC = () => { const { resetUploadEdits } = useUploadEdits() const { getFormState } = useServerFunctions() + const abortControllerRef = useRef(new AbortController()) + const locale = params.get('locale') const collectionConfig = getEntityConfig({ collectionSlug }) as ClientCollectionConfig @@ -233,6 +233,17 @@ export const DefaultEditView: React.FC = () => { const onChange: FormProps['onChange'][0] = useCallback( async ({ formState: prevFormState }) => { + if (abortControllerRef.current) { + try { + abortControllerRef.current.abort() + } catch (e) { + // swallow error + } + } + + const abortController = new AbortController() + abortControllerRef.current = abortController + const currentTime = Date.now() const timeSinceLastUpdate = currentTime - lastUpdateTime @@ -253,6 +264,7 @@ export const DefaultEditView: React.FC = () => { operation, returnLockStatus: isLockingEnabled ? true : false, schemaPath, + signal: abortController.signal, updateLastEdited, }) @@ -282,6 +294,7 @@ export const DefaultEditView: React.FC = () => { } } } + return state }, [ @@ -304,6 +317,14 @@ export const DefaultEditView: React.FC = () => { // Clean up when the component unmounts or when the document is unlocked useEffect(() => { return () => { + if (abortControllerRef.current) { + try { + abortControllerRef.current.abort() + } catch (e) { + // swallow error + } + } + if (!isLockingEnabled) { return } diff --git a/test/versions/e2e.spec.ts b/test/versions/e2e.spec.ts index 6b3bc8ca048..b05dcc515c3 100644 --- a/test/versions/e2e.spec.ts +++ b/test/versions/e2e.spec.ts @@ -116,6 +116,11 @@ describe('versions', () => { }) beforeEach(async () => { + /* await throttleTest({ + page, + context, + delay: 'Slow 4G', + }) */ await reInitializeDB({ serverURL, snapshotKey: 'versionsTest',