feat: desktop app

Co-authored-by: Vivek R <123vivekr@gmail.com>
Co-authored-by: Liyas Thomas <liyascthomas@gmail.com>
This commit is contained in:
Andrew Bastin
2023-11-07 14:01:00 +05:30
parent 4ebf850cb6
commit 16044b5840
134 changed files with 11814 additions and 206 deletions

View File

@@ -0,0 +1,102 @@
import { Observable } from "rxjs"
import DispatchingStore from "@hoppscotch/common/newstore/DispatchingStore"
export type DispatchersOf<T extends DispatchingStore<any, any>> =
T extends DispatchingStore<any, infer U>
? U extends Record<infer D, any>
? D
: never
: never
export type StoreSyncDefinitionOf<T extends DispatchingStore<any, any>> = {
[x in DispatchersOf<T>]?: T extends DispatchingStore<any, infer U>
? U extends Record<x, any>
? U[x] extends (x: any, y: infer Y) => any
? (payload: Y) => void
: never
: never
: never
}
let _isRunningDispatchWithoutSyncing = true
export function runDispatchWithOutSyncing(func: () => void) {
_isRunningDispatchWithoutSyncing = false
func()
_isRunningDispatchWithoutSyncing = true
}
export const getSyncInitFunction = <T extends DispatchingStore<any, any>>(
store: T,
storeSyncDefinition: StoreSyncDefinitionOf<T>,
shouldSyncValue: () => boolean,
shouldSyncObservable?: Observable<boolean>
) => {
let startSubscriptions: () => () => void | undefined
let stopSubscriptions: () => void | undefined
let oldSyncStatus = shouldSyncValue()
// Start and stop the subscriptions according to the sync settings from profile
shouldSyncObservable &&
shouldSyncObservable.subscribe((newSyncStatus) => {
if (oldSyncStatus === true && newSyncStatus === false) {
stopListeningToSubscriptions()
} else if (oldSyncStatus === false && newSyncStatus === true) {
startListeningToSubscriptions()
}
oldSyncStatus = newSyncStatus
})
function startStoreSync() {
store.dispatches$.subscribe((actionParams) => {
// typescript cannot understand that the dispatcher can be the index, so casting to any
if ((storeSyncDefinition as any)[actionParams.dispatcher]) {
const dispatcher = actionParams.dispatcher
const payload = actionParams.payload
const operationMapperFunction = (storeSyncDefinition as any)[dispatcher]
if (
operationMapperFunction &&
_isRunningDispatchWithoutSyncing &&
shouldSyncValue()
) {
operationMapperFunction(payload)
}
}
})
}
function setupSubscriptions(func: () => () => void) {
startSubscriptions = func
}
function startListeningToSubscriptions() {
if (!startSubscriptions) {
console.warn(
"We don't have a function to start subscriptions. Please use `setupSubscriptions` to setup the start function."
)
}
stopSubscriptions = startSubscriptions()
}
function stopListeningToSubscriptions() {
if (!stopSubscriptions) {
console.warn(
"We don't have a function to unsubscribe. make sure you return the unsubscribe function when using setupSubscriptions"
)
}
stopSubscriptions()
}
return {
startStoreSync,
setupSubscriptions,
startListeningToSubscriptions,
stopListeningToSubscriptions,
}
}

View File

@@ -0,0 +1,42 @@
export const createMapper = <
LocalIDType extends string | number,
BackendIDType extends string | number
>() => {
const backendIDByLocalIDMap = new Map<
LocalIDType,
BackendIDType | undefined
>()
const localIDByBackendIDMap = new Map<
BackendIDType,
LocalIDType | undefined
>()
return {
addEntry(localIdentifier: LocalIDType, backendIdentifier: BackendIDType) {
backendIDByLocalIDMap.set(localIdentifier, backendIdentifier)
localIDByBackendIDMap.set(backendIdentifier, localIdentifier)
},
getValue() {
return backendIDByLocalIDMap
},
getBackendIDByLocalID(localIdentifier: LocalIDType) {
return backendIDByLocalIDMap.get(localIdentifier)
},
getLocalIDByBackendID(backendId: BackendIDType) {
return localIDByBackendIDMap.get(backendId)
},
removeEntry(backendId?: BackendIDType, index?: LocalIDType) {
if (backendId) {
const index = localIDByBackendIDMap.get(backendId)
localIDByBackendIDMap.delete(backendId)
index && backendIDByLocalIDMap.delete(index)
} else if (index) {
const backendId = backendIDByLocalIDMap.get(index)
backendIDByLocalIDMap.delete(index)
backendId && localIDByBackendIDMap.delete(backendId)
}
},
}
}