From e50a6d6f2b8a80d1a96ac65504c5fff59d5698f6 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Fri, 29 Nov 2024 21:43:41 +0100 Subject: [PATCH] fix: :bug: Fix infinite refresh of account cache --- composables/CacheRefresh.ts | 106 +++++++++++++++++++----------------- 1 file changed, 55 insertions(+), 51 deletions(-) diff --git a/composables/CacheRefresh.ts b/composables/CacheRefresh.ts index 7b23cff..94d0188 100644 --- a/composables/CacheRefresh.ts +++ b/composables/CacheRefresh.ts @@ -7,62 +7,66 @@ export const useCacheRefresh = (client: MaybeRef) => { } // Refresh custom emojis and instance data and me on every reload - watchEffect(async () => { - console.info("Refreshing emoji, instance and account cache"); - if (identity.value) { - toValue(client) - ?.verifyAccountCredentials() - .then((res) => { - if (identity.value) { - identity.value.account = res.data; - } - }) - .catch((err) => { - const code = err.response.status; + watch( + [identity, client], + async () => { + console.info("Refreshing emoji, instance and account cache"); + if (identity.value) { + toValue(client) + ?.verifyAccountCredentials() + .then((res) => { + if (identity.value) { + identity.value.account = res.data; + } + }) + .catch((err) => { + const code = err.response.status; - if (code === 401) { - // Reset tokenData - identity.value = null; - useEvent("notification:new", { - type: "error", - title: "Your session has expired", - description: - "You have been logged out. Please log in again.", - }); - } - }); + if (code === 401) { + // Reset tokenData + identity.value = null; + useEvent("notification:new", { + type: "error", + title: "Your session has expired", + description: + "You have been logged out. Please log in again.", + }); + } + }); + + toValue(client) + ?.getInstanceCustomEmojis() + .then((res) => { + if (identity.value) { + identity.value.emojis = res.data; + } + }); + + toValue(client) + ?.getAccountRoles(identity.value.account.id) + .then((res) => { + const roles = res.data; + + // Get all permissions and deduplicate + const permissions = roles + .flatMap((r) => r.permissions) + .filter((p, i, arr) => arr.indexOf(p) === i); + + if (identity.value) { + identity.value.permissions = + permissions as unknown as RolePermission[]; + } + }); + } toValue(client) - ?.getInstanceCustomEmojis() + ?.getInstance() .then((res) => { if (identity.value) { - identity.value.emojis = res.data; + identity.value.instance = res.data; } }); - - toValue(client) - ?.getAccountRoles(identity.value.account.id) - .then((res) => { - const roles = res.data; - - // Get all permissions and deduplicate - const permissions = roles - .flatMap((r) => r.permissions) - .filter((p, i, arr) => arr.indexOf(p) === i); - - if (identity.value) { - identity.value.permissions = - permissions as unknown as RolePermission[]; - } - }); - } - - toValue(client) - ?.getInstance() - .then((res) => { - if (identity.value) { - identity.value.instance = res.data; - } - }); - }); + }, + { flush: "sync" }, + ); };