fix: sync popup fire multiple times
This commit is contained in:
40
packages/hoppscotch-common/src/newstore/syncing.ts
Normal file
40
packages/hoppscotch-common/src/newstore/syncing.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import { distinctUntilChanged, pluck } from "rxjs"
|
||||||
|
import DispatchingStore, { defineDispatchers } from "./DispatchingStore"
|
||||||
|
|
||||||
|
type SyncState = {
|
||||||
|
initialSync: boolean
|
||||||
|
sync: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
type CurrentSyncingState = {
|
||||||
|
currentSyncingItem: SyncState
|
||||||
|
}
|
||||||
|
|
||||||
|
const initialState: CurrentSyncingState = {
|
||||||
|
currentSyncingItem: {
|
||||||
|
initialSync: false,
|
||||||
|
sync: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
const dispatchers = defineDispatchers({
|
||||||
|
changeCurrentSyncStatus(_, { syncItem }: { syncItem: SyncState }) {
|
||||||
|
return {
|
||||||
|
currentSyncingItem: syncItem,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
export const currentSyncStore = new DispatchingStore(initialState, dispatchers)
|
||||||
|
|
||||||
|
export const currentSyncingStatus$ = currentSyncStore.subject$.pipe(
|
||||||
|
pluck("currentSyncingItem"),
|
||||||
|
distinctUntilChanged()
|
||||||
|
)
|
||||||
|
|
||||||
|
export function changeCurrentSyncStatus(syncItem: SyncState) {
|
||||||
|
currentSyncStore.dispatch({
|
||||||
|
dispatcher: "changeCurrentSyncStatus",
|
||||||
|
payload: { syncItem },
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -123,6 +123,11 @@ import { useToast } from "~/composables/toast"
|
|||||||
import { PersistableRESTTabState } from "~/helpers/rest/tab"
|
import { PersistableRESTTabState } from "~/helpers/rest/tab"
|
||||||
import { watchDebounced } from "@vueuse/core"
|
import { watchDebounced } from "@vueuse/core"
|
||||||
import { oauthRedirect } from "~/helpers/oauth"
|
import { oauthRedirect } from "~/helpers/oauth"
|
||||||
|
import { useReadonlyStream } from "~/composables/stream"
|
||||||
|
import {
|
||||||
|
changeCurrentSyncStatus,
|
||||||
|
currentSyncingStatus$,
|
||||||
|
} from "~/newstore/syncing"
|
||||||
|
|
||||||
const savingRequest = ref(false)
|
const savingRequest = ref(false)
|
||||||
const confirmingCloseForTabID = ref<string | null>(null)
|
const confirmingCloseForTabID = ref<string | null>(null)
|
||||||
@@ -134,7 +139,10 @@ const toast = useToast()
|
|||||||
|
|
||||||
const tabs = getActiveTabs()
|
const tabs = getActiveTabs()
|
||||||
|
|
||||||
const confirmSync = ref(false)
|
const confirmSync = useReadonlyStream(currentSyncingStatus$, {
|
||||||
|
initialSync: false,
|
||||||
|
sync: true,
|
||||||
|
})
|
||||||
const tabStateForSync = ref<PersistableRESTTabState | null>(null)
|
const tabStateForSync = ref<PersistableRESTTabState | null>(null)
|
||||||
|
|
||||||
function bindRequestToURLParams() {
|
function bindRequestToURLParams() {
|
||||||
@@ -229,28 +237,39 @@ const onSaveModalClose = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
watch(confirmSync, (newValue) => {
|
watch(
|
||||||
if (newValue) {
|
() => confirmSync.value.initialSync,
|
||||||
toast.show(t("confirm.sync"), {
|
(newValue) => {
|
||||||
duration: 0,
|
if (newValue) {
|
||||||
action: [
|
toast.show(t("confirm.sync"), {
|
||||||
{
|
duration: 0,
|
||||||
text: `${t("action.yes")}`,
|
action: [
|
||||||
onClick: (_, toastObject) => {
|
{
|
||||||
syncTabState()
|
text: `${t("action.yes")}`,
|
||||||
toastObject.goAway(0)
|
onClick: (_, toastObject) => {
|
||||||
|
syncTabState()
|
||||||
|
changeCurrentSyncStatus({
|
||||||
|
initialSync: true,
|
||||||
|
sync: true,
|
||||||
|
})
|
||||||
|
toastObject.goAway(0)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
{
|
||||||
{
|
text: `${t("action.no")}`,
|
||||||
text: `${t("action.no")}`,
|
onClick: (_, toastObject) => {
|
||||||
onClick: (_, toastObject) => {
|
changeCurrentSyncStatus({
|
||||||
toastObject.goAway(0)
|
initialSync: true,
|
||||||
|
sync: false,
|
||||||
|
})
|
||||||
|
toastObject.goAway(0)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
],
|
||||||
],
|
})
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
})
|
)
|
||||||
|
|
||||||
const syncTabState = () => {
|
const syncTabState = () => {
|
||||||
if (tabStateForSync.value) loadTabsFromPersistedState(tabStateForSync.value)
|
if (tabStateForSync.value) loadTabsFromPersistedState(tabStateForSync.value)
|
||||||
@@ -305,9 +324,12 @@ function setupTabStateSync() {
|
|||||||
const tabStateFromSync =
|
const tabStateFromSync =
|
||||||
await platform.sync.tabState.loadTabStateFromSync()
|
await platform.sync.tabState.loadTabStateFromSync()
|
||||||
|
|
||||||
if (tabStateFromSync) {
|
if (tabStateFromSync && !confirmSync.value.initialSync) {
|
||||||
tabStateForSync.value = tabStateFromSync
|
tabStateForSync.value = tabStateFromSync
|
||||||
confirmSync.value = true
|
changeCurrentSyncStatus({
|
||||||
|
initialSync: true,
|
||||||
|
sync: false,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user