fix: getInstances refactor for API rate limiting

This commit is contained in:
Ben Allfree 2023-10-18 00:42:47 -07:00
parent 0d6506012a
commit 14f8395b91
4 changed files with 26 additions and 30 deletions

View File

@ -4,13 +4,9 @@
import MediaQuery from '$components/MediaQuery.svelte'
import { DOCS_URL } from '$src/env'
import { handleLogoutAndRedirect } from '$util/database'
import { getInstances } from '$util/getInstances'
import { globalInstancesStore } from '$util/stores'
import { values } from '@s-libs/micro-dash'
// This will query the database for all instances and then update the global state
getInstances()
const linkClasses =
'font-medium text-xl text-base-content btn btn-ghost capitalize justify-start'
const subLinkClasses =

View File

@ -7,7 +7,10 @@
import '../app.css'
import '../services'
import { getInstances } from '$util/getInstances'
import { isUserLoggedIn } from '$util/stores'
getInstances()
</script>
<Meta />

View File

@ -2,18 +2,15 @@
import { page } from '$app/stores'
import AuthStateGuard from '$components/helpers/AuthStateGuard.svelte'
import { getSingleInstance } from '$util/getInstances'
import { createCleanupManager } from '@pockethost/common'
import { onDestroy } from 'svelte'
import { assertTruthy } from '@pockethost/common'
import { instance } from './store'
const cm = createCleanupManager()
// Run anytime the page params changes
$: {
getSingleInstance($page.params.instanceId)
const { instanceId } = $page.params
assertTruthy(instanceId)
getSingleInstance(instanceId)
}
onDestroy(() => cm.shutdown())
</script>
<AuthStateGuard>

View File

@ -1,7 +1,7 @@
import { browser } from '$app/environment'
import { client } from '$src/pocketbase'
import { instance } from '$src/routes/app/instances/[instanceId]/store'
import { globalInstancesStore } from '$util/stores'
import { globalInstancesStore, isUserLoggedIn } from '$util/stores'
import {
LoggerService,
assertExists,
@ -13,32 +13,32 @@ import { onDestroy, onMount } from 'svelte'
export const getInstances = async () => {
const { error } = LoggerService()
onMount(() => {
if (browser) {
;(async () => {
const { getAllInstancesById } = client()
const cm = createCleanupManager()
onMount(async () => {
const unsub = isUserLoggedIn.subscribe(async (isLoggedIn) => {
if (!isLoggedIn) return
const { getAllInstancesById } = client()
const instances = await getAllInstancesById()
const instances = await getAllInstancesById()
globalInstancesStore.set(instances)
globalInstancesStore.set(instances)
client()
.client.collection('instances')
.subscribe<InstanceFields>('*', (data) => {
globalInstancesStore.update((instances) => {
instances[data.record.id] = data.record
return instances
})
const unsub = await client()
.client.collection('instances')
.subscribe<InstanceFields>('*', (data) => {
globalInstancesStore.update((instances) => {
instances[data.record.id] = data.record
return instances
})
})().catch(error)
}
})
cm.add(unsub)
})
cm.add(unsub)
})
// Stop listening to the db if this component unmounts
onDestroy(() => {
if (browser) {
client().client.collection('instances').unsubscribe('*').catch(error)
}
cm.shutdown().catch(console.error)
})
}