cordis/src/client/webapp/elements/require/guild-subscriptions.ts

343 lines
12 KiB
TypeScript
Raw Normal View History

2021-12-13 04:01:30 +00:00
import * as electronRemote from '@electron/remote';
const electronConsole = electronRemote.getGlobal('console') as Console;
import Logger from '../../../../logger/logger';
const LOG = Logger.create(__filename, electronConsole);
2021-12-13 05:25:23 +00:00
import { Changes, GuildMetadata, Resource } from "../../data-types";
2021-12-13 04:01:30 +00:00
import CombinedGuild from "../../guild-combined";
import React, { DependencyList, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { AutoVerifierChangesType } from "../../auto-verifier";
import { Conflictable, Connectable } from "../../guild-types";
import { EventEmitter } from 'tsee';
import { IDQuery } from '../../auto-verifier-with-args';
2021-12-13 05:25:23 +00:00
import { Token } from '../../data-types';
2021-12-13 04:01:30 +00:00
2021-12-13 05:25:23 +00:00
export type SingleSubscriptionEvents = {
2021-12-13 04:01:30 +00:00
'fetch': () => void;
2021-12-13 05:25:23 +00:00
'updated': () => void;
2021-12-13 04:01:30 +00:00
'conflict': () => void;
'fetch-error': () => void;
}
2021-12-13 05:25:23 +00:00
export type MultipleSubscriptionEvents<T> = {
'fetch': () => void;
'fetch-error': () => void;
'new': (newValues: T[]) => void;
'updated': (updatedValues: T[]) => void;
'removed': (removedValues: T[]) => void;
'conflict': (changes: Changes<T>) => void;
}
2021-12-13 04:01:30 +00:00
interface EffectParams<T> {
guild: CombinedGuild;
onFetch: (value: T | null) => void;
onFetchError: (e: unknown) => void;
bindEventsFunc: () => void;
unbindEventsFunc: () => void;
2021-12-13 04:01:30 +00:00
}
type Arguments<T> = T extends (...args: infer A) => unknown ? A : never;
2021-12-13 05:25:23 +00:00
interface SingleEventMappingParams<T, UE extends keyof Connectable, CE extends keyof Conflictable> {
updatedEventName: UE;
updatedEventArgsMap: (...args: Arguments<Connectable[UE]>) => T;
2021-12-13 04:01:30 +00:00
conflictEventName: CE;
2021-12-13 05:25:23 +00:00
conflictEventArgsMap: (...args: Arguments<Conflictable[CE]>) => T;
}
interface MultipleEventMappingParams<
T,
NE extends keyof Connectable,
UE extends keyof Connectable,
RE extends keyof Connectable,
CE extends keyof Conflictable
> {
newEventName: NE;
newEventArgsMap: (...args: Arguments<Connectable[NE]>) => T[]; // list of new elements
updatedEventName: UE;
updatedEventArgsMap: (...args: Arguments<Connectable[UE]>) => T[]; // list of updated elements
removedEventName: RE;
removedEventArgsMap: (...args: Arguments<Connectable[RE]>) => T[]; // list of removed elements
conflictEventName: CE;
conflictEventArgsMap: (...args: Arguments<Conflictable[CE]>) => Changes<T>;
sortFunc: (a: T, b: T) => number;
2021-12-13 04:01:30 +00:00
}
export default class GuildSubscriptions {
private static useGuildSubscriptionEffect<T>(
isMountedRef: React.MutableRefObject<boolean>,
subscriptionParams: EffectParams<T>,
fetchFunc: (() => Promise<T>) | (() => Promise<T | null>)
2021-12-13 04:01:30 +00:00
) {
const { guild, onFetch, onFetchError, bindEventsFunc, unbindEventsFunc } = subscriptionParams;
2021-12-13 04:01:30 +00:00
const fetchManagerFunc = useMemo(() => {
return async () => {
if (!isMountedRef.current) return;
try {
const value = await fetchFunc();
if (!isMountedRef.current) return;
onFetch(value);
} catch (e: unknown) {
LOG.error('error fetching for subscription', e);
if (!isMountedRef.current) return;
onFetchError(e);
}
2021-12-13 04:01:30 +00:00
}
}, [ fetchFunc ]);
2021-12-13 04:01:30 +00:00
useEffect(() => {
isMountedRef.current = true;
2021-12-13 04:01:30 +00:00
// Bind guild events to make sure we have the most up to date information
guild.on('connect', fetchManagerFunc);
bindEventsFunc();
2021-12-13 04:01:30 +00:00
// Fetch the data once
fetchManagerFunc();
return () => {
isMountedRef.current = false;
2021-12-13 04:01:30 +00:00
// Unbind the events so that we don't have any memory leaks
guild.off('connect', fetchManagerFunc);
unbindEventsFunc();
2021-12-13 04:01:30 +00:00
}
}, [ fetchManagerFunc ]);
}
private static useSingleGuildSubscription<T, UE extends keyof Connectable, CE extends keyof Conflictable>(
2021-12-13 05:25:23 +00:00
guild: CombinedGuild,
eventMappingParams: SingleEventMappingParams<T, UE, CE>,
fetchFunc: (() => Promise<T>) | (() => Promise<T | null>)
2021-12-13 05:25:23 +00:00
): [value: T | null, fetchError: unknown | null, events: EventEmitter<SingleSubscriptionEvents>] {
const { updatedEventName, updatedEventArgsMap, conflictEventName, conflictEventArgsMap } = eventMappingParams;
const isMountedRef = useRef<boolean>(false);
2021-12-13 04:01:30 +00:00
const [ fetchError, setFetchError ] = useState<unknown | null>(null);
const [ value, setValue ] = useState<T | null>(null);
2021-12-13 05:25:23 +00:00
const events = useMemo(() => new EventEmitter<SingleSubscriptionEvents>(), []);
2021-12-13 04:01:30 +00:00
const onFetch = useCallback((fetchValue: T | null) => {
setValue(fetchValue);
setFetchError(null);
events.emit('fetch');
}, []);
const onFetchError = useCallback((e: unknown) => {
setFetchError(e);
setValue(null);
events.emit('fetch-error');
}, []);
2021-12-13 05:25:23 +00:00
const onUpdated = useCallback((updateValue: T) => {
2021-12-13 04:01:30 +00:00
setValue(updateValue);
2021-12-13 05:25:23 +00:00
events.emit('updated');
2021-12-13 04:01:30 +00:00
}, []);
const onConflict = useCallback((conflictValue: T) => {
setValue(conflictValue);
events.emit('conflict');
}, []);
// I think the typed EventEmitter class isn't ready for this level of insane type safety
// otherwise, I may have done this wrong. Forcing it to work with these calls
const boundUpdateFunc = useCallback((...args: Arguments<Connectable[UE]>): void => {
if (!isMountedRef.current) return;
2021-12-13 05:25:23 +00:00
const value = updatedEventArgsMap(...args);
onUpdated(value);
}, []) as (Connectable & Conflictable)[UE];
const boundConflictFunc = useCallback((...args: Arguments<Conflictable[CE]>): void => {
if (!isMountedRef.current) return;
const value = conflictEventArgsMap(...args);
onConflict(value);
}, []) as (Connectable & Conflictable)[CE];
const bindEventsFunc = useCallback(() => {
2021-12-13 05:25:23 +00:00
guild.on(updatedEventName, boundUpdateFunc);
guild.on(conflictEventName, boundConflictFunc);
}, []);
const unbindEventsFunc = useCallback(() => {
guild.off(updatedEventName, boundUpdateFunc);
guild.off(conflictEventName, boundConflictFunc);
}, []);
GuildSubscriptions.useGuildSubscriptionEffect(isMountedRef, {
guild,
onFetch,
onFetchError,
bindEventsFunc,
unbindEventsFunc
}, fetchFunc);
2021-12-13 05:25:23 +00:00
return [ value, fetchError, events ];
}
private static useMultipleGuildSubscription<
T extends { id: string },
NE extends keyof Connectable,
UE extends keyof Connectable,
RE extends keyof Connectable,
CE extends keyof Conflictable
>(
guild: CombinedGuild,
eventMappingParams: MultipleEventMappingParams<T, NE, UE, RE, CE>,
fetchFunc: (() => Promise<T[]>) | (() => Promise<T[] | null>)
2021-12-13 05:25:23 +00:00
): [value: T[] | null, fetchError: unknown | null, events: EventEmitter<MultipleSubscriptionEvents<T>>] {
const {
newEventName, newEventArgsMap,
updatedEventName, updatedEventArgsMap,
removedEventName, removedEventArgsMap,
conflictEventName, conflictEventArgsMap,
sortFunc
} = eventMappingParams;
const isMountedRef = useRef<boolean>(false);
const [ fetchError, setFetchError ] = useState<unknown | null>(null);
const [ value, setValue ] = useState<T[] | null>(null);
const events = useMemo(() => new EventEmitter<MultipleSubscriptionEvents<T>>(), []);
const onFetch = useCallback((fetchValue: T[] | null) => {
setValue(fetchValue);
setFetchError(null);
events.emit('fetch');
}, []);
const onFetchError = useCallback((e: unknown) => {
setFetchError(e);
setValue(null);
events.emit('fetch-error');
}, []);
const onNew = useCallback((newElements: T[]) => {
setValue(currentValue => {
if (currentValue === null) return null;
return currentValue.concat(newElements).sort(sortFunc);
})
events.emit('new', newElements);
}, []);
const onUpdated = useCallback((updatedElements: T[]) => {
setValue(currentValue => {
if (currentValue === null) return null;
return currentValue.map(element => updatedElements.find(updatedElement => updatedElement.id === element.id) ?? element).sort(sortFunc);
});
events.emit('updated', updatedElements);
}, []);
const onRemoved = useCallback((removedElements: T[]) => {
setValue(currentValue => {
if (currentValue === null) return null;
const deletedIds = new Set(removedElements.map(deletedElement => deletedElement.id));
return currentValue.filter(element => !deletedIds.has(element.id)).sort(sortFunc);
});
events.emit('removed', removedElements);
}, []);
const onConflict = useCallback((changes: Changes<T>) => {
setValue(currentValue => {
if (currentValue === null) return null;
const deletedIds = new Set(changes.deleted.map(deletedElement => deletedElement.id));
return currentValue
.concat(changes.added)
.map(element => changes.updated.find(change => change.newDataPoint.id === element.id)?.newDataPoint ?? element)
.filter(element => !deletedIds.has(element.id))
.sort(sortFunc);
});
events.emit('conflict', changes);
}, []);
// I think the typed EventEmitter class isn't ready for this level of insane type safety
// otherwise, I may have done this wrong. Forcing it to work with these calls
const boundNewFunc = useCallback((...args: Arguments<Connectable[NE]>): void => {
if (!isMountedRef.current) return;
onNew(newEventArgsMap(...args));
}, []) as (Connectable & Conflictable)[NE];
const boundUpdateFunc = useCallback((...args: Arguments<Connectable[UE]>): void => {
if (!isMountedRef.current) return;
onUpdated(updatedEventArgsMap(...args));
}, []) as (Connectable & Conflictable)[UE];
const boundRemovedFunc = useCallback((...args: Arguments<Connectable[RE]>): void => {
if (!isMountedRef.current) return;
onRemoved(removedEventArgsMap(...args));
}, []) as (Connectable & Conflictable)[RE];
const boundConflictFunc = useCallback((...args: Arguments<Conflictable[CE]>): void => {
if (!isMountedRef.current) return;
onConflict(conflictEventArgsMap(...args));
}, []) as (Connectable & Conflictable)[CE];
const bindEventsFunc = useCallback(() => {
guild.on(newEventName, boundNewFunc);
guild.on(updatedEventName, boundUpdateFunc);
guild.on(removedEventName, boundRemovedFunc);
guild.on(conflictEventName, boundConflictFunc);
}, []);
const unbindEventsFunc = useCallback(() => {
2021-12-13 05:25:23 +00:00
guild.off(newEventName, boundNewFunc);
guild.off(updatedEventName, boundUpdateFunc);
guild.off(removedEventName, boundRemovedFunc);
guild.off(conflictEventName, boundConflictFunc);
}, []);
GuildSubscriptions.useGuildSubscriptionEffect(isMountedRef, {
2021-12-13 04:01:30 +00:00
guild,
onFetch,
onFetchError,
bindEventsFunc,
unbindEventsFunc
}, fetchFunc);
2021-12-13 04:01:30 +00:00
return [ value, fetchError, events ];
}
static useGuildMetadataSubscription(guild: CombinedGuild) {
const fetchMetadataFunc = useCallback(async () => {
//LOG.silly('fetching metadata for subscription');
return await guild.fetchMetadata();
}, [ guild ]);
return GuildSubscriptions.useSingleGuildSubscription<GuildMetadata, 'update-metadata', 'conflict-metadata'>(guild, {
2021-12-13 05:25:23 +00:00
updatedEventName: 'update-metadata',
updatedEventArgsMap: (guildMeta: GuildMetadata) => guildMeta,
2021-12-13 04:01:30 +00:00
conflictEventName: 'conflict-metadata',
conflictEventArgsMap: (changesType: AutoVerifierChangesType, oldGuildMeta: GuildMetadata, newGuildMeta: GuildMetadata) => newGuildMeta
}, fetchMetadataFunc);
2021-12-13 04:01:30 +00:00
}
static useResourceSubscription(guild: CombinedGuild, resourceId: string | null) {
const fetchResourceFunc = useCallback(async () => {
//LOG.silly('fetching resource for subscription (resourceId: ' + resourceId + ')');
if (resourceId === null) return null;
return await guild.fetchResource(resourceId);
}, [ guild, resourceId ]);
return GuildSubscriptions.useSingleGuildSubscription<Resource, 'update-resource', 'conflict-resource'>(guild, {
2021-12-13 05:25:23 +00:00
updatedEventName: 'update-resource',
updatedEventArgsMap: (resource: Resource) => resource,
2021-12-13 04:01:30 +00:00
conflictEventName: 'conflict-resource',
conflictEventArgsMap: (query: IDQuery, changesType: AutoVerifierChangesType, oldResource: Resource, newResource: Resource) => newResource
}, fetchResourceFunc);
2021-12-13 04:01:30 +00:00
}
2021-12-13 05:25:23 +00:00
static useTokensSubscription(guild: CombinedGuild) {
const fetchTokensFunc = useCallback(async () => {
//LOG.silly('fetching tokens for subscription');
return await guild.fetchTokens();
}, [ guild ]);
2021-12-13 05:25:23 +00:00
return GuildSubscriptions.useMultipleGuildSubscription<Token, 'new-tokens', 'update-tokens', 'remove-tokens', 'conflict-tokens'>(guild, {
newEventName: 'new-tokens',
newEventArgsMap: (tokens: Token[]) => tokens,
updatedEventName: 'update-tokens',
updatedEventArgsMap: (updatedTokens: Token[]) => updatedTokens,
removedEventName: 'remove-tokens',
removedEventArgsMap: (removedTokens: Token[]) => removedTokens,
conflictEventName: 'conflict-tokens',
conflictEventArgsMap: (changesType: AutoVerifierChangesType, changes: Changes<Token>) => changes,
sortFunc: Token.sortFurthestExpiresFirst
}, fetchTokensFunc);
2021-12-13 05:25:23 +00:00
}
2021-12-13 04:01:30 +00:00
}