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 { Flux, FluxDispatcher, PopoutActions, PopoutWindowStore, SnowflakeUtils } from "@webpack/common";
|
||||||
import DeckPopout from "./components/DeckPopout";
|
import DeckPopout from "./components/DeckPopout";
|
||||||
import { DataStore } from "@api/index";
|
import { DataStore } from "@api/index";
|
||||||
|
@ -16,7 +16,6 @@ export interface DeckColumn {
|
||||||
width: `${number}px` | `${number}%`;
|
width: `${number}px` | `${number}%`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't wanna run before Flux and Dispatcher are ready!
|
|
||||||
export const ChannelDeckStore = proxyLazyWebpack(() => {
|
export const ChannelDeckStore = proxyLazyWebpack(() => {
|
||||||
class ChannelDeckStore extends Flux.Store {
|
class ChannelDeckStore extends Flux.Store {
|
||||||
|
|
||||||
|
@ -47,31 +46,45 @@ export const ChannelDeckStore = proxyLazyWebpack(() => {
|
||||||
this._decks.set(deck.id, deck);
|
this._decks.set(deck.id, deck);
|
||||||
const { id } = deck;
|
const { id } = deck;
|
||||||
const windowKey = this.windowKeyPrefix + id;
|
const windowKey = this.windowKeyPrefix + id;
|
||||||
if (deck?.open && !PopoutWindowStore.getWindowKeys().includes(windowKey)) {
|
if (deck?.open && !PopoutWindowStore.getWindowKeys().includes(windowKey))
|
||||||
PopoutActions.open(
|
PopoutActions.open(
|
||||||
windowKey,
|
windowKey,
|
||||||
(key) => <DeckPopout deckId={id} windowKey={key} />, {
|
(key) => <DeckPopout deckId={id} windowKey={key} />, {
|
||||||
defaultWidth: 1200,
|
defaultWidth: 1200,
|
||||||
defaultHeight: 960
|
defaultHeight: 960
|
||||||
});
|
});
|
||||||
const deckWindow = this.getDeckWindow(id);
|
// We cannot add a beforeunload event to the window here, it will be never fired as Discord has their own handler.
|
||||||
debugger;
|
|
||||||
deckWindow.addEventListener("beforeunload", () => {
|
|
||||||
const deck_ = this.getDeck(id);
|
|
||||||
if (deck_?.open) this.setDeck({ ...deck, open: false });
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (!deck?.open && PopoutWindowStore.getWindowKeys().includes(windowKey))
|
if (!deck?.open && PopoutWindowStore.getWindowKeys().includes(windowKey))
|
||||||
this.getDeckWindow(id).close();
|
this.getDeckWindow(id).close();
|
||||||
if (noWrite) return;
|
if (noWrite || !this.loaded) return;
|
||||||
this.emitChange();
|
|
||||||
this.writeData();
|
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) {
|
public getDeckWindow(id: string) {
|
||||||
return PopoutWindowStore.getWindow(this.windowKeyPrefix + id);
|
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() {
|
public async loadData() {
|
||||||
this.loaded = true;
|
this.loaded = true;
|
||||||
const decks: Map<string, ChannelDeck> | undefined = await DataStore.get(this.dataStoreKey);
|
const decks: Map<string, ChannelDeck> | undefined = await DataStore.get(this.dataStoreKey);
|
||||||
|
@ -96,5 +109,7 @@ export const ChannelDeckStore = proxyLazyWebpack(() => {
|
||||||
const store = new ChannelDeckStore(FluxDispatcher, {
|
const store = new ChannelDeckStore(FluxDispatcher, {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
PopoutWindowStore.addChangeListener(() => store.syncClosedWindows());
|
||||||
|
|
||||||
return store;
|
return store;
|
||||||
});;
|
});;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue