perf(app): cleanup connect provider timers
This commit is contained in:
@@ -27,6 +27,17 @@ export function DialogConnectProvider(props: { provider: string }) {
|
|||||||
const globalSDK = useGlobalSDK()
|
const globalSDK = useGlobalSDK()
|
||||||
const platform = usePlatform()
|
const platform = usePlatform()
|
||||||
const language = useLanguage()
|
const language = useLanguage()
|
||||||
|
|
||||||
|
const alive = { value: true }
|
||||||
|
const timer = { current: undefined as ReturnType<typeof setTimeout> | undefined }
|
||||||
|
|
||||||
|
onCleanup(() => {
|
||||||
|
alive.value = false
|
||||||
|
if (timer.current === undefined) return
|
||||||
|
clearTimeout(timer.current)
|
||||||
|
timer.current = undefined
|
||||||
|
})
|
||||||
|
|
||||||
const provider = createMemo(() => globalSync.data.provider.all.find((x) => x.id === props.provider)!)
|
const provider = createMemo(() => globalSync.data.provider.all.find((x) => x.id === props.provider)!)
|
||||||
const methods = createMemo(
|
const methods = createMemo(
|
||||||
() =>
|
() =>
|
||||||
@@ -53,6 +64,11 @@ export function DialogConnectProvider(props: { provider: string }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function selectMethod(index: number) {
|
async function selectMethod(index: number) {
|
||||||
|
if (timer.current !== undefined) {
|
||||||
|
clearTimeout(timer.current)
|
||||||
|
timer.current = undefined
|
||||||
|
}
|
||||||
|
|
||||||
const method = methods()[index]
|
const method = methods()[index]
|
||||||
setStore(
|
setStore(
|
||||||
produce((draft) => {
|
produce((draft) => {
|
||||||
@@ -75,11 +91,15 @@ export function DialogConnectProvider(props: { provider: string }) {
|
|||||||
{ throwOnError: true },
|
{ throwOnError: true },
|
||||||
)
|
)
|
||||||
.then((x) => {
|
.then((x) => {
|
||||||
|
if (!alive.value) return
|
||||||
const elapsed = Date.now() - start
|
const elapsed = Date.now() - start
|
||||||
const delay = 1000 - elapsed
|
const delay = 1000 - elapsed
|
||||||
|
|
||||||
if (delay > 0) {
|
if (delay > 0) {
|
||||||
setTimeout(() => {
|
if (timer.current !== undefined) clearTimeout(timer.current)
|
||||||
|
timer.current = setTimeout(() => {
|
||||||
|
timer.current = undefined
|
||||||
|
if (!alive.value) return
|
||||||
setStore("state", "complete")
|
setStore("state", "complete")
|
||||||
setStore("authorization", x.data!)
|
setStore("authorization", x.data!)
|
||||||
}, delay)
|
}, delay)
|
||||||
@@ -89,6 +109,7 @@ export function DialogConnectProvider(props: { provider: string }) {
|
|||||||
setStore("authorization", x.data!)
|
setStore("authorization", x.data!)
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
|
if (!alive.value) return
|
||||||
setStore("state", "error")
|
setStore("state", "error")
|
||||||
setStore("error", String(e))
|
setStore("error", String(e))
|
||||||
})
|
})
|
||||||
@@ -372,26 +393,33 @@ export function DialogConnectProvider(props: { provider: string }) {
|
|||||||
return instructions
|
return instructions
|
||||||
})
|
})
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(() => {
|
||||||
if (store.authorization?.url) {
|
void (async () => {
|
||||||
platform.openLink(store.authorization.url)
|
if (store.authorization?.url) {
|
||||||
}
|
platform.openLink(store.authorization.url)
|
||||||
const result = await globalSDK.client.provider.oauth
|
}
|
||||||
.callback({
|
|
||||||
providerID: props.provider,
|
const result = await globalSDK.client.provider.oauth
|
||||||
method: store.methodIndex,
|
.callback({
|
||||||
})
|
providerID: props.provider,
|
||||||
.then((value) =>
|
method: store.methodIndex,
|
||||||
value.error ? { ok: false as const, error: value.error } : { ok: true as const },
|
})
|
||||||
)
|
.then((value) =>
|
||||||
.catch((error) => ({ ok: false as const, error }))
|
value.error ? { ok: false as const, error: value.error } : { ok: true as const },
|
||||||
if (!result.ok) {
|
)
|
||||||
const message = result.error instanceof Error ? result.error.message : String(result.error)
|
.catch((error) => ({ ok: false as const, error }))
|
||||||
setStore("state", "error")
|
|
||||||
setStore("error", message)
|
if (!alive.value) return
|
||||||
return
|
|
||||||
}
|
if (!result.ok) {
|
||||||
await complete()
|
const message = result.error instanceof Error ? result.error.message : String(result.error)
|
||||||
|
setStore("state", "error")
|
||||||
|
setStore("error", message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
await complete()
|
||||||
|
})()
|
||||||
})
|
})
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user