Reliable switch events

This commit is contained in:
Jiiks 2018-03-08 23:40:38 +02:00
parent a746d6a03e
commit 976ea796f8
2 changed files with 65 additions and 29 deletions

View File

@ -15,10 +15,42 @@ import BdModals from './components/bd/BdModals.vue';
import { Events, WebpackModules } from 'modules'; import { Events, WebpackModules } from 'modules';
import { Utils } from 'common'; import { Utils } from 'common';
import AutoManip from './automanip'; import AutoManip from './automanip';
import { remote } from 'electron';
class TempApi {
static get currentGuild() {
try {
const currentGuildId = WebpackModules.getModuleByName('SelectedGuildStore').getGuildId();
return WebpackModules.getModuleByName('GuildStore').getGuild(currentGuildId);
} catch (err) {
return null;
}
}
static get currentChannel() {
try {
const currentChannelId = WebpackModules.getModuleByName('SelectedChannelStore').getChannelId();
return WebpackModules.getModuleByName('ChannelStore').getChannel(currentChannelId);
} catch (err) {
return 0;
}
}
static get currentUserId() {
try {
return WebpackModules.getModuleByName('UserStore').getCurrentUser().id;
} catch (err) {
return 0;
}
}
}
export default class { export default class {
static initUiEvents() { static initUiEvents() {
this.pathCache = {
isDm: null,
server: TempApi.currentGuild,
channel: TempApi.currentChannel
};
this.autoManip = new AutoManip(); this.autoManip = new AutoManip();
const defer = setInterval(() => { const defer = setInterval(() => {
if (!this.profilePopupModule) return; if (!this.profilePopupModule) return;
@ -29,39 +61,42 @@ export default class {
data: { userid } data: { userid }
})); }));
}, 100); }, 100);
// This is temporary
const locationInterval = setInterval(() => { const ehookInterval = setInterval(() => {
try { if (!remote.BrowserWindow.getFocusedWindow()) return;
const path = window.location.pathname.match(/\d+/g); clearInterval(ehookInterval);
if (!path) { remote.BrowserWindow.getFocusedWindow().webContents.on('did-navigate-in-page', (e, url, isMainFrame) => {
this.prevLocation = null; const { currentGuild, currentChannel } = TempApi;
Events.emit('server-switch'); if (!this.pathCache.server) {
Events.emit('server-switch', { 'server': currentGuild, 'channel': currentChannel });
this.pathCache.server = currentGuild;
this.pathCache.channel = currentChannel;
return; return;
} }
const ids = path.reduce((obj, el, index) => { if (!this.pathCache.channel) {
obj[index === 0 ? 'guildId' : 'channelId'] = el; Events.emit('channel-switch', currentChannel);
return obj; this.pathCache.server = currentGuild;
}, this.pathCache.channel = currentChannel;
{}); return;
}
if (currentGuild &&
currentGuild.id &&
this.pathCache.server &&
this.pathCache.server.id !== currentGuild.id) {
Events.emit('server-switch', { 'server': currentGuild, 'channel': currentChannel });
this.pathCache.server = currentGuild;
this.pathCache.channel = currentChannel;
return;
}
if (currentChannel &&
currentChannel.id &&
this.pathCache.channel &&
this.pathCache.channel.id !== currentChannel.id) Events.emit('channel-switch', currentChannel);
if (Object.keys(ids).length === 1) {
ids.isDm = true;
ids.dmId = ids.guildId
}
if (!this.prevLocation) { this.pathCache.server = currentGuild;
Events.emit('server-switch'); this.pathCache.channel = currentChannel;
} else { });
if (this.prevLocation.channelId !== ids.channelId) {
Events.emit('channel-switch');
} else if (this.prevLocation.guildId !== ids.guildId) {
Events.emit('server-switch');
}
}
this.prevLocation = ids;
} catch (err) {
console.log(err);
}
}, 100); }, 100);
} }

View File

@ -10,6 +10,7 @@
class Reflection { class Reflection {
static reactInternalInstance(node) { static reactInternalInstance(node) {
if (!node) return null;
if (!Object.keys(node) || !Object.keys(node).length) return null; if (!Object.keys(node) || !Object.keys(node).length) return null;
const riiKey = Object.keys(node).find(k => k.startsWith('__reactInternalInstance')); const riiKey = Object.keys(node).find(k => k.startsWith('__reactInternalInstance'));
return riiKey ? node[riiKey] : null; return riiKey ? node[riiKey] : null;