From 27d0a7c4374b5ab89c2699441e7d4b26caa5c475 Mon Sep 17 00:00:00 2001 From: Nivedin <53208152+nivedin@users.noreply.github.com> Date: Tue, 23 Jan 2024 22:13:57 +0530 Subject: [PATCH] refactor: persist running requests while switching tabs (#3742) --- .../src/components/http/Request.vue | 24 ++++++++++++------- .../src/helpers/RequestRunner.ts | 3 +++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/hoppscotch-common/src/components/http/Request.vue b/packages/hoppscotch-common/src/components/http/Request.vue index 69c3b7ba0..74a584fc3 100644 --- a/packages/hoppscotch-common/src/components/http/Request.vue +++ b/packages/hoppscotch-common/src/components/http/Request.vue @@ -237,7 +237,7 @@ import { useReadonlyStream, useStreamSubscriber } from "@composables/stream" import { useToast } from "@composables/toast" import { useVModel } from "@vueuse/core" import * as E from "fp-ts/Either" -import { Ref, computed, onBeforeUnmount, ref } from "vue" +import { Ref, computed, ref, onUnmounted } from "vue" import { defineActionHandler, invokeAction } from "~/helpers/actions" import { runMutation } from "~/helpers/backend/GQLClient" import { UpdateRequestDocument } from "~/helpers/backend/graphql" @@ -322,6 +322,10 @@ const userHistories = computed(() => { return history.value.map((history) => history.request.endpoint).slice(0, 10) }) +const inspectionService = useService(InspectionService) + +const tabs = useService(RESTTabService) + const newSendRequest = async () => { if (newEndpoint.value === "" || /^\s+$/.test(newEndpoint.value)) { toast.error(`${t("empty.endpoint")}`) @@ -422,6 +426,17 @@ function isCURL(curl: string) { return curl.includes("curl ") } +const currentTabID = tabs.currentTabID.value + +onUnmounted(() => { + //check if current tab id exist in the current tab id lists + const isCurrentTabRemoved = !tabs + .getActiveTabs() + .value.some((tab) => tab.id === currentTabID) + + if (isCurrentTabRemoved) cancelRequest() +}) + const cancelRequest = () => { loading.value = false requestCancelFunc.value?.() @@ -553,10 +568,6 @@ const saveRequest = () => { const request = ref(null) -onBeforeUnmount(() => { - if (loading.value) cancelRequest() -}) - defineActionHandler("request.send-cancel", () => { if (!loading.value) newSendRequest() else cancelRequest() @@ -607,8 +618,5 @@ const isCustomMethod = computed(() => { const COLUMN_LAYOUT = useSetting("COLUMN_LAYOUT") -const inspectionService = useService(InspectionService) - -const tabs = useService(RESTTabService) const tabResults = inspectionService.getResultViewFor(tabs.currentTabID.value) diff --git a/packages/hoppscotch-common/src/helpers/RequestRunner.ts b/packages/hoppscotch-common/src/helpers/RequestRunner.ts index 7e1c473a8..a653226f6 100644 --- a/packages/hoppscotch-common/src/helpers/RequestRunner.ts +++ b/packages/hoppscotch-common/src/helpers/RequestRunner.ts @@ -154,6 +154,9 @@ export function runRESTRequest$( ) if (E.isRight(runResult)) { + // set the response in the tab so that multiple tabs can run request simultaneously + tab.value.document.response = res + tab.value.document.testResults = translateToSandboxTestResults( runResult.right )