From c8a7e6e7e7cc29d87ed1d1fbe7af43f37e41b1c0 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 26 Dec 2022 09:34:30 +0100 Subject: [PATCH] fix: use lazy pagination (#563) --- app.vue | 3 +-- composables/paginator.ts | 9 ++++++++- composables/users.ts | 11 +++++++++++ middleware/auth.ts | 15 ++++++++++++++- 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/app.vue b/app.vue index 4d44ed25..20dd1ed6 100644 --- a/app.vue +++ b/app.vue @@ -13,8 +13,7 @@ const { params } = useRoute() diff --git a/composables/paginator.ts b/composables/paginator.ts index 617830e8..13663e70 100644 --- a/composables/paginator.ts +++ b/composables/paginator.ts @@ -3,7 +3,7 @@ import { useDeactivated } from './lifecycle' import type { PaginatorState } from '~/types' export function usePaginator(paginator: Paginator, stream?: WsEvents, eventType: 'notification' | 'update' = 'update') { - const state = ref('idle') + const state = ref(isMastoInitialised.value ? 'idle' : 'loading') const items = ref([]) const nextItems = ref([]) const prevItems = ref([]) @@ -74,6 +74,13 @@ export function usePaginator(paginator: Paginator, stream?: WsEvent bound.update() }, 1000) + if (!isMastoInitialised.value) { + watchOnce(isMastoInitialised, () => { + state.value = 'idle' + loadNext() + }) + } + watch( () => [isInScreen, state], () => { diff --git a/composables/users.ts b/composables/users.ts index 73c4f3f3..dbf2f7b9 100644 --- a/composables/users.ts +++ b/composables/users.ts @@ -294,6 +294,17 @@ export const createMasto = () => { if (!api.value) { return new Proxy({}, { get(_, subkey) { + if (typeof subkey === 'string' && subkey.startsWith('iterate')) { + return (...args: any[]) => { + let paginator: any + function next() { + paginator = paginator || (api.value as any)?.[key][subkey](...args) + return paginator.next() + } + return { next } + } + } + return (...args: any[]) => apiPromise.value?.then((r: any) => r[key][subkey](...args)) }, }) diff --git a/middleware/auth.ts b/middleware/auth.ts index 4c251eb7..c5a09ef6 100644 --- a/middleware/auth.ts +++ b/middleware/auth.ts @@ -1,7 +1,20 @@ export default defineNuxtRouteMiddleware((to) => { if (process.server) return - if (!currentUser.value && to.path !== '/signin/callback') + if (to.path === '/signin/callback') + return + + if (!isMastoInitialised.value) { + watchOnce(isMastoInitialised, () => { + if (!currentUser.value) + return navigateTo(`/${currentServer.value}/public`) + if (to.path === '/') + return navigateTo('/home') + }) + return + } + + if (!currentUser.value) return navigateTo(`/${currentServer.value}/public`) if (to.path === '/') return navigateTo('/home')