Skip to content

Commit d9844e1

Browse files
perf(frontend): optimistic fetching for custom emojis on boot
Co-authored-by: しろは <[email protected]>
1 parent c15ab55 commit d9844e1

3 files changed

Lines changed: 32 additions & 7 deletions

File tree

‎packages/frontend/src/boot/common.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { getAccountFromId } from '@/utility/get-account-from-id.js';
2626
import { deckStore } from '@/ui/deck/deck-store.js';
2727
import { analytics, initAnalytics } from '@/analytics.js';
2828
import { miLocalStorage } from '@/local-storage.js';
29-
import { fetchCustomEmojis } from '@/custom-emojis.js';
29+
import { initCustomEmojiCache } from '@/custom-emojis.js';
3030
import { prefer } from '@/preferences.js';
3131
import { $i } from '@/i.js';
3232
import { launchPlugins } from '@/plugin.js';
@@ -242,9 +242,7 @@ export async function common(createVue: () => Promise<App<Element>>) {
242242
}
243243
//#endregion
244244

245-
try {
246-
await fetchCustomEmojis();
247-
} catch (err) { /* empty */ }
245+
await initCustomEmojiCache();
248246

249247
// analytics
250248
fetchInstanceMetaPromise.then(async () => {

‎packages/frontend/src/components/MkReactionsViewer.vue‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { getEmojiNameFromReaction, isLocalCustomEmojiReaction } from '@@/js/emoj
3737
import XReaction from '@/components/MkReactionsViewer.reaction.vue';
3838
import { $i } from '@/i.js';
3939
import { prefer } from '@/preferences.js';
40-
import { customEmojisMap } from '@/custom-emojis.js';
40+
import { customEmojis, customEmojisMap } from '@/custom-emojis.js';
4141
import { DI } from '@/di.js';
4242
4343
const props = withDefaults(defineProps<{
@@ -82,7 +82,9 @@ function canReact(reaction: string) {
8282
: isSupportedEmoji(reaction);
8383
}
8484
85-
watch([() => props.reactions, () => props.maxNumber], ([newSource, maxNumber]) => {
85+
let lastCanReact: Map<string, boolean> | null = null;
86+
87+
function rebuildReactions(newSource: Misskey.entities.Note['reactions'], maxNumber: number) {
8688
let newReactions: [string, number][] = [];
8789
hasMoreReactions.value = Object.keys(newSource).length > maxNumber;
8890
@@ -101,6 +103,7 @@ watch([() => props.reactions, () => props.maxNumber], ([newSource, maxNumber]) =
101103
for (const [emoji] of sorted) {
102104
canReactCache.set(emoji, canReact(emoji));
103105
}
106+
lastCanReact = canReactCache;
104107
sorted.sort(([emojiA, countA], [emojiB, countB]) => {
105108
const canReactA = canReactCache.get(emojiA)!;
106109
const canReactB = canReactCache.get(emojiB)!;
@@ -124,7 +127,19 @@ watch([() => props.reactions, () => props.maxNumber], ([newSource, maxNumber]) =
124127
}
125128
126129
_reactions.value = newReactions;
130+
}
131+
132+
watch([() => props.reactions, () => props.maxNumber], ([newSource, maxNumber]) => {
133+
rebuildReactions(newSource, maxNumber);
127134
}, { immediate: true, deep: true });
135+
136+
watch(customEmojis, () => {
137+
if (!prefer.s.showAvailableReactionsFirstInNote || lastCanReact == null) return;
138+
const sortedWith = lastCanReact;
139+
if (!Object.keys(props.reactions).some(r => sortedWith.has(r) && sortedWith.get(r) !== canReact(r))) return;
140+
_reactions.value = [];
141+
rebuildReactions(props.reactions, props.maxNumber);
142+
});
128143
</script>
129144

130145
<style lang="scss" module>

‎packages/frontend/src/custom-emojis.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: AGPL-3.0-only
44
*/
55

6-
import { shallowRef, computed, markRaw, watch } from 'vue';
6+
import { shallowRef, computed, markRaw, watch, triggerRef } from 'vue';
77
import * as Misskey from 'misskey-js';
88
import { misskeyApi, misskeyApiGet } from '@/utility/misskey-api.js';
99
import { get, set } from '@/utility/idb-proxy.js';
@@ -43,6 +43,18 @@ export function removeCustomEmojis(emojis: Misskey.entities.EmojiSimple[]) {
4343
set('emojis', customEmojis.value);
4444
}
4545

46+
export async function initCustomEmojiCache() {
47+
const fetchingPromise = fetchCustomEmojis()
48+
.then(() => {
49+
triggerRef(customEmojis);
50+
})
51+
.catch(() => { /* nop */ });
52+
53+
if (customEmojis.value.length === 0) {
54+
await fetchingPromise;
55+
}
56+
}
57+
4658
export async function fetchCustomEmojis(force = false) {
4759
const now = Date.now();
4860

0 commit comments

Comments
 (0)