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'; export default class PairVerifierFetchable extends EventEmitter implements AsyncFetchable { private readonly fetchMetadataVerifier: AutoVerifier; private readonly fetchMembersVerifier: AutoVerifier; private readonly fetchChannelsVerifier: AutoVerifier; private readonly fetchTokensVerifier: AutoVerifier; private readonly fetchResourceVerifier: AutoVerifierWithArg private readonly fetchMessagesRecentVerifier: AutoVerifierWithArg; private readonly fetchMessagesBeforeVerifier: AutoVerifierWithArg; private readonly fetchMessagesAfterVerifier: AutoVerifierWithArg; constructor( primary: Fetchable & Lackable, trusted: Fetchable ) { super(); this.fetchMetadataVerifier = AutoVerifier.createStandardSingleAutoVerifier( 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( async () => await primary.fetchMembers(), async () => await trusted.fetchMembers(), async (changesType: AutoVerifierChangesType, changes: Changes) => { 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( async () => await primary.fetchChannels(), async () => await trusted.fetchChannels(), async (changesType: AutoVerifierChangesType, changes: Changes) => { 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( async () => await primary.fetchTokens(), async () => await trusted.fetchTokens(), async (changesType: AutoVerifierChangesType, changes: Changes) => { 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( 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) => { 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) => { 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) => { 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 { return await this.fetchMetadataVerifier.fetchAndVerifyIfNeeded(); } async fetchMembers(): Promise { return await this.fetchMembersVerifier.fetchAndVerifyIfNeeded(); } async fetchChannels(): Promise { return await this.fetchChannelsVerifier.fetchAndVerifyIfNeeded(); } async fetchMessagesRecent(channelId: string, number: number): Promise { return await this.fetchMessagesRecentVerifier.fetchAndVerifyIfNeded({ channelId, messageId: null, number }); } async fetchMessagesBefore(channelId: string, messageId: string, number: number): Promise { return await this.fetchMessagesBeforeVerifier.fetchAndVerifyIfNeded({ channelId, messageId, number }); } async fetchMessagesAfter(channelId: string, messageId: string, number: number): Promise { return await this.fetchMessagesAfterVerifier.fetchAndVerifyIfNeded({ channelId, messageId, number }); } async fetchResource(resourceId: string): Promise { return await this.fetchResourceVerifier.fetchAndVerifyIfNeded({ id: resourceId }); } async fetchTokens(): Promise { return await this.fetchTokensVerifier.fetchAndVerifyIfNeeded(); } }