fix not refreshing when re-connecting, cancel load commands when changing guilds

This commit is contained in:
Michael Peters 2022-01-13 22:34:38 -06:00
parent 228352bedb
commit a1bd951812
6 changed files with 19 additions and 20 deletions

View File

@ -1,3 +1,8 @@
import * as electronRemote from '@electron/remote';
const electronConsole = electronRemote.getGlobal('console') as Console;
import Logger from '../../logger/logger';
const LOG = Logger.create(__filename, electronConsole);
// lets you wait until it is uncorked by someone before running
export default class DedupAwaiter<T> {
private promise: Promise<T> | null = null;

View File

@ -6,7 +6,7 @@ const LOG = Logger.create(__filename, electronConsole);
import { Changes, GuildMetadata, Member, Message, Resource } from "../../data-types";
import CombinedGuild from "../../guild-combined";
import { Dispatch, SetStateAction, useCallback, useEffect, useMemo, useState } from 'react';
import { Dispatch, SetStateAction, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { AutoVerifierChangesType } from "../../auto-verifier";
import { Conflictable, Connectable } from "../../guild-types";
import { EventEmitter } from 'tsee';
@ -67,18 +67,6 @@ interface MultipleEventMappingParams<
conflictEventArgsMap: (...args: Arguments<Conflictable[CE]>) => Changes<T>;
sortFunc: (a: T, b: T) => number; // Friendly reminder that v8 uses timsort so this is O(n) for pre-sorted stuff
}
export default class GuildSubscriptions {
}
function useGuildSubscriptionEffect<T>(
@ -88,16 +76,20 @@ function useGuildSubscriptionEffect<T>(
const { guild, onFetch, onFetchError, bindEventsFunc, unbindEventsFunc } = subscriptionParams;
const isMounted = useIsMountedRef();
const guildRef = useRef<CombinedGuild>(guild);
const fetchManagerFunc = useCallback(async () => {
if (!isMounted.current) return;
if (guildRef.current !== guild) return;
try {
const value = await fetchFunc();
if (!isMounted.current) return;
if (guildRef.current !== guild) return; // Don't call onFetch if we changed guilds
onFetch(value);
} catch (e: unknown) {
LOG.error('error fetching for subscription', e);
if (!isMounted.current) return;
if (guildRef.current !== guild) return;
onFetchError(e);
}
}, [ fetchFunc ]);
@ -105,6 +97,7 @@ function useGuildSubscriptionEffect<T>(
useEffect(() => {
// Bind guild events to make sure we have the most up to date information
guild.on('connect', fetchManagerFunc);
guild.on('disconnect', fetchManagerFunc);
bindEventsFunc();
// Fetch the data once
@ -113,6 +106,7 @@ function useGuildSubscriptionEffect<T>(
return () => {
// Unbind the events so that we don't have any memory leaks
guild.off('connect', fetchManagerFunc);
guild.off('disconnect', fetchManagerFunc);
unbindEventsFunc();
}
}, [ fetchManagerFunc ]);

View File

@ -49,11 +49,13 @@ export default class CombinedGuild extends EventEmitter<Connectable & Conflictab
// Connect/Disconnect
this.socketGuild.on('connect', () => {
LOG.info(`g#${this.id} connected`);
diskSocket.unverify();
ramDiskSocket.unverify();
this.emit('connect');
});
this.socketGuild.on('disconnect', async () => {
LOG.info(`g#${this.id} disconnected`);
this.unverify();
diskSocket.unverify();
await personalDB.clearAllMembersStatus(this.id);
this.emit('disconnect');
});
@ -186,7 +188,7 @@ export default class CombinedGuild extends EventEmitter<Connectable & Conflictab
this.emit('conflict-metadata', changesType, oldGuildMeta, newGuildMeta);
});
ramDiskSocket.on('conflict-members', (changesType: AutoVerifierChangesType, changes: Changes<Member>) => {
LOG.info(`g#${this.id} members conflict`);
LOG.info(`g#${this.id} members conflict`); // This should be pretty common since we manually update the status to "unknown" which will cause a conflict
this.emit('conflict-members', changesType, changes);
});
ramDiskSocket.on('conflict-channels', (changesType: AutoVerifierChangesType, changes: Changes<Channel>) => {
@ -256,10 +258,6 @@ export default class CombinedGuild extends EventEmitter<Connectable & Conflictab
return this.socketGuild.verifier.isVerified;
}
private unverify(): void {
this.mainPairVerifier.unverify();
}
public disconnect(): void {
this.socketGuild.disconnect();
}

View File

@ -94,6 +94,7 @@ export default class SocketGuild extends EventEmitter<Connectable> implements As
if (!this.verifier.isVerified) {
throw new Error(`attempted to make query before verified @${endpoint} / [${args.map(arg => LOG.inspect(arg)).join(', ')}]`);
}
LOG.silly(`query@${endpoint} / [${args.map(arg => LOG.inspect(arg)).join(', ')}]`);
return await new Promise((resolve, reject) => {
Util.socketEmitTimeout(this.socket, timeout, endpoint, ...args, (errMsg: string | null, serverData: any) => {
if (errMsg) {

View File

@ -518,7 +518,7 @@ export default class PersonalDB {
) AS "r" ORDER BY "r"."order"
`, { ':guild_id': guildId, ':channel_id': channelId, ':number': number });
if (messages.length === 0) return null;
LOG.debug(`return ${messages.length}/${number} recent messages`);
//LOG.debug(`return ${messages.length}/${number} recent messages`);
return messages.map(dataMessage => Message.fromDBData(dataMessage));
}

View File

@ -28,6 +28,7 @@ export default class SocketVerifier extends EventEmitter<{ 'verified': () => voi
socket.on('disconnect', () => {
this.isVerified = false;
this.memberId = null;
LOG.debug('disconnected and marked as not verified');
});
}