From 3eabc5b70614f982477cc60860223efee327163e Mon Sep 17 00:00:00 2001 From: kamecha Date: Thu, 14 Mar 2024 20:52:26 +0900 Subject: [PATCH 1/4] =?UTF-8?q?channel=E7=94=A8=E3=81=AEmap=E3=82=92?= =?UTF-8?q?=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- denops/traqvim/model.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/denops/traqvim/model.ts b/denops/traqvim/model.ts index b33350d..c3e8f9f 100644 --- a/denops/traqvim/model.ts +++ b/denops/traqvim/model.ts @@ -15,6 +15,28 @@ export type channelMessageOptions = { order?: "asc" | "desc"; }; +export const channelMapCache: Map = new Map(); + +const getChannelCache = () => { + return channelMapCache; +}; + +const makeCacheChannel = async () => { + const channelsRes = await api.api.getChannels(); + const channels = channelsRes.data; + channels.public.forEach((channel: traq.Channel) => { + setCacheChannel(channel); + }); +}; + +const setCacheChannel = (channel: traq.Channel) => { + channelMapCache.set(channel.id, channel); +}; + +const getCacheChannel = (channelId: string): traq.Channel | undefined => { + return channelMapCache.get(channelId); +}; + const makeChannelPath = ( channels: traq.Channel[], channel: traq.Channel, From 81f00d892b5c98ca50401b10b886295402c3c566 Mon Sep 17 00:00:00 2001 From: kamecha Date: Thu, 14 Mar 2024 20:54:03 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E3=83=81=E3=83=A3=E3=83=B3=E3=83=8D?= =?UTF-8?q?=E3=83=AB=E3=83=91=E3=82=B9=E3=81=AE=E4=BD=9C=E6=88=90=E3=81=AB?= =?UTF-8?q?map=E3=82=AD=E3=83=A3=E3=83=83=E3=82=B7=E3=83=A5=E3=82=92?= =?UTF-8?q?=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- denops/traqvim/model.ts | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/denops/traqvim/model.ts b/denops/traqvim/model.ts index c3e8f9f..1158be9 100644 --- a/denops/traqvim/model.ts +++ b/denops/traqvim/model.ts @@ -38,31 +38,29 @@ const getCacheChannel = (channelId: string): traq.Channel | undefined => { }; const makeChannelPath = ( - channels: traq.Channel[], - channel: traq.Channel, + channelCache: Map, + channelId: string, ): string => { - const parentChannel = channels.find((c: traq.Channel) => - c.id === channel.parentId - ); - if (!parentChannel) { - return "#" + channel.name; + if (getCacheChannel(channelId) === undefined) { + throw new Error("channel not found"); + } + const parentId = getCacheChannel(channelId)?.parentId; + if (parentId === null || parentId === undefined) { + return "#" + getCacheChannel(channelId)?.name; } else { - return makeChannelPath(channels, parentChannel) + "/" + channel.name; + return ( + makeChannelPath(channelCache, parentId) + "/" + + getCacheChannel(channelId)?.name + ); } }; // channelUUIDに対応するchannelPathを生成する export const channelPath = async (channelUUID: string): Promise => { - const channelsRes = await api.api.getChannels(); - const publicChannels = channelsRes.data.public; - const channel = publicChannels.find((c: traq.Channel) => - c.id === channelUUID - ); - if (!channel) { - return ""; - } else { - return makeChannelPath(publicChannels, channel); + if (getCacheChannel(channelUUID) === undefined) { + await makeCacheChannel(); } + return makeChannelPath(getChannelCache(), channelUUID); }; // 自身のユーザー情報を取得する @@ -160,11 +158,16 @@ export const channelTimeline = async ( export const channelsRecursive = async (): Promise => { const channelsRes = await api.api.getChannels(); const publicChannels = channelsRes.data.public; + publicChannels.forEach((channel: traq.Channel) => { + if (getCacheChannel(channel.id) === undefined) { + setCacheChannel(channel); + } + }); const channelsConverted: Channel[] = publicChannels.map( (channel: traq.Channel) => { return { ...channel, - path: makeChannelPath(publicChannels, channel), + path: makeChannelPath(getChannelCache(), channel.id), }; }, ); From c67126579a9a76f70d71f1e97f4a100efa21026c Mon Sep 17 00:00:00 2001 From: kamecha Date: Thu, 14 Mar 2024 21:05:32 +0900 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20cache=E3=82=92model=E3=81=8B?= =?UTF-8?q?=E3=82=89=E3=81=AE=E3=81=BF=E4=BD=BF=E3=81=88=E3=82=8B=E3=82=88?= =?UTF-8?q?=E3=81=86=E5=88=B6=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- denops/traqvim/model.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/denops/traqvim/model.ts b/denops/traqvim/model.ts index 1158be9..09304e1 100644 --- a/denops/traqvim/model.ts +++ b/denops/traqvim/model.ts @@ -15,7 +15,8 @@ export type channelMessageOptions = { order?: "asc" | "desc"; }; -export const channelMapCache: Map = new Map(); +// TODO: ↓チャンネル周りはclassに分離しても良いかも +const channelMapCache: Map = new Map(); const getChannelCache = () => { return channelMapCache; From 6856031596c5db2f3a0b0004f9f3b2b11a525e23 Mon Sep 17 00:00:00 2001 From: kamecha Date: Thu, 14 Mar 2024 21:13:14 +0900 Subject: [PATCH 4/4] rename --- denops/traqvim/model.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/denops/traqvim/model.ts b/denops/traqvim/model.ts index 09304e1..7478397 100644 --- a/denops/traqvim/model.ts +++ b/denops/traqvim/model.ts @@ -18,11 +18,11 @@ export type channelMessageOptions = { // TODO: ↓チャンネル周りはclassに分離しても良いかも const channelMapCache: Map = new Map(); -const getChannelCache = () => { +const getChannelMapCache = () => { return channelMapCache; }; -const makeCacheChannel = async () => { +const makeChannelMapCache = async () => { const channelsRes = await api.api.getChannels(); const channels = channelsRes.data; channels.public.forEach((channel: traq.Channel) => { @@ -59,9 +59,9 @@ const makeChannelPath = ( // channelUUIDに対応するchannelPathを生成する export const channelPath = async (channelUUID: string): Promise => { if (getCacheChannel(channelUUID) === undefined) { - await makeCacheChannel(); + await makeChannelMapCache(); } - return makeChannelPath(getChannelCache(), channelUUID); + return makeChannelPath(getChannelMapCache(), channelUUID); }; // 自身のユーザー情報を取得する @@ -168,7 +168,7 @@ export const channelsRecursive = async (): Promise => { (channel: traq.Channel) => { return { ...channel, - path: makeChannelPath(getChannelCache(), channel.id), + path: makeChannelPath(getChannelMapCache(), channel.id), }; }, );