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 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 =

View File

@ -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 />

View File

@ -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>

View File

@ -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,16 +13,17 @@ 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) => {
if (!isLoggedIn) return
const { getAllInstancesById } = client() 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) => {
@ -30,15 +31,14 @@ export const getInstances = async () => {
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)
}
}) })
} }