Fix state syncing when closing windows
This commit is contained in:
parent
7b1da05aa3
commit
b9cd52285e
1 changed files with 27 additions and 12 deletions
|
@ -1,4 +1,4 @@
|
|||
import { proxyLazyWebpack, findByProps, findByPropsLazy } from "@webpack";
|
||||
import { proxyLazyWebpack } from "@webpack";
|
||||
import { Flux, FluxDispatcher, PopoutActions, PopoutWindowStore, SnowflakeUtils } from "@webpack/common";
|
||||
import DeckPopout from "./components/DeckPopout";
|
||||
import { DataStore } from "@api/index";
|
||||
|
@ -16,7 +16,6 @@ export interface DeckColumn {
|
|||
width: `${number}px` | `${number}%`;
|
||||
}
|
||||
|
||||
// Don't wanna run before Flux and Dispatcher are ready!
|
||||
export const ChannelDeckStore = proxyLazyWebpack(() => {
|
||||
class ChannelDeckStore extends Flux.Store {
|
||||
|
||||
|
@ -47,31 +46,45 @@ export const ChannelDeckStore = proxyLazyWebpack(() => {
|
|||
this._decks.set(deck.id, deck);
|
||||
const { id } = deck;
|
||||
const windowKey = this.windowKeyPrefix + id;
|
||||
if (deck?.open && !PopoutWindowStore.getWindowKeys().includes(windowKey)) {
|
||||
if (deck?.open && !PopoutWindowStore.getWindowKeys().includes(windowKey))
|
||||
PopoutActions.open(
|
||||
windowKey,
|
||||
(key) => <DeckPopout deckId={id} windowKey={key} />, {
|
||||
defaultWidth: 1200,
|
||||
defaultHeight: 960
|
||||
});
|
||||
const deckWindow = this.getDeckWindow(id);
|
||||
debugger;
|
||||
deckWindow.addEventListener("beforeunload", () => {
|
||||
const deck_ = this.getDeck(id);
|
||||
if (deck_?.open) this.setDeck({ ...deck, open: false });
|
||||
});
|
||||
}
|
||||
// We cannot add a beforeunload event to the window here, it will be never fired as Discord has their own handler.
|
||||
if (!deck?.open && PopoutWindowStore.getWindowKeys().includes(windowKey))
|
||||
this.getDeckWindow(id).close();
|
||||
if (noWrite) return;
|
||||
this.emitChange();
|
||||
if (noWrite || !this.loaded) return;
|
||||
this.writeData();
|
||||
this.emitChange();
|
||||
};
|
||||
|
||||
public deleteDeck(deck: ChannelDeck) {
|
||||
// Close window
|
||||
this.setDeck({ ...deck, open: false }, true);
|
||||
this._decks.delete(deck.id);
|
||||
this.writeData();
|
||||
// @ts-ignore
|
||||
delete PopoutWindowStore.getState()[this.windowKeyPrefix + deck.id];
|
||||
// @ts-ignore
|
||||
PopoutWindowStore.persist();
|
||||
this.emitChange();
|
||||
}
|
||||
|
||||
public getDeckWindow(id: string) {
|
||||
return PopoutWindowStore.getWindow(this.windowKeyPrefix + id);
|
||||
}
|
||||
|
||||
// Workaround for Discord's beforeunload event
|
||||
public syncClosedWindows() {
|
||||
this._decks.forEach((deck) => {
|
||||
if (deck.open && !PopoutWindowStore.getWindowKeys().includes(this.windowKeyPrefix + deck.id))
|
||||
this.setDeck({ ...deck, open: false });
|
||||
});
|
||||
}
|
||||
|
||||
public async loadData() {
|
||||
this.loaded = true;
|
||||
const decks: Map<string, ChannelDeck> | undefined = await DataStore.get(this.dataStoreKey);
|
||||
|
@ -96,5 +109,7 @@ export const ChannelDeckStore = proxyLazyWebpack(() => {
|
|||
const store = new ChannelDeckStore(FluxDispatcher, {
|
||||
});
|
||||
|
||||
PopoutWindowStore.addChangeListener(() => store.syncClosedWindows());
|
||||
|
||||
return store;
|
||||
});;
|
||||
|
|
Loading…
Add table
Reference in a new issue