From e8e3ab3cd5b0dea3af44f9ceb2e481473163a846 Mon Sep 17 00:00:00 2001 From: Bsian Date: Sun, 15 Mar 2020 16:10:54 +0000 Subject: [PATCH] #72 reflections --- index.d.ts | 1 + types/Core/Stores/ARegistry.ts | 43 +++----- types/Core/Stores/GuildConfigCache.ts | 21 ++-- types/Utility/Collection.ts | 117 ++------------------ types/Utility/External/LRUCache.ts | 85 ++++----------- types/Utility/External/SortedList.ts | 2 +- types/Utility/Store.ts | 149 ++++++++++++++++++++++++++ types/index.ts | 3 +- 8 files changed, 200 insertions(+), 221 deletions(-) create mode 100644 types/Utility/Store.ts diff --git a/index.d.ts b/index.d.ts index 97646efa..819cc1b0 100644 --- a/index.d.ts +++ b/index.d.ts @@ -159,6 +159,7 @@ declare namespace AxonCore { export import Stack = Types.Stack; export import AxonUtils = Types.AxonUtils; export import Collection = Types.Collection; + export import Store = Types.Store; export import Utils = Types.Utils; export import AxonClient = Types.AxonClient; export import AxonOptions = Types.AxonOptions; diff --git a/types/Core/Stores/ARegistry.ts b/types/Core/Stores/ARegistry.ts index 22807afe..980d04e6 100644 --- a/types/Core/Stores/ARegistry.ts +++ b/types/Core/Stores/ARegistry.ts @@ -1,4 +1,4 @@ -import { AxonClient, Collection } from '../../'; +import { AxonClient, Store } from '../../'; /** * Abstract class to hold and manage a set of items. @@ -7,12 +7,13 @@ import { AxonClient, Collection } from '../../'; * * @abstract * @class ARegistry + * @extends Store */ -export declare class ARegistry { +export declare class ARegistry extends Store { /** The AxonClient */ private _axon: AxonClient; - /** The collection of items hold by the registry */ - public registry: Collection; + /** The base definition to use for the registry */ + private _base: T /** * Creates an instance of ARegistry. * @@ -23,63 +24,47 @@ export declare class ARegistry { constructor(axon: AxonClient, base: T); /** * Get the AxonClient - * * @readonly * @memberof ARegistry */ readonly axon: AxonClient; /** - * Get the size of the registry - * + * Returns the current registry * @readonly * @memberof ARegistry */ - readonly size: number; + readonly registry: Map; /** - * Check whether the item exist in the registry - * + * Check whether the item exists in the registry * @returns Whether the item exists * @memberof ARegistry */ - has(key: string): boolean; + public has(key: string): boolean; /** * Get an item from the registry - * * @returns The item * @memberof ARegistry */ - get(key: string): T | null; - /** - * Get the registry - * - * @returns The current registry - * @memberof ARegistry - */ - getAll(): Collection; + public get(key: string): T; /** * Add an item to the registry - * * @returns The registry * @memberof ARegistry */ - add(key: string, value: T): Collection; + public add(key: string, value: T): ARegistry; /** * Remove an item from the registry - * - * @returns {Boolean} - Whether it could remove the item or not + * @returns Whether it removed the item or not * @memberof ARegistry */ - remove(key: string): boolean; - public [Symbol.iterator](): [string|number, T][]; + public remove(key: string): boolean; /** - * Register correctly an item in the registry. - * + * Register correctly an item in the registry * @memberof ARegistry */ public register(key: string, value: T): any; // Not implemented /** * Unregister correctly an item from the registry. - * * @memberof ARegistry */ public unregister(key: string, value: T): any; // Not implemented diff --git a/types/Core/Stores/GuildConfigCache.ts b/types/Core/Stores/GuildConfigCache.ts index b81c614e..feb45505 100644 --- a/types/Core/Stores/GuildConfigCache.ts +++ b/types/Core/Stores/GuildConfigCache.ts @@ -1,4 +1,4 @@ -import { AxonClient, LRUCache, GuildConfig } from '../../'; +import { AxonClient, LRUCache, GuildConfig, Store } from '../../'; /** * Handles GuildConfigs cache. @@ -7,10 +7,10 @@ import { AxonClient, LRUCache, GuildConfig } from '../../'; * @author KhaaZ * * @class GuildConfigsCache + * @extends Store */ -export declare class GuildConfigCache { +export declare class GuildConfigCache extends Store { private _axon: AxonClient; - public guildConfigs: LRUCache; /** * Creates an instance of GuildConfigsCache. * @@ -18,18 +18,11 @@ export declare class GuildConfigCache { */ constructor(axonClient: AxonClient); /** - * Get a GuildConfig from the guild ID. - * - * @memberof GuildConfigsCache - */ - get(key: string): GuildConfig; - /** - * Set a GuildConfig with the Guild ID as key. - * - * @memberof GuildConfigsCache + * Returns the cache + * @readonly + * @memberof GuildConfigCache */ - set(key: string, value: GuildConfig): void; - public [Symbol.iterator](): [string|number, GuildConfig]; + readonly guildConfigs: LRUCache; /** * Get a GuildConfig from the cache or from the DB if not in the cache. * diff --git a/types/Utility/Collection.ts b/types/Utility/Collection.ts index 09eb8a9c..e19a13d2 100644 --- a/types/Utility/Collection.ts +++ b/types/Utility/Collection.ts @@ -1,15 +1,16 @@ +import { Store } from '../'; + /** - * Extended Map with built in methods for ease of data manipulation. - * Based on Eris.Collection + * Custom Store that uses Map as cache. + * Enforces that a single object type exists in a Collection. * * @author KhaaZ * * @class Collection - * @extends - * - * @prop {Class} baseObject - The base class for all items + * @extends Store */ -export declare class Collection extends Map { +export declare class Collection extends Store { + /** The base class for all items */ public baseObject: new (...args: any[] ) => T; /** * Creates an instance of Collection. @@ -40,118 +41,14 @@ export declare class Collection extends Map { */ public add(key: string, value: T, replace?: boolean): T; - /** - * Return the first object to make the function evaluate true - * - * @param func - A function that takes an object and returns true if it matches - * @returns The first matching object, or null if no match - * @memberof Collection - */ - public find(func: (i: T) => boolean): T; - - /** - * Get a random object from the Collection - * - * @returns The random object, or null if there is no match - * @memberof Collection - */ - public random(): T; - - /** - * Return all the objects that make the function evaluate true - * - * @param func - A function that takes an object and returns true if it matches - * @returns An array containing all the objects that matched - * @memberof Collection - */ - public filter(func: (i: T) => boolean): T[]; - - /** - * Return an array with the results of applying the given function to each element - * - * @param func - A function that takes an object and returns something - * @returns An array containing the results - * @memberof Collection - */ - public map(func: (i: T) => R): R[]; - - /** - * Reduce values by function - * - * @param func - Function to execute on each element in the array - * @param initialValue - Value to use as the first argument to the first call of the callback - * @returns Accumulator - * @memberof Collection - */ - public reduce(func: (accumulator: U, val: T) => U, initialValue?: number): U; - - /** - * Test if all elements pass the test implemented by the provided function. Returns true if yes, or false if not. - * - * @param func - A function that takes an object and returns true if it matches - * @returns An array containing all the objects that matched - * @memberof Collection - */ - public every(func: (i: T) => boolean): boolean; - - /** - * Test if at least one element passes the test implemented by the provided function. Returns true if yes, or false if not. - * - * @param func - A function that takes an object and returns true if it matches - * @returns An array containing all the objects that matched - * @memberof Collection - */ - public some(func: (i: T) => boolean): boolean; - - /** - * Update an object - * - * @param key - The ID of the object - * @param value - The updated object data - * @returns The updated object - * @memberof Collection - */ - public update(key: string, value: T): T; - /** * Remove an object * * @param {String} key - The ID of the object * @returns {T} The removed object, or null if nothing was removed - * * @memberof Collection */ public remove(key: string): T | null; - /** - * Map to array - * [ value, value, value ] - * @memberof Collection - */ - public toArray(): T[]; - - /** - * Map to object - * { key: value, key: value } - * @memberof Collection - */ - public toObject(): {[key: string]: T;}; - public toString(): `[Collection]`; - - /** - * Apply a function to the Collection and returns a new Collection - * @param key - The property to use as key for the new Collection - * @param func - The function name to apply to the Collection - * @param args - All the argument that need to be applied to the Collection - * @returns A new Collection modified by the apply call - */ - public apply(key: string, func: 'from', args: [Array, string] ): Collection; - public apply(key: string, func: 'add', args: [string, T, boolean?] ): Collection; - public apply(key: string, func: 'find' | 'filter', args: [(i: T) => boolean] ): Collection; - public apply(key: string, func: 'random' | 'toArray' | 'toObject'): Collection; - public apply(key: string, func: 'map', args: [(i: T) => R] ): Collection; - public apply(key: string, func: 'reduce', args: [(accumulator: U, val: T) => U, U] ): Collection; - public apply(key: string, func: 'update', args: [string, T] ): Collection; - public apply(key: string, func: 'remove', args: [string] ): Collection; } diff --git a/types/Utility/External/LRUCache.ts b/types/Utility/External/LRUCache.ts index 61c5b733..ac5277b7 100644 --- a/types/Utility/External/LRUCache.ts +++ b/types/Utility/External/LRUCache.ts @@ -1,5 +1,9 @@ -import { Collection } from '../../'; +import { Store } from '../../'; +/** + * @author KhaaZ + * @class Node + */ declare class Node { public key: string; public value: T; @@ -20,94 +24,43 @@ declare class Node { * @author KhaaZ * * @class LRUCache + * @extends Store> */ -export declare class LRUCache { +export declare class LRUCache extends Store { /** Maximum size of the LRU */ public limit: number; - /** Current size of the LRU */ - public size: number; public head: Node | null; public tail: Node | null; - /** The Collection holding the cache (private, handled by the LRU structure) */ - private _cache: Collection; /** * Creates an instance of LRUCache. * - * @param limit - Max number of element in the Collection - * @param options - Options used to construct the Collection + * @param limit - Max number of elements in the cache + * @param options - Options used to construct the cache * @memberof LRUCache */ - constructor(limit: number, options: { base?: new (...args: any[] ) => T; iterable?: {[key: string]: T;} | [string, T][]; } ); + constructor(limit: number, options: { iterable?: {[key: string]: T;} | [string, T][]; } ); + /** - * Add a value in the LRU cache. - * + * Retrieve a value from the LRU cache * @memberof LRUCache */ - public set(key: string, value: T): void; + public get(key: string): T | null; /** - * Retrieve a value from the LRU cache - * - * @returns Value + * Add a value in the LRU cache. * @memberof LRUCache */ - public get(key: string): T | null; + public set(key: string, value: T): LRUCache; /** - * Remove an element from the LRUCache - * + * Delete an element from the LRU cache * @memberof LRUCache */ - public remove(key: string): void; + public delete(key: string): boolean; /** * Empty the LRUCache entirely * @memberof LRUCache */ public clear(): void; private _ensureLimit(): void; - /** - * Execute a function against every element of the Collection - * - * @memberof LRUCache - */ - public forEach(fn: (value: T, key: K, map: Map) => void): void; - /** - * Return the first object to make the function evaluate true - * - * @param func - A function that takes an object and returns true if it matches - * @returns The first matching object, or null if no match - * @memberof LRUCache - */ - public find(func: (i: T) => boolean): T; - /** - * Return an array with the results of applying the given function to each element - * - * @param func - A function that takes an object and returns something - * @returns An array containing the results - * @memberof LRUCache - */ - public map(func: (i: T) => R): R[]; - /** - * Return all the objects that make the function evaluate true - * - * @param func - A function that takes an object and returns true if it matches - * @returns An array containing all the objects that matched - * @memberof LRUCache - */ - public filter(func: (i: T) => boolean): T[]; - /** - * Test if at least one element passes the test implemented by the provided function. Returns true if yes, or false if not. - * - * @param func - A function that takes an object and returns true if it matches - * @returns An array containing all the objects that matched - * @memberof LRUCache - */ - public some(func: (i: T) => boolean): boolean; - /** - * Test if all elements pass the test implemented by the provided function. Returns true if yes, or false if not. - * - * @param func - A function that takes an object and returns true if it matches - * @returns An array containing all the objects that matched - * @memberof LRUCache - */ - public every(func: (i: T) => boolean): boolean; - public [Symbol.iterator](): [string|number, T][]; + + public [Symbol.iterator](): IterableIterator<[string, T]>; } diff --git a/types/Utility/External/SortedList.ts b/types/Utility/External/SortedList.ts index 7b369556..c396c59f 100644 --- a/types/Utility/External/SortedList.ts +++ b/types/Utility/External/SortedList.ts @@ -19,7 +19,7 @@ export declare class SortedList { * @readonly * @memberof SortedList */ - public size: number; + readonly size: number; /** * Whether the list is empty or not diff --git a/types/Utility/Store.ts b/types/Utility/Store.ts new file mode 100644 index 00000000..c73f37d7 --- /dev/null +++ b/types/Utility/Store.ts @@ -0,0 +1,149 @@ +/** + * Generic class that expose all data manipulation methods. + * A Store can be constructed with any Key => Value structure (Map like object). + * It exposes all methods similar to aarray methods in one place. + * + * @author KhaaZ + * @class Store + * @prop {Map} cache - Key => Value Data structure + */ +export declare class Store { + /** Key => Value data structure */ + public cache: Map; + + /** + * Current size of the cache + * @readonly + * @memberof Store + */ + readonly size: number; + + /** + * Iterator over `cache.entries` + * @memberof Store + */ + public entries(): IterableIterator<[string, T]>; + /** + * Iterator over `cache.keys` + * @memberof Store + */ + public keys(): IterableIterator; + /** + * Iterator over `cache.values` + * @memberof Store + */ + public values(): IterableIterator; + /** + * Iterator over cache + * @memberof Store + */ + public [Symbol.iterator](): IterableIterator<[string, T]>; + /** + * Whether an element exists in the store + * @memberof Store + */ + public has(key: string): boolean; + /** + * Get an element by key in the store + * @memberof Store + */ + public get(key: string): T | null; + /** + * Set an element in the store + * @returns Current store + * @memberof Store + */ + public set(key: string, value: T): Store; + /** + * Delete an element by key in the store + * @returns Whether the element was deleted or not + * @memberof Store + */ + public delete(key: string): boolean; + /** + * `Store.cache` to array + * @memberof Store + */ + public toArray(): T[]; + /** + * `Store.cache` to object + * @memberof Store + */ + public toObject(): { [key: string]: T; }; + /** + * Execute a function over each element of the Store in a synchronous fashion + * @param func A function that takes an object and do something on it + * @returns The current store + * @memberof Store + */ + public forEach(func: (i: T) => void): Store; + /** + * Execute a function over each element of the Store in an asynchronous fashion + * @param func A function that takes an object and do something on it + * @returns The current store + * @memberof Store + */ + public forEachAsync(func: (i: T) => Promise): Promise>; + /** + * Return the first object to make the function evaluate true + * @param func A function that takes an object and returns true if it matches + * @returns The first matching object, or null if no match + * @memberof Store + */ + public find(func: (i: T) => boolean): T; + /** + * Return an array with the results of applying the given function to each element + * @param func A function that takes an object and returns something + * @returns An array containing the results + * @memberof Store + */ + public map(func: (i: T) => R): R[]; + /** + * Return all the objects that make the function evaluate true + * @param func A function that takes an object and returns true if it matches + * @returns An array containing all the objects that matched + * @memberof Store + */ + public filter(func: (i: T) => boolean): T[]; + /** + * Reduce values by function + * @param func Function to execute on each element in the array + * @param initialValue Value to use as the first argument to the first call of the callback + * @returns Accumulator + * @memberof Store + */ + public reduce(func: (accumulator: U, val: T) => U, initialValue?: number): U; + /** + * Test if at least one element passes the test implemented by the provided function. Returns true if yes, or false if not. + * @param func A function that takes an object and returns true if it matches + * @returns Whether or not any of the elements returned true + * @memberof Store + */ + public some(func: (i: T) => boolean): boolean + /** + * Test if all elements pass the test implemented by the provided function. Returns true if yes, or false if not. + * @param func A function that takes an object and returns true if it matches + * @returns Whether or not all of the elements returned true + * @memberof Store + */ + public every(func: (i: T) => boolean): boolean + /** + * Get a random object from the store + * @returns The random object, or null if there is no match + * @memberof Store + */ + public random(): T; + + /** + * Apply a function to the Store and returns a new Store. + * Usable over: `map`, `filter`, `toArray` + * @param key The property to use as key for the new Store + * @param func The function name to apply to the Store + * @param fn A function that takes an object and returns it (map) or true if it matches (filter) + */ + public apply(key: string, func: 'map', fn: (i: T) => R): Store; + public apply(key: string, func: 'filter', fn: (i: T) => boolean): Store; + public apply(key: string, func: 'toArray'): Store; + + public toString(): string; +} diff --git a/types/index.ts b/types/index.ts index 528a3609..17effa40 100644 --- a/types/index.ts +++ b/types/index.ts @@ -91,17 +91,18 @@ export { Prompt } from './Utility/Discord/Prompt'; export { ReactionCollector } from './Utility/Discord/Collectors/ReactionCollector'; export { MessageCollector } from './Utility/Discord/Collectors/MessageCollector'; export { Collector } from './Utility/Discord/Collectors/Collector'; -export { SortedList } from './Utility/External/SortedList'; export { AsyncQueue } from './Utility/External/AsyncQueue'; export { AutoQueue } from './Utility/External/AutoQueue'; export { FunctionQueue } from './Utility/External/FunctionQueue'; export { LRUCache } from './Utility/External/LRUCache'; export { Queue } from './Utility/External/Queue'; +export { SortedList } from './Utility/External/SortedList'; export { Stack } from './Utility/External/Stack'; export { AxonUtils } from './Utility/AxonUtils'; export { Collection } from './Utility/Collection'; +export { Store } from './Utility/Store'; export { Utils } from './Utility/Utils'; export { AxonClient } from './AxonClient';