From e60ecce1bbc0d0b40cdc1caa5d3eceb02db4eddd Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Tue, 8 Aug 2023 15:48:58 +0300 Subject: [PATCH] fix selected channel saving and loading --- frontend/src/gateway.js | 11 ++++--- frontend/src/stores.js | 70 ++++++++++++++--------------------------- 2 files changed, 31 insertions(+), 50 deletions(-) diff --git a/frontend/src/gateway.js b/frontend/src/gateway.js index 34a07ee..a2e9cd7 100644 --- a/frontend/src/gateway.js +++ b/frontend/src/gateway.js @@ -194,10 +194,10 @@ export default { }, subscribe(event, handler) { if (!this.handlers.get(event)) { - this.handlers.set(event, new Set()); + this.handlers.set(event, []); } - this.handlers.get(event).add(handler); + this.handlers.get(event).push(handler); return handler; // can later be used for unsubscribe() }, unsubscribe(event, handler) { @@ -205,9 +205,12 @@ export default { if (!eventHandlers) return; - eventHandlers.delete(handler); + const index = eventHandlers.indexOf(handler); + if (index !== -1) { + eventHandlers.splice(index, 1); + } - if (eventHandlers.size < 1) { + if (eventHandlers.length < 1) { this.handlers.delete(event); } }, diff --git a/frontend/src/stores.js b/frontend/src/stores.js index a5f96ed..0fe0b97 100644 --- a/frontend/src/stores.js +++ b/frontend/src/stores.js @@ -102,8 +102,6 @@ class Store { this.value = this._applyPipes(this.value); - if (!this._handlers.size) return; - storeLog(`[Update] (${this.name}) Will queue ${this._handlers.size} handlers`, "value:", this.value, "handlers:", this._handlers); const isRootNode = storeCallbackQueue.length === 0; @@ -705,7 +703,24 @@ class SelectedChannelStore extends Store { this.communityIdToSelectedChannel = new Map(); + const applySavedMap = () => { + if (!gateway.channels) return; + + this.communityIdToSelectedChannel.clear(); + + const savedMap = getItem("state:selectedChannels"); + for (const [communityId, channelId] of Object.entries(savedMap)) { + const channel = gateway.channels.find(c => c.id === channelId); + if (channel) { + this.communityIdToSelectedChannel.set(parseInt(communityId), channel); + } + } + + this.updateSavedMap(); + } + filteredChannelsStore.subscribe((channels) => { + applySavedMap(); let channel = this.communityIdToSelectedChannel.get(selectedCommunity.value.id); if (!channel && channels.length) { channel = channels[0]; @@ -714,17 +729,8 @@ class SelectedChannelStore extends Store { this.updated(); }); - gateway.subscribe(GatewayEventType.Ready, ({ channels }) => { - this.communityIdToSelectedChannel.clear(); - const savedMap = getItem("state:selectedChannels"); - for (const [communityId, channelId] of Object.entries(savedMap)) { - const channel = channels.find(c => c.id === channelId); - if (channel) { - this.communityIdToSelectedChannel.set(parseInt(communityId), channel); - } - } - this.updateSavedMap(); - }); + gateway.subscribe(GatewayEventType.Ready, () => applySavedMap()); + gateway.subscribe(GatewayEventType.CommunityDelete, ({ id }) => { if (this.communityIdToSelectedChannel.delete(id) && this.value && this.value.id === id) { this.value = noneChannel; @@ -821,16 +827,16 @@ class SelectedCommunityStore extends Store { class FilteredChannelsStore extends Store { constructor() { super([], "FilteredChannelsStore"); - channels.on(() => this.update()); + channelsStore.on(() => this.update()); selectedCommunity.on(() => this.update()); this.update(); } update() { if (selectedCommunity.value.id === -1) { - this.value = channels.value.filter(n => n.community_id === null); + this.value = channelsStore.value.filter(n => n.community_id === null); } else { - this.value = channels.value.filter(n => n.community_id === selectedCommunity.value.id); + this.value = channelsStore.value.filter(n => n.community_id === selectedCommunity.value.id); } this.updated(); } @@ -838,7 +844,7 @@ class FilteredChannelsStore extends Store { export const selectedCommunity = new SelectedCommunityStore(); -export const channels = new ChannelsStore(); +export const channelsStore = new ChannelsStore(); export const filteredChannelsStore = new FilteredChannelsStore(); export const selectedChannel = new SelectedChannelStore(); export const showSidebar = new Store(true, "showSidebar"); @@ -862,33 +868,6 @@ export const totalUnreadsStore = new Store(0, "TotalUnreadsStore"); export const statusBarStore = new Store(null, "statusBarStore"); export const setMessageInputEvent = new Store(null, "event:setMessageInput"); -export const sendMessageAction = createAction("sendMessageAction", async ({channelId, content}) => { - if (content.trim() === "" || !userInfoStore.value) - return; - - // optimistically add message to store - const optimisticMessageId = Math.floor(Math.random() * 999999); - const optimisticMessage = { - id: optimisticMessageId, - content: content, - channel_id: channelId, - author_id: userInfoStore.value.id, - author_username: userInfoStore.value.username, - created_at: Date.now().toString(), - _isPending: true - }; - const messagesStoreForChannel = messagesStoreProvider.getStore(channelId); - messagesStoreForChannel.addMessage(optimisticMessage); - - const res = await remoteSignal(methods.createChannelMessage, channelId, content, optimisticMessageId, null); - - if (!responseOk(res)) { - messagesStoreForChannel.deleteMessage({ - id: optimisticMessageId - }); - overlayStore.toast(`Couldn't send message: ${getMessageFromResponse(res)}`); - } -}); export const allStores = { selectedChannel, @@ -898,7 +877,7 @@ export const allStores = { showChannelView, theme, doAnimations, - channels, + channels: channelsStore, gatewayStatus, messagesStoreProvider, userInfoStore, @@ -908,7 +887,6 @@ export const allStores = { unreadStore, pluginStore, setMessageInputEvent, - sendMessageAction, }; unreadStore.subscribe(() => {