Skip to content

Commit

Permalink
Khaaz#72 reflections
Browse files Browse the repository at this point in the history
  • Loading branch information
bsian03 committed Mar 15, 2020
1 parent 5c72c97 commit e8e3ab3
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 221 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
43 changes: 14 additions & 29 deletions types/Core/Stores/ARegistry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AxonClient, Collection } from '../../';
import { AxonClient, Store } from '../../';

/**
* Abstract class to hold and manage a set of items.
Expand All @@ -7,12 +7,13 @@ import { AxonClient, Collection } from '../../';
*
* @abstract
* @class ARegistry
* @extends Store
*/
export declare class ARegistry<T> {
export declare class ARegistry<T> extends Store<T> {
/** The AxonClient */
private _axon: AxonClient;
/** The collection of items hold by the registry */
public registry: Collection<T>;
/** The base definition to use for the registry */
private _base: T
/**
* Creates an instance of ARegistry.
*
Expand All @@ -23,63 +24,47 @@ export declare class ARegistry<T> {
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<string, T>;
/**
* 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<T>;
public get(key: string): T;
/**
* Add an item to the registry
*
* @returns The registry
* @memberof ARegistry
*/
add(key: string, value: T): Collection<T>;
public add(key: string, value: T): ARegistry<T>;
/**
* 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
Expand Down
21 changes: 7 additions & 14 deletions types/Core/Stores/GuildConfigCache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AxonClient, LRUCache, GuildConfig } from '../../';
import { AxonClient, LRUCache, GuildConfig, Store } from '../../';

/**
* Handles GuildConfigs cache.
Expand All @@ -7,29 +7,22 @@ import { AxonClient, LRUCache, GuildConfig } from '../../';
* @author KhaaZ
*
* @class GuildConfigsCache
* @extends Store<GuildConfig>
*/
export declare class GuildConfigCache {
export declare class GuildConfigCache extends Store<GuildConfig> {
private _axon: AxonClient;
public guildConfigs: LRUCache<GuildConfig>;
/**
* Creates an instance of GuildConfigsCache.
*
* @memberof GuildConfigsCache
*/
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<GuildConfig>;
/**
* Get a GuildConfig from the cache or from the DB if not in the cache.
*
Expand Down
117 changes: 7 additions & 110 deletions types/Utility/Collection.ts
Original file line number Diff line number Diff line change
@@ -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<T>
*/
export declare class Collection<T> extends Map<string | number, T> {
export declare class Collection<T> extends Store<T> {
/** The base class for all items */
public baseObject: new (...args: any[] ) => T;
/**
* Creates an instance of Collection.
Expand Down Expand Up @@ -40,118 +41,14 @@ export declare class Collection<T> extends Map<string | number, T> {
*/
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<R>(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<U>(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<Name>]`;

/**
* 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<R>(key: string, func: 'from', args: [Array<R>, string] ): Collection<R>;
public apply(key: string, func: 'add', args: [string, T, boolean?] ): Collection<T>;
public apply(key: string, func: 'find' | 'filter', args: [(i: T) => boolean] ): Collection<T>;
public apply(key: string, func: 'random' | 'toArray' | 'toObject'): Collection<T>;
public apply<R>(key: string, func: 'map', args: [(i: T) => R] ): Collection<R>;
public apply<U>(key: string, func: 'reduce', args: [(accumulator: U, val: T) => U, U] ): Collection<U>;
public apply(key: string, func: 'update', args: [string, T] ): Collection<T>;
public apply(key: string, func: 'remove', args: [string] ): Collection<T | null>;
}
Loading

0 comments on commit e8e3ab3

Please sign in to comment.