Skip to content

Commit

Permalink
thrads abort controller to onchange
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobsfletch committed Oct 7, 2024
1 parent ca8629e commit 292d8b5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
25 changes: 23 additions & 2 deletions packages/next/src/views/Edit/Default/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ export const DefaultEditView: React.FC = () => {

const { refreshCookieAsync, user } = useAuth()

// const { getFormState } = useServerFunctions()

const {
config,
config: {
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -253,6 +264,7 @@ export const DefaultEditView: React.FC = () => {
operation,
returnLockStatus: isLockingEnabled ? true : false,
schemaPath,
signal: abortController.signal,
updateLastEdited,
})

Expand Down Expand Up @@ -282,6 +294,7 @@ export const DefaultEditView: React.FC = () => {
}
}
}

return state
},
[
Expand All @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions test/versions/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ describe('versions', () => {
})

beforeEach(async () => {
/* await throttleTest({
page,
context,
delay: 'Slow 4G',
}) */
await reInitializeDB({
serverURL,
snapshotKey: 'versionsTest',
Expand Down

0 comments on commit 292d8b5

Please sign in to comment.