fix: fix cancel request issues and stream setup issues
This commit is contained in:
@@ -197,7 +197,7 @@ import {
|
|||||||
} from "~/newstore/RESTSession"
|
} from "~/newstore/RESTSession"
|
||||||
import { getPlatformSpecialKey } from "~/helpers/platformutils"
|
import { getPlatformSpecialKey } from "~/helpers/platformutils"
|
||||||
import { runRESTRequest$ } from "~/helpers/RequestRunner"
|
import { runRESTRequest$ } from "~/helpers/RequestRunner"
|
||||||
import { subscribeToStream, useStream } from "~/helpers/utils/composables"
|
import { useStreamSubscriber, useStream } from "~/helpers/utils/composables"
|
||||||
import { defineActionHandler } from "~/helpers/actions"
|
import { defineActionHandler } from "~/helpers/actions"
|
||||||
import { copyToClipboard } from "~/helpers/utils/clipboard"
|
import { copyToClipboard } from "~/helpers/utils/clipboard"
|
||||||
|
|
||||||
@@ -221,6 +221,7 @@ export default defineComponent({
|
|||||||
app: { i18n },
|
app: { i18n },
|
||||||
} = useContext()
|
} = useContext()
|
||||||
const t = i18n.t.bind(i18n)
|
const t = i18n.t.bind(i18n)
|
||||||
|
const { subscribeToStream } = useStreamSubscriber()
|
||||||
|
|
||||||
const newEndpoint = useStream(restEndpoint$, "", setRESTEndpoint)
|
const newEndpoint = useStream(restEndpoint$, "", setRESTEndpoint)
|
||||||
const newMethod = useStream(restMethod$, "", updateRESTMethod)
|
const newMethod = useStream(restMethod$, "", updateRESTMethod)
|
||||||
@@ -240,7 +241,11 @@ export default defineComponent({
|
|||||||
runRESTRequest$(),
|
runRESTRequest$(),
|
||||||
(responseState) => {
|
(responseState) => {
|
||||||
console.log(responseState)
|
console.log(responseState)
|
||||||
updateRESTResponse(responseState)
|
if (loading.value) {
|
||||||
|
// Check exists because, loading can be set to false
|
||||||
|
// when cancelled
|
||||||
|
updateRESTResponse(responseState)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
() => {
|
() => {
|
||||||
loading.value = false
|
loading.value = false
|
||||||
@@ -251,6 +256,11 @@ export default defineComponent({
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const cancelRequest = () => {
|
||||||
|
loading.value = false
|
||||||
|
updateRESTResponse(null)
|
||||||
|
}
|
||||||
|
|
||||||
const updateMethod = (method: string) => {
|
const updateMethod = (method: string) => {
|
||||||
updateRESTMethod(method)
|
updateRESTMethod(method)
|
||||||
}
|
}
|
||||||
@@ -304,7 +314,10 @@ export default defineComponent({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
defineActionHandler("request.send-cancel", newSendRequest)
|
defineActionHandler("request.send-cancel", () => {
|
||||||
|
if (!loading.value) newSendRequest()
|
||||||
|
else cancelRequest()
|
||||||
|
})
|
||||||
defineActionHandler("request.reset", clearContent)
|
defineActionHandler("request.reset", clearContent)
|
||||||
defineActionHandler("request.copy-link", copyRequest)
|
defineActionHandler("request.copy-link", copyRequest)
|
||||||
defineActionHandler("request.method.next", cycleDownMethod)
|
defineActionHandler("request.method.next", cycleDownMethod)
|
||||||
|
|||||||
@@ -90,33 +90,35 @@ export function pluckRef<T, K extends keyof T>(ref: Ref<T>, key: K): Ref<T[K]> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A composable that listens to the stream and fires update callbacks
|
* A composable that provides the ability to run streams
|
||||||
* but respects the component lifecycle
|
* and subscribe to them and respect the component lifecycle.
|
||||||
*
|
|
||||||
* @param stream The stream to subscribe to
|
|
||||||
* @param next Callback called on value emission
|
|
||||||
* @param error Callback called on stream error
|
|
||||||
* @param complete Callback called on stream completion
|
|
||||||
*/
|
*/
|
||||||
export function subscribeToStream<T>(
|
export function useStreamSubscriber() {
|
||||||
stream: Observable<T>,
|
const subs: Subscription[] = []
|
||||||
next: (value: T) => void,
|
|
||||||
error: (e: any) => void,
|
const runAndSubscribe = <T>(
|
||||||
complete: () => void
|
stream: Observable<T>,
|
||||||
) {
|
next: (value: T) => void,
|
||||||
let sub: Subscription | null = null
|
error: (e: any) => void,
|
||||||
|
complete: () => void
|
||||||
|
) => {
|
||||||
|
const sub = stream.subscribe({
|
||||||
|
next,
|
||||||
|
error,
|
||||||
|
complete: () => {
|
||||||
|
complete()
|
||||||
|
subs.splice(subs.indexOf(sub), 1)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
subs.push(sub)
|
||||||
|
}
|
||||||
|
|
||||||
// Don't perform anymore updates if the component is
|
|
||||||
// gonna unmount
|
|
||||||
onBeforeUnmount(() => {
|
onBeforeUnmount(() => {
|
||||||
if (sub) {
|
subs.forEach((sub) => sub.unsubscribe())
|
||||||
sub.unsubscribe()
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
sub = stream.subscribe({
|
return {
|
||||||
next,
|
subscribeToStream: runAndSubscribe,
|
||||||
error,
|
}
|
||||||
complete,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user