cordis/client/webapp/fetchable-pair-verifier.ts

177 lines
9.0 KiB
TypeScript
Raw Normal View History

2021-11-21 01:06:50 +00:00
import { Changes, Channel, GuildMetadata, Member, Message, Resource, Token } from './data-types';
import { AsyncFetchable, Fetchable, Lackable, Conflictable } from './guild-types';
import { AutoVerifier, AutoVerifierChangesType } from './auto-verifier';
import { AutoVerifierWithArg, PartialMessageListQuery, IDQuery } from './auto-verifier-with-args';
import { EventEmitter } from 'tsee';
2021-11-21 18:29:42 +00:00
export default class PairVerifierFetchable extends EventEmitter<Conflictable> implements AsyncFetchable {
2021-11-21 01:06:50 +00:00
private readonly fetchMetadataVerifier: AutoVerifier<GuildMetadata>;
private readonly fetchMembersVerifier: AutoVerifier<Member[]>;
private readonly fetchChannelsVerifier: AutoVerifier<Channel[]>;
private readonly fetchTokensVerifier: AutoVerifier<Token[]>;
private readonly fetchResourceVerifier: AutoVerifierWithArg<Resource, IDQuery>
private readonly fetchMessagesRecentVerifier: AutoVerifierWithArg<Message[], PartialMessageListQuery>;
private readonly fetchMessagesBeforeVerifier: AutoVerifierWithArg<Message[], PartialMessageListQuery>;
private readonly fetchMessagesAfterVerifier: AutoVerifierWithArg<Message[], PartialMessageListQuery>;
constructor(
primary: Fetchable & Lackable,
trusted: Fetchable
) {
super();
this.fetchMetadataVerifier = AutoVerifier.createStandardSingleAutoVerifier<GuildMetadata>(
async () => await primary.fetchMetadata(),
async () => await trusted.fetchMetadata(),
async (changesType: AutoVerifierChangesType, primaryMetadata: GuildMetadata | null, trustedMetadata: GuildMetadata | null) => {
if (changesType === AutoVerifierChangesType.TRUSTED_ONLY) {
await primary.handleMetadataChanged(trustedMetadata as GuildMetadata);
} else if (changesType === AutoVerifierChangesType.CONFLICT) {
await primary.handleMetadataChanged(trustedMetadata as GuildMetadata);
this.emit('conflict-metadata', primaryMetadata as GuildMetadata, trustedMetadata as GuildMetadata);
}
}
)
this.fetchMembersVerifier = AutoVerifier.createStandardListAutoVerifier<Member>(
async () => await primary.fetchMembers(),
async () => await trusted.fetchMembers(),
async (changesType: AutoVerifierChangesType, changes: Changes<Member>) => {
if (changes.added.length > 0) await primary.handleMembersAdded(changes.added);
if (changes.updated.length > 0) await primary.handleMembersChanged(changes.updated.map(change => change.newDataPoint));
if (changes.deleted.length > 0) await primary.handleMembersDeleted(changes.deleted);
if (changesType === AutoVerifierChangesType.CONFLICT) {
this.emit('conflict-members', changes);
}
}
);
this.fetchChannelsVerifier = AutoVerifier.createStandardListAutoVerifier<Channel>(
async () => await primary.fetchChannels(),
async () => await trusted.fetchChannels(),
async (changesType: AutoVerifierChangesType, changes: Changes<Channel>) => {
if (changes.added.length > 0) await primary.handleChannelsAdded(changes.added);
if (changes.updated.length > 0) await primary.handleChannelsChanged(changes.updated.map(change => change.newDataPoint));
if (changes.deleted.length > 0) await primary.handleChannelsDeleted(changes.deleted);
if (changesType === AutoVerifierChangesType.CONFLICT) {
this.emit('conflict-channels', changes);
}
}
);
this.fetchTokensVerifier = AutoVerifier.createStandardListAutoVerifier<Token>(
async () => await primary.fetchTokens(),
async () => await trusted.fetchTokens(),
async (changesType: AutoVerifierChangesType, changes: Changes<Token>) => {
if (changes.added.length > 0) await primary.handleTokensAdded(changes.added);
if (changes.updated.length > 0) await primary.handleTokensChanged(changes.updated.map(change => change.newDataPoint));
if (changes.deleted.length > 0) await primary.handleTokensDeleted(changes.deleted);
if (changesType === AutoVerifierChangesType.CONFLICT) {
this.emit('conflict-tokens', changes);
}
}
);
this.fetchResourceVerifier = AutoVerifierWithArg.createStandardIDQueriedSingleAutoVerifier<Resource>(
async (query: IDQuery) => await primary.fetchResource(query.id),
async (query: IDQuery) => await trusted.fetchResource(query.id),
async (query: IDQuery, changesType: AutoVerifierChangesType, primaryResource: Resource | null, trustedResource: Resource | null) => {
if (changesType === AutoVerifierChangesType.PRIMARY_ONLY) {
await primary.handleResourceDeleted(trustedResource as Resource);
} else if (changesType === AutoVerifierChangesType.TRUSTED_ONLY) {
await primary.handleResourceAdded(trustedResource as Resource);
} else if (changesType === AutoVerifierChangesType.CONFLICT) {
await primary.handleResourceChanged(trustedResource as Resource);
this.emit('conflict-resource', primaryResource as Resource, trustedResource as Resource);
}
}
);
this.fetchMessagesRecentVerifier = AutoVerifierWithArg.createStandardPartialMessageListAutoVerifier(
async (query: PartialMessageListQuery) => await primary.fetchMessagesRecent(query.channelId, query.number),
async (query: PartialMessageListQuery) => await trusted.fetchMessagesRecent(query.channelId, query.number),
async (query: PartialMessageListQuery, changesType: AutoVerifierChangesType, changes: Changes<Message>) => {
if (changes.added.length > 0) await primary.handleMessagesAdded(changes.added);
if (changes.updated.length > 0) await primary.handleMessagesChanged(changes.updated.map(change => change.newDataPoint));
if (changes.deleted.length > 0) await primary.handleMessagesDeleted(changes.deleted);
if (changesType === AutoVerifierChangesType.CONFLICT) {
this.emit('conflict-messages', changes);
}
}
);
this.fetchMessagesBeforeVerifier = AutoVerifierWithArg.createStandardPartialMessageListAutoVerifier(
async (query: PartialMessageListQuery) => await primary.fetchMessagesBefore(query.channelId, query.messageId as string, query.number),
async (query: PartialMessageListQuery) => await trusted.fetchMessagesBefore(query.channelId, query.messageId as string, query.number),
async (query: PartialMessageListQuery, changesType: AutoVerifierChangesType, changes: Changes<Message>) => {
if (changes.added.length > 0) await primary.handleMessagesAdded(changes.added);
if (changes.updated.length > 0) await primary.handleMessagesChanged(changes.updated.map(change => change.newDataPoint));
if (changes.deleted.length > 0) await primary.handleMessagesDeleted(changes.deleted);
if (changesType === AutoVerifierChangesType.CONFLICT) {
this.emit('conflict-messages', changes);
}
}
);
this.fetchMessagesAfterVerifier = AutoVerifierWithArg.createStandardPartialMessageListAutoVerifier(
async (query: PartialMessageListQuery) => await primary.fetchMessagesAfter(query.channelId, query.messageId as string, query.number),
async (query: PartialMessageListQuery) => await trusted.fetchMessagesAfter(query.channelId, query.messageId as string, query.number),
async (query: PartialMessageListQuery, changesType: AutoVerifierChangesType, changes: Changes<Message>) => {
if (changes.added.length > 0) await primary.handleMessagesAdded(changes.added);
if (changes.updated.length > 0) await primary.handleMessagesChanged(changes.updated.map(change => change.newDataPoint));
if (changes.deleted.length > 0) await primary.handleMessagesDeleted(changes.deleted);
if (changesType === AutoVerifierChangesType.CONFLICT) {
this.emit('conflict-messages', changes);
}
}
);
}
unverify() {
this.fetchMetadataVerifier.unverify();
this.fetchMembersVerifier.unverify();
this.fetchChannelsVerifier.unverify();
this.fetchTokensVerifier.unverify();
this.fetchResourceVerifier.unverifyAll();
this.fetchMessagesRecentVerifier.unverifyAll();
this.fetchMessagesBeforeVerifier.unverifyAll();
this.fetchMessagesAfterVerifier.unverifyAll();
}
async fetchMetadata(): Promise<GuildMetadata | null> {
return await this.fetchMetadataVerifier.fetchAndVerifyIfNeeded();
}
async fetchMembers(): Promise<Member[] | null> {
return await this.fetchMembersVerifier.fetchAndVerifyIfNeeded();
}
async fetchChannels(): Promise<Channel[] | null> {
return await this.fetchChannelsVerifier.fetchAndVerifyIfNeeded();
}
async fetchMessagesRecent(channelId: string, number: number): Promise<Message[] | null> {
return await this.fetchMessagesRecentVerifier.fetchAndVerifyIfNeded({ channelId, messageId: null, number });
}
async fetchMessagesBefore(channelId: string, messageId: string, number: number): Promise<Message[] | null> {
return await this.fetchMessagesBeforeVerifier.fetchAndVerifyIfNeded({ channelId, messageId, number });
}
async fetchMessagesAfter(channelId: string, messageId: string, number: number): Promise<Message[] | null> {
return await this.fetchMessagesAfterVerifier.fetchAndVerifyIfNeded({ channelId, messageId, number });
}
async fetchResource(resourceId: string): Promise<Resource | null> {
return await this.fetchResourceVerifier.fetchAndVerifyIfNeded({ id: resourceId });
}
async fetchTokens(): Promise<Token[] | null> {
return await this.fetchTokensVerifier.fetchAndVerifyIfNeeded();
}
}