refactor: handle based updates for affected requests post collection deletion

This commit is contained in:
jamesgeorge007
2024-05-14 23:29:17 +05:30
parent 6032cbb17b
commit 648cc8f5bd
2 changed files with 100 additions and 47 deletions

View File

@@ -64,10 +64,7 @@ import {
} from "~/services/new-workspace/workspace" } from "~/services/new-workspace/workspace"
import { getAffectedIndexes } from "~/helpers/collection/affectedIndex" import { getAffectedIndexes } from "~/helpers/collection/affectedIndex"
import { import { getFoldersByPath } from "~/helpers/collection/collection"
getFoldersByPath,
resolveSaveContextOnCollectionReorder,
} from "~/helpers/collection/collection"
import { getRequestsByPath } from "~/helpers/collection/request" import { getRequestsByPath } from "~/helpers/collection/request"
import { initializeDownloadFile } from "~/helpers/import-export/export" import { initializeDownloadFile } from "~/helpers/import-export/export"
import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties" import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties"
@@ -318,70 +315,126 @@ export class PersonalWorkspaceProviderService
return Promise.resolve(E.left("INVALID_COLLECTION_HANDLE" as const)) return Promise.resolve(E.left("INVALID_COLLECTION_HANDLE" as const))
} }
const { collectionID } = collectionHandleRef.value.data const { collectionID: removedCollectionID } = collectionHandleRef.value.data
const isRootCollection = collectionID.split("/").length === 1 const isRootCollection = this.isAlreadyInRoot(removedCollectionID)
const collectionIndex = parseInt(collectionID)
const parentCollectionID = this.isAlreadyInRoot(removedCollectionID)
? ""
: removedCollectionID.split("/").slice(0, -1).join("/")
const parentCollectionSizeBeforeRemoval = isRootCollection
? this.restCollectionState.value.state.length
: getFoldersByPath(
this.restCollectionState.value.state,
parentCollectionID
).length
const collectionIndexPos = isRootCollection
? parseInt(removedCollectionID)
: this.pathToLastIndex(removedCollectionID)
const affectedCollectionIDRange =
parentCollectionSizeBeforeRemoval - 1 - collectionIndexPos
if (isRootCollection) { if (isRootCollection) {
const collectionToRemove = navigateToFolderWithIndexPath( const collectionToRemove = navigateToFolderWithIndexPath(
restCollectionStore.value.state, restCollectionStore.value.state,
[collectionIndex] [collectionIndexPos]
) )
removeRESTCollection( removeRESTCollection(
collectionIndex, collectionIndexPos,
collectionToRemove ? collectionToRemove.id : undefined collectionToRemove ? collectionToRemove.id : undefined
) )
} else { } else {
const folderToRemove = path const folderToRemove = path
? navigateToFolderWithIndexPath( ? navigateToFolderWithIndexPath(
restCollectionStore.value.state, restCollectionStore.value.state,
collectionID.split("/").map((id) => parseInt(id)) removedCollectionID.split("/").map((id) => parseInt(id))
) )
: undefined : undefined
removeRESTFolder( removeRESTFolder(
collectionID, removedCollectionID,
folderToRemove ? folderToRemove.id : undefined folderToRemove ? folderToRemove.id : undefined
) )
} }
for (const [idx, handle] of this.issuedHandles.entries()) { this.issuedHandles.forEach((handle) => {
if (handle.value.type === "invalid") continue if (
handle.value.type === "invalid" ||
!("requestID" in handle.value.data)
) {
return
}
if ("requestID" in handle.value.data) { if (handle.value.data.requestID.startsWith(removedCollectionID)) {
if (handle.value.data.requestID.startsWith(collectionID)) { handle.value = {
// @ts-expect-error - We're deleting the data to invalidate the handle type: "invalid",
delete this.issuedHandles[idx].value.data reason: "REQUEST_INVALIDATED",
this.issuedHandles[idx].value.type = "invalid"
// @ts-expect-error - Setting the handle invalidation reason
this.issuedHandles[idx].value.reason = "REQUEST_INVALIDATED"
} }
} }
} })
if (isRootCollection) { Array.from({
resolveSaveContextOnCollectionReorder({ length: affectedCollectionIDRange,
lastIndex: collectionIndex, }).forEach((_, idx) => {
newIndex: -1, const resolvedCollectionIndexPos = collectionIndexPos + idx + 1
folderPath: "", // root folder
length: restCollectionStore.value.state.length, const affectedCollectionID = parentCollectionID
? `${parentCollectionID}/${resolvedCollectionIndexPos}`
: resolvedCollectionIndexPos.toString()
this.issuedHandles.forEach((handle) => {
if (
handle.value.type === "invalid" ||
!("requestID" in handle.value.data)
) {
return
}
// For each affected collection, we'll have to iterate over the `issuedHandles`
// The collection index position which has to be reduced by `1` is computed by matching against the string representation of the affected collection ID
// This is done to account for nested collections under each affected collection
if (handle.value.data.requestID.startsWith(affectedCollectionID)) {
const { collectionID, requestID } = handle.value.data
// Compute the length of the string representation of the affected collection ID
const affectedCollectionIDStrLen =
affectedCollectionID.split("/").length
// Obtain a subset of the collection ID till the affected collection ID string length
const collectionIDSubset = collectionID
.split("/")
.slice(0, affectedCollectionIDStrLen)
.join("/")
// Compute the index position of the subset collection ID
const collectionIDSubsetIndexPos =
this.pathToLastIndex(collectionIDSubset)
// Replace the affected collection ID with `1` reduced from the index position
const newAffectedCollectionID = parentCollectionID
? `${parentCollectionID}/${collectionIDSubsetIndexPos - 1}`
: (collectionIDSubsetIndexPos - 1).toString()
const newCollectionID = collectionID.replace(
affectedCollectionID,
newAffectedCollectionID
)
const newRequestID = requestID.replace(
affectedCollectionID,
newAffectedCollectionID
)
handle.value.data.collectionID = newCollectionID
handle.value.data.requestID = newRequestID
}
}) })
} else { })
const parentCollectionID = collectionID.split("/").slice(0, -1).join("/") // remove last folder to get parent folder
resolveSaveContextOnCollectionReorder({
lastIndex: this.pathToLastIndex(collectionID),
newIndex: -1,
folderPath: parentCollectionID,
length: getFoldersByPath(
restCollectionStore.value.state,
parentCollectionID
).length,
})
}
return Promise.resolve(E.right(undefined)) return Promise.resolve(E.right(undefined))
} }
@@ -531,10 +584,10 @@ export class PersonalWorkspaceProviderService
}) })
if (deletedRequestHandle) { if (deletedRequestHandle) {
deletedRequestHandle.value.type = "invalid" deletedRequestHandle.value = {
type: "invalid",
// @ts-expect-error - Setting the handle invalidation reason reason: "REQUEST_INVALIDATED",
deletedRequestHandle.value.reason = "REQUEST_INVALIDATED" }
} }
affectedRequestIDs.forEach((requestID) => { affectedRequestIDs.forEach((requestID) => {

View File

@@ -62,7 +62,7 @@ export class RESTTabService extends TabService<HoppRESTDocument> {
const requestHandle = tab.document.saveContext.requestHandle const requestHandle = tab.document.saveContext.requestHandle
if (!ctx.requestHandle || !requestHandle) { if (!ctx.requestHandle || !requestHandle) {
return null continue
} }
const tabRequestHandleRef = requestHandle.get() const tabRequestHandleRef = requestHandle.get()
@@ -72,7 +72,7 @@ export class RESTTabService extends TabService<HoppRESTDocument> {
requestHandleRef.value.type === "invalid" || requestHandleRef.value.type === "invalid" ||
tabRequestHandleRef.value.type === "invalid" tabRequestHandleRef.value.type === "invalid"
) { ) {
return null continue
} }
if ( if (