forked from Khaaz/AxonCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
315 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { EventEmitter } from 'events'; | ||
import { | ||
Collection, TimeoutQueue, AxonClient, CollectorContainer, LibClient, LibraryInterface, | ||
} from '../../../'; | ||
|
||
/** | ||
* @param collectors The collectors that will collect the element | ||
* @param obj Collected element and id | ||
*/ | ||
type collectListener<T> = (collectors: CollectorContainer<T>[], obj: { | ||
/** The collected element id */ id: string; | ||
/** The collected element */ collected: T; | ||
} ) => void | ||
/** | ||
* @param id The id of the CollectorContainer that timed out | ||
*/ | ||
type timeoutListener = (id: string) => void; | ||
|
||
/** | ||
* Base Collector class. | ||
* | ||
* Collect a specific number of an element. | ||
* Resolve with a Collection of the element collected. | ||
* Timeout if needed. | ||
* | ||
* It is advised only to use one instance per Collector type. | ||
* This Collector handles using only one Collector instance with many collectors running. | ||
* | ||
* @class Collector | ||
* @extends {EventEmitter} | ||
*/ | ||
export declare class Collector<T> extends EventEmitter { | ||
/** The AxonClient instance */ | ||
private _axon: AxonClient; | ||
/** Collection of CollectorContainer */ | ||
public collectors: Collection<CollectorContainer<T>>; | ||
/** The current timeout queue sorted with the first timeout due at the top of the queue */ | ||
public timeoutQueue: TimeoutQueue; | ||
/** Unique increment count used to generate ids */ | ||
private _INCREMENT: number; | ||
/** Whether the Collector is currently running */ | ||
public running: boolean; | ||
/** setInterval ID used to clear setinterval */ | ||
private _intervalID: string|null; | ||
/** | ||
* Creates an instance of Collector. | ||
* @memberof Collector | ||
*/ | ||
constructor(axonClient: AxonClient); | ||
|
||
/** | ||
* The AxonClient instance | ||
* @readonly | ||
* @memberof Collector | ||
*/ | ||
readonly axon: AxonClient; | ||
/** | ||
* The BotClient instance | ||
* @readonly | ||
* @memberof Collector | ||
*/ | ||
readonly bot: LibClient; | ||
/** | ||
* The LibraryInterface instance | ||
* @readonly | ||
* @memberof Collector | ||
*/ | ||
readonly lib: LibraryInterface; | ||
/** | ||
* The current INCREMENT count. | ||
* Reset at 9999 | ||
* @readonly | ||
* @memberof Collector | ||
*/ | ||
readonly INCREMENT: number; | ||
|
||
/** | ||
* Generate a unique ID by using the current timestamp and appending a count (INCREMENT) | ||
* @returns The unique ID generated | ||
* @memberof Collector | ||
*/ | ||
private _genID(): string; | ||
/** | ||
* Run this Collector with the given options | ||
* @returns Collection of elements to resolve | ||
*/ | ||
public run(...args: any[] ): Promise<Map<string, T>>; // Not Implemented | ||
/** | ||
* Set all listeners to the relevant function (listen to the event) | ||
* @memberof Collector | ||
*/ | ||
public setListeners(): void; // Not Implemented | ||
/** | ||
* Unset all listeners (stop listening) | ||
* @memberof Collector | ||
*/ | ||
public unsetListeners(): void; | ||
private _run(options: { | ||
/** Number of milliseconds before timing out */ timeout?: number; | ||
/** Number of elements to collect before resolving */ count?: number; | ||
} ): Promise<Map<string, T>>; | ||
private _preRun<U, V = never>(options: { | ||
/** Number of milliseconds before timing out */ timeout?: number; | ||
/** Number of elements to collect before resolving */ count?: number; | ||
}, resolve: (value?: U | PromiseLike<U>) => Promise<U>|Promise<void>, reject: (reason: any) => Promise<V>): void; | ||
private _postRun(): void; | ||
/** | ||
* Handles checking for timeout via setInterval | ||
* @fires Collectir#collect | ||
* @memberof Collector | ||
*/ | ||
public timeout(): void; | ||
/** | ||
* Fired to collect an element | ||
* @event Collector#collect | ||
* @memberof Collector | ||
*/ | ||
on(event: 'collect', listener: collectListener<T>): this; | ||
/** | ||
* Fired on timeout for a CollectorContainer | ||
* @event Collector#timeout | ||
* @memberof Collector | ||
*/ | ||
on(event: 'timeout', listener: timeoutListener): this; | ||
/** | ||
* Handles on collect action | ||
* @memberof Collector | ||
*/ | ||
public onCollect(collectors: CollectorContainer<T>[], param: { | ||
id: string; | ||
/** Element collected */ collected: T; | ||
} ): void; | ||
/** | ||
* Handles on timeout action | ||
* @memberof Collector | ||
*/ | ||
public onTimeout(id: string): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { | ||
CollectorOptions, AxonClient, LibMessage, LibTextableChannel, Collector, CollectorContainer, | ||
} from '../../..'; | ||
|
||
/** | ||
* Collect bunch of message object according to chosen options | ||
* | ||
* @author VoidNull, KhaaZ | ||
* | ||
* @class MessageCollector | ||
* @extends EventEmitter | ||
*/ | ||
export declare class MessageCollector extends Collector<LibMessage> { | ||
public options: CollectorOptions; | ||
public onMessageCreate: (msg: LibMessage) => void; | ||
public onMessageDelete: (msg: LibMessage) => void; | ||
public onMessageUpdate: (msg: LibMessage, msg1: LibMessage) => void | ||
|
||
/** | ||
* Creates an instance of MessageCollector | ||
* @param client - The axon client object | ||
* @param options - The default options for the message collector instance | ||
* @example | ||
* const collector = new MessageCollector(this.axon, { count: 10, ignoreBots: false }); | ||
* @memberof MessageCollector | ||
*/ | ||
constructor(client: AxonClient, options?: CollectorOptions); | ||
|
||
/** | ||
* Runs the message collector | ||
* | ||
* @param channel The channel object to listen to | ||
* @param options The options for the message collector | ||
* @returns Map of messages collected. | ||
* | ||
* @example | ||
* const messages = await collector.run(msg.channel, { caseInsensitive: false }); | ||
* @memberof MessageCollector | ||
*/ | ||
public run(channel: LibTextableChannel, options?: CollectorOptions): Promise<Map<string, LibMessage> >; | ||
/** | ||
* Get all CollectorContainers that will collect from this particular message | ||
* @memberof MessageCollector | ||
*/ | ||
public getCollectors(message: LibMessage): CollectorContainer<LibMessage>[]; | ||
/** | ||
* Function bound to messageCreate event. | ||
* Collect the message for all collectors that responds to the criteria. | ||
* Emits collect event | ||
* @emits MessageCollector#collect | ||
* @memberof MessageCollector | ||
*/ | ||
private _onMessageCreate(msg: LibMessage): void; | ||
/** | ||
* Function bound to messageDelete event. | ||
* Remove the message from all collectors that collected this message | ||
* @memberof MessageCollector | ||
*/ | ||
private _onMessageDelete(msg: LibMessage): void; | ||
/** | ||
* Function bound to messageUpdate event. | ||
* Updates the message from all collector that collected this message | ||
* @memberof MessageCollector | ||
*/ | ||
private _onMessageUpdate(oldMsg: LibMessage, msg: LibMessage): void; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Timeout } from '../../../'; | ||
|
||
/** | ||
* TimeoutQueue Object. Sorted Array of timeout, with the next due timeout at the top | ||
* | ||
* @class TimeoutQueue | ||
*/ | ||
export declare class TimeoutQueue { | ||
public queue: Timeout[]; | ||
/** | ||
* Creates an instance of TimeoutQueue | ||
* @memberof TimeoutQueue | ||
*/ | ||
constructor(); | ||
|
||
/** | ||
* Whether the queue is empty or not | ||
* @memberof TimeoutQueue | ||
*/ | ||
public isEmpty(): boolean; | ||
/** | ||
* Returns the first element of the queue without removing it | ||
* @memberof TimeoutQueue | ||
*/ | ||
public peek(): Timeout; | ||
/** | ||
* Adds an element in the queue, sorting by ascending timeout | ||
* @memberof TimeoutQueue | ||
*/ | ||
add(id: string, timeout: number): void; | ||
/** | ||
* Get the first timeout in the queue and removes it | ||
* @memberof TimeoutQueue | ||
*/ | ||
getNext(): Timeout; | ||
} |
Oops, something went wrong.