summaryrefslogtreecommitdiff
path: root/packages/frontend/src/scripts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2023-09-11 14:55:18 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2023-09-11 14:55:18 +0900
commit25e030a7074f8e00cfbdaf85e491b9b22886991d (patch)
treedc65176bd88a68f64d907c2bb5a41b3fda1c0a5c /packages/frontend/src/scripts
parentfeat: テスト通知を送信できるようにする (#11810) (diff)
downloadsharkey-25e030a7074f8e00cfbdaf85e491b9b22886991d.tar.gz
sharkey-25e030a7074f8e00cfbdaf85e491b9b22886991d.tar.bz2
sharkey-25e030a7074f8e00cfbdaf85e491b9b22886991d.zip
enhance(frontend): improve some caches
Diffstat (limited to 'packages/frontend/src/scripts')
-rw-r--r--packages/frontend/src/scripts/cache.ts46
-rw-r--r--packages/frontend/src/scripts/get-note-menu.ts2
-rw-r--r--packages/frontend/src/scripts/get-user-menu.ts6
3 files changed, 11 insertions, 43 deletions
diff --git a/packages/frontend/src/scripts/cache.ts b/packages/frontend/src/scripts/cache.ts
index 0dda203ef0..12347cf4b1 100644
--- a/packages/frontend/src/scripts/cache.ts
+++ b/packages/frontend/src/scripts/cache.ts
@@ -9,9 +9,11 @@ export class Cache<T> {
private cachedAt: number | null = null;
public value = ref<T | undefined>();
private lifetime: number;
+ private fetcher: () => Promise<T>;
- constructor(lifetime: Cache<never>['lifetime']) {
+ constructor(lifetime: Cache<never>['lifetime'], fetcher: () => Promise<T>) {
this.lifetime = lifetime;
+ this.fetcher = fetcher;
}
public set(value: T): void {
@@ -35,51 +37,17 @@ export class Cache<T> {
/**
* キャッシュがあればそれを返し、無ければfetcherを呼び出して結果をキャッシュ&返します
- * optional: キャッシュが存在してもvalidatorでfalseを返すとキャッシュ無効扱いにします
*/
- public async fetch(fetcher: () => Promise<T>, validator?: (cachedValue: T) => boolean): Promise<T> {
+ public async fetch(): Promise<T> {
const cachedValue = this.get();
if (cachedValue !== undefined) {
- if (validator) {
- if (validator(cachedValue)) {
- // Cache HIT
- return cachedValue;
- }
- } else {
- // Cache HIT
- return cachedValue;
- }
+ // Cache HIT
+ return cachedValue;
}
// Cache MISS
- const value = await fetcher();
+ const value = await this.fetcher();
this.set(value);
return value;
}
-
- /**
- * キャッシュがあればそれを返し、無ければfetcherを呼び出して結果をキャッシュ&返します
- * optional: キャッシュが存在してもvalidatorでfalseを返すとキャッシュ無効扱いにします
- */
- public async fetchMaybe(fetcher: () => Promise<T | undefined>, validator?: (cachedValue: T) => boolean): Promise<T | undefined> {
- const cachedValue = this.get();
- if (cachedValue !== undefined) {
- if (validator) {
- if (validator(cachedValue)) {
- // Cache HIT
- return cachedValue;
- }
- } else {
- // Cache HIT
- return cachedValue;
- }
- }
-
- // Cache MISS
- const value = await fetcher();
- if (value !== undefined) {
- this.set(value);
- }
- return value;
- }
}
diff --git a/packages/frontend/src/scripts/get-note-menu.ts b/packages/frontend/src/scripts/get-note-menu.ts
index c8b1cb8dfc..5bda993fff 100644
--- a/packages/frontend/src/scripts/get-note-menu.ts
+++ b/packages/frontend/src/scripts/get-note-menu.ts
@@ -32,7 +32,7 @@ export async function getNoteClipMenu(props: {
const appearNote = isRenote ? props.note.renote as Misskey.entities.Note : props.note;
- const clips = await clipsCache.fetch(() => os.api('clips/list'));
+ const clips = await clipsCache.fetch();
return [...clips.map(clip => ({
text: clip.name,
action: () => {
diff --git a/packages/frontend/src/scripts/get-user-menu.ts b/packages/frontend/src/scripts/get-user-menu.ts
index 3b125edfc7..2ab21e6c23 100644
--- a/packages/frontend/src/scripts/get-user-menu.ts
+++ b/packages/frontend/src/scripts/get-user-menu.ts
@@ -170,7 +170,7 @@ export function getUserMenu(user: Misskey.entities.UserDetailed, router: Router
icon: 'ti ti-list',
text: i18n.ts.addToList,
children: async () => {
- const lists = await userListsCache.fetch(() => os.api('users/lists/list'));
+ const lists = await userListsCache.fetch();
return lists.map(list => {
const isListed = ref(list.userIds.includes(user.id));
cleanups.push(watch(isListed, () => {
@@ -203,7 +203,7 @@ export function getUserMenu(user: Misskey.entities.UserDetailed, router: Router
icon: 'ti ti-antenna',
text: i18n.ts.addToAntenna,
children: async () => {
- const antennas = await antennasCache.fetch(() => os.api('antennas/list'));
+ const antennas = await antennasCache.fetch();
const canonical = user.host === null ? `@${user.username}` : `@${user.username}@${toUnicode(user.host)}`;
return antennas.filter((a) => a.src === 'users').map(antenna => ({
text: antenna.name,
@@ -234,7 +234,7 @@ export function getUserMenu(user: Misskey.entities.UserDetailed, router: Router
icon: 'ti ti-badges',
text: i18n.ts.roles,
children: async () => {
- const roles = await rolesCache.fetch(() => os.api('admin/roles/list'));
+ const roles = await rolesCache.fetch();
return roles.filter(r => r.target === 'manual').map(r => ({
text: r.name,