refactor: persist request handles under tab saveContext
Only the IDs (workspace, provider & request IDs) to restore the handle are stored under `localStorage` and the handle is restored back at runtime.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { refWithControl } from "@vueuse/core"
|
||||
import { Service } from "dioc"
|
||||
import * as E from "fp-ts/Either"
|
||||
import { v4 as uuidV4 } from "uuid"
|
||||
import {
|
||||
ComputedRef,
|
||||
@@ -10,16 +11,23 @@ import {
|
||||
shallowReadonly,
|
||||
watch,
|
||||
} from "vue"
|
||||
import { HoppRESTDocument } from "~/helpers/rest/document"
|
||||
import {
|
||||
HoppTab,
|
||||
PersistableTabState,
|
||||
TabService as TabServiceInterface,
|
||||
} from "."
|
||||
|
||||
import { NewWorkspaceService } from "../new-workspace"
|
||||
import { HandleRef } from "../new-workspace/handle"
|
||||
import { WorkspaceRequest } from "../new-workspace/workspace"
|
||||
|
||||
export abstract class TabService<Doc>
|
||||
extends Service
|
||||
implements TabServiceInterface<Doc>
|
||||
{
|
||||
private workspaceService = this.bind(NewWorkspaceService)
|
||||
|
||||
protected tabMap = reactive(new Map<string, HoppTab<Doc>>())
|
||||
protected tabOrdering = ref<string[]>(["test"])
|
||||
|
||||
@@ -82,15 +90,65 @@ export abstract class TabService<Doc>
|
||||
this.currentTabID.value = tabID
|
||||
}
|
||||
|
||||
public loadTabsFromPersistedState(data: PersistableTabState<Doc>): void {
|
||||
public async loadTabsFromPersistedState(
|
||||
data: PersistableTabState<Doc>
|
||||
): Promise<void> {
|
||||
if (data) {
|
||||
this.tabMap.clear()
|
||||
this.tabOrdering.value = []
|
||||
|
||||
for (const doc of data.orderedDocs) {
|
||||
let requestHandle: HandleRef<WorkspaceRequest> | null = null
|
||||
let resolvedTabDoc = doc.doc
|
||||
|
||||
// TODO: Account for GQL
|
||||
const { saveContext } = doc.doc as HoppRESTDocument
|
||||
|
||||
if (saveContext?.originLocation === "workspace-user-collection") {
|
||||
const { providerID, requestID, workspaceID } = saveContext
|
||||
|
||||
if (!providerID || !workspaceID || !requestID) {
|
||||
continue
|
||||
}
|
||||
|
||||
const workspaceHandleResult =
|
||||
await this.workspaceService.getWorkspaceHandle(
|
||||
providerID!,
|
||||
workspaceID!
|
||||
)
|
||||
|
||||
if (E.isLeft(workspaceHandleResult)) {
|
||||
continue
|
||||
}
|
||||
|
||||
const workspaceHandle = workspaceHandleResult.right
|
||||
|
||||
if (workspaceHandle.value.type === "invalid") {
|
||||
continue
|
||||
}
|
||||
|
||||
const requestHandleResult =
|
||||
await this.workspaceService.getRequestHandle(
|
||||
workspaceHandle,
|
||||
requestID!
|
||||
)
|
||||
|
||||
if (E.isRight(requestHandleResult)) {
|
||||
requestHandle = requestHandleResult.right
|
||||
|
||||
resolvedTabDoc = {
|
||||
...resolvedTabDoc,
|
||||
saveContext: {
|
||||
...saveContext,
|
||||
requestHandle,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.tabMap.set(doc.tabID, {
|
||||
id: doc.tabID,
|
||||
document: doc.doc,
|
||||
document: resolvedTabDoc,
|
||||
})
|
||||
|
||||
this.tabOrdering.value.push(doc.tabID)
|
||||
@@ -99,7 +157,6 @@ export abstract class TabService<Doc>
|
||||
this.setActiveTab(data.lastActiveTabID)
|
||||
}
|
||||
}
|
||||
|
||||
public getActiveTabs(): Readonly<ComputedRef<HoppTab<Doc>[]>> {
|
||||
return shallowReadonly(
|
||||
computed(() => this.tabOrdering.value.map((x) => this.tabMap.get(x)!))
|
||||
@@ -180,13 +237,51 @@ export abstract class TabService<Doc>
|
||||
this.currentTabID.value = tabID
|
||||
}
|
||||
|
||||
private getPersistedDocument(tabDoc: Doc): Doc {
|
||||
const { saveContext } = tabDoc as HoppRESTDocument
|
||||
|
||||
if (saveContext?.originLocation !== "workspace-user-collection") {
|
||||
return tabDoc
|
||||
}
|
||||
|
||||
const { requestHandle } = saveContext
|
||||
|
||||
if (!requestHandle) {
|
||||
return tabDoc
|
||||
}
|
||||
|
||||
if (requestHandle.value.type === "invalid") {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const { requestHandle, ...rest } = saveContext
|
||||
|
||||
// Return the document without the handle
|
||||
return {
|
||||
...tabDoc,
|
||||
saveContext: rest,
|
||||
}
|
||||
}
|
||||
|
||||
const { providerID, workspaceID, requestID } = requestHandle.value.data
|
||||
|
||||
// Return the document without the handle
|
||||
return {
|
||||
...tabDoc,
|
||||
saveContext: {
|
||||
originLocation: "workspace-user-collection",
|
||||
requestID,
|
||||
providerID,
|
||||
workspaceID,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
public persistableTabState = computed<PersistableTabState<Doc>>(() => ({
|
||||
lastActiveTabID: this.currentTabID.value,
|
||||
orderedDocs: this.tabOrdering.value.map((tabID) => {
|
||||
const tab = this.tabMap.get(tabID)! // tab ordering is guaranteed to have value for this key
|
||||
return {
|
||||
tabID: tab.id,
|
||||
doc: tab.document,
|
||||
doc: this.getPersistedDocument(tab.document),
|
||||
}
|
||||
}),
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user