mirror of
https://github.com/pockethost/pockethost.git
synced 2025-10-14 00:59:25 +00:00
fix: getInstances refactor for API rate limiting
This commit is contained in:
parent
0d6506012a
commit
14f8395b91
@ -4,13 +4,9 @@
|
|||||||
import MediaQuery from '$components/MediaQuery.svelte'
|
import MediaQuery from '$components/MediaQuery.svelte'
|
||||||
import { DOCS_URL } from '$src/env'
|
import { DOCS_URL } from '$src/env'
|
||||||
import { handleLogoutAndRedirect } from '$util/database'
|
import { handleLogoutAndRedirect } from '$util/database'
|
||||||
import { getInstances } from '$util/getInstances'
|
|
||||||
import { globalInstancesStore } from '$util/stores'
|
import { globalInstancesStore } from '$util/stores'
|
||||||
import { values } from '@s-libs/micro-dash'
|
import { values } from '@s-libs/micro-dash'
|
||||||
|
|
||||||
// This will query the database for all instances and then update the global state
|
|
||||||
getInstances()
|
|
||||||
|
|
||||||
const linkClasses =
|
const linkClasses =
|
||||||
'font-medium text-xl text-base-content btn btn-ghost capitalize justify-start'
|
'font-medium text-xl text-base-content btn btn-ghost capitalize justify-start'
|
||||||
const subLinkClasses =
|
const subLinkClasses =
|
||||||
|
@ -7,7 +7,10 @@
|
|||||||
import '../app.css'
|
import '../app.css'
|
||||||
import '../services'
|
import '../services'
|
||||||
|
|
||||||
|
import { getInstances } from '$util/getInstances'
|
||||||
import { isUserLoggedIn } from '$util/stores'
|
import { isUserLoggedIn } from '$util/stores'
|
||||||
|
|
||||||
|
getInstances()
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Meta />
|
<Meta />
|
||||||
|
@ -2,18 +2,15 @@
|
|||||||
import { page } from '$app/stores'
|
import { page } from '$app/stores'
|
||||||
import AuthStateGuard from '$components/helpers/AuthStateGuard.svelte'
|
import AuthStateGuard from '$components/helpers/AuthStateGuard.svelte'
|
||||||
import { getSingleInstance } from '$util/getInstances'
|
import { getSingleInstance } from '$util/getInstances'
|
||||||
import { createCleanupManager } from '@pockethost/common'
|
import { assertTruthy } from '@pockethost/common'
|
||||||
import { onDestroy } from 'svelte'
|
|
||||||
import { instance } from './store'
|
import { instance } from './store'
|
||||||
|
|
||||||
const cm = createCleanupManager()
|
|
||||||
|
|
||||||
// Run anytime the page params changes
|
// Run anytime the page params changes
|
||||||
$: {
|
$: {
|
||||||
getSingleInstance($page.params.instanceId)
|
const { instanceId } = $page.params
|
||||||
|
assertTruthy(instanceId)
|
||||||
|
getSingleInstance(instanceId)
|
||||||
}
|
}
|
||||||
|
|
||||||
onDestroy(() => cm.shutdown())
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<AuthStateGuard>
|
<AuthStateGuard>
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { browser } from '$app/environment'
|
import { browser } from '$app/environment'
|
||||||
import { client } from '$src/pocketbase'
|
import { client } from '$src/pocketbase'
|
||||||
import { instance } from '$src/routes/app/instances/[instanceId]/store'
|
import { instance } from '$src/routes/app/instances/[instanceId]/store'
|
||||||
import { globalInstancesStore } from '$util/stores'
|
import { globalInstancesStore, isUserLoggedIn } from '$util/stores'
|
||||||
import {
|
import {
|
||||||
LoggerService,
|
LoggerService,
|
||||||
assertExists,
|
assertExists,
|
||||||
@ -13,32 +13,32 @@ import { onDestroy, onMount } from 'svelte'
|
|||||||
export const getInstances = async () => {
|
export const getInstances = async () => {
|
||||||
const { error } = LoggerService()
|
const { error } = LoggerService()
|
||||||
|
|
||||||
onMount(() => {
|
const cm = createCleanupManager()
|
||||||
if (browser) {
|
onMount(async () => {
|
||||||
;(async () => {
|
const unsub = isUserLoggedIn.subscribe(async (isLoggedIn) => {
|
||||||
const { getAllInstancesById } = client()
|
if (!isLoggedIn) return
|
||||||
|
const { getAllInstancesById } = client()
|
||||||
|
|
||||||
const instances = await getAllInstancesById()
|
const instances = await getAllInstancesById()
|
||||||
|
|
||||||
globalInstancesStore.set(instances)
|
globalInstancesStore.set(instances)
|
||||||
|
|
||||||
client()
|
const unsub = await client()
|
||||||
.client.collection('instances')
|
.client.collection('instances')
|
||||||
.subscribe<InstanceFields>('*', (data) => {
|
.subscribe<InstanceFields>('*', (data) => {
|
||||||
globalInstancesStore.update((instances) => {
|
globalInstancesStore.update((instances) => {
|
||||||
instances[data.record.id] = data.record
|
instances[data.record.id] = data.record
|
||||||
return instances
|
return instances
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})().catch(error)
|
})
|
||||||
}
|
cm.add(unsub)
|
||||||
|
})
|
||||||
|
cm.add(unsub)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Stop listening to the db if this component unmounts
|
// Stop listening to the db if this component unmounts
|
||||||
onDestroy(() => {
|
onDestroy(() => {
|
||||||
if (browser) {
|
cm.shutdown().catch(console.error)
|
||||||
client().client.collection('instances').unsubscribe('*').catch(error)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user