fix selected channel saving and loading

This commit is contained in:
hippoz 2023-08-08 15:48:58 +03:00
parent 0f6a88ac36
commit e60ecce1bb
Signed by: hippoz
GPG key ID: 56C4E02A85F2FBED
2 changed files with 31 additions and 50 deletions

View file

@ -194,10 +194,10 @@ export default {
}, },
subscribe(event, handler) { subscribe(event, handler) {
if (!this.handlers.get(event)) { 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() return handler; // can later be used for unsubscribe()
}, },
unsubscribe(event, handler) { unsubscribe(event, handler) {
@ -205,9 +205,12 @@ export default {
if (!eventHandlers) if (!eventHandlers)
return; 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); this.handlers.delete(event);
} }
}, },

View file

@ -102,8 +102,6 @@ class Store {
this.value = this._applyPipes(this.value); 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); storeLog(`[Update] (${this.name}) Will queue ${this._handlers.size} handlers`, "value:", this.value, "handlers:", this._handlers);
const isRootNode = storeCallbackQueue.length === 0; const isRootNode = storeCallbackQueue.length === 0;
@ -705,7 +703,24 @@ class SelectedChannelStore extends Store {
this.communityIdToSelectedChannel = new Map(); 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) => { filteredChannelsStore.subscribe((channels) => {
applySavedMap();
let channel = this.communityIdToSelectedChannel.get(selectedCommunity.value.id); let channel = this.communityIdToSelectedChannel.get(selectedCommunity.value.id);
if (!channel && channels.length) { if (!channel && channels.length) {
channel = channels[0]; channel = channels[0];
@ -714,17 +729,8 @@ class SelectedChannelStore extends Store {
this.updated(); this.updated();
}); });
gateway.subscribe(GatewayEventType.Ready, ({ channels }) => { gateway.subscribe(GatewayEventType.Ready, () => applySavedMap());
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.CommunityDelete, ({ id }) => { gateway.subscribe(GatewayEventType.CommunityDelete, ({ id }) => {
if (this.communityIdToSelectedChannel.delete(id) && this.value && this.value.id === id) { if (this.communityIdToSelectedChannel.delete(id) && this.value && this.value.id === id) {
this.value = noneChannel; this.value = noneChannel;
@ -821,16 +827,16 @@ class SelectedCommunityStore extends Store {
class FilteredChannelsStore extends Store { class FilteredChannelsStore extends Store {
constructor() { constructor() {
super([], "FilteredChannelsStore"); super([], "FilteredChannelsStore");
channels.on(() => this.update()); channelsStore.on(() => this.update());
selectedCommunity.on(() => this.update()); selectedCommunity.on(() => this.update());
this.update(); this.update();
} }
update() { update() {
if (selectedCommunity.value.id === -1) { 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 { } 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(); this.updated();
} }
@ -838,7 +844,7 @@ class FilteredChannelsStore extends Store {
export const selectedCommunity = new SelectedCommunityStore(); export const selectedCommunity = new SelectedCommunityStore();
export const channels = new ChannelsStore(); export const channelsStore = new ChannelsStore();
export const filteredChannelsStore = new FilteredChannelsStore(); export const filteredChannelsStore = new FilteredChannelsStore();
export const selectedChannel = new SelectedChannelStore(); export const selectedChannel = new SelectedChannelStore();
export const showSidebar = new Store(true, "showSidebar"); 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 statusBarStore = new Store(null, "statusBarStore");
export const setMessageInputEvent = new Store(null, "event:setMessageInput"); 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 = { export const allStores = {
selectedChannel, selectedChannel,
@ -898,7 +877,7 @@ export const allStores = {
showChannelView, showChannelView,
theme, theme,
doAnimations, doAnimations,
channels, channels: channelsStore,
gatewayStatus, gatewayStatus,
messagesStoreProvider, messagesStoreProvider,
userInfoStore, userInfoStore,
@ -908,7 +887,6 @@ export const allStores = {
unreadStore, unreadStore,
pluginStore, pluginStore,
setMessageInputEvent, setMessageInputEvent,
sendMessageAction,
}; };
unreadStore.subscribe(() => { unreadStore.subscribe(() => {