From 815e0f4bf87a4e39b29215542ecdba8c3c7c4f3f Mon Sep 17 00:00:00 2001 From: Yuzu Date: Wed, 13 Jul 2022 10:14:43 -0500 Subject: [PATCH] fix(gateway): remove Collection --- .../gateway/manager/shardManager.ts | 3 +- packages/discordeno/util/collection.ts | 100 ------------------ 2 files changed, 1 insertion(+), 102 deletions(-) delete mode 100644 packages/discordeno/util/collection.ts diff --git a/packages/discordeno/gateway/manager/shardManager.ts b/packages/discordeno/gateway/manager/shardManager.ts index 91a18f5..e621530 100644 --- a/packages/discordeno/gateway/manager/shardManager.ts +++ b/packages/discordeno/gateway/manager/shardManager.ts @@ -1,6 +1,5 @@ import { DiscordGatewayPayload } from "../../types/discord.ts"; import { PickPartial } from "../../types/shared.ts"; -import { Collection } from "../../util/collection.ts"; import { CreateShard, createShard } from "../shard/createShard.ts"; import { Shard, ShardGatewayConfig } from "../shard/types.ts"; @@ -33,7 +32,7 @@ export function createShardManager(options: CreateShardManager) { /** Gateway configuration which is used when creating a Shard. */ gatewayConfig: options.gatewayConfig, /** Managed Shards. */ - shards: new Collection( + shards: new Map( options.shardIds.map((shardId) => { const shard = createShard({ ...options.createShardOptions, diff --git a/packages/discordeno/util/collection.ts b/packages/discordeno/util/collection.ts deleted file mode 100644 index 16193b8..0000000 --- a/packages/discordeno/util/collection.ts +++ /dev/null @@ -1,100 +0,0 @@ -export class Collection extends Map { - maxSize: number | undefined; - - constructor(entries?: (readonly (readonly [K, V])[] | null) | Map, options?: CollectionOptions) { - super(entries ?? []); - - this.maxSize = options?.maxSize; - } - - set(key: K, value: V) { - // When this collection is maxSized make sure we can add first - if ((this.maxSize || this.maxSize === 0) && this.size >= this.maxSize) { - return this; - } - - return super.set(key, value); - } - - forceSet(key: K, value: V) { - return super.set(key, value); - } - - array() { - return [...this.values()]; - } - - /** Retrieve the value of the first element in this collection */ - first(): V | undefined { - return this.values().next().value; - } - - last(): V | undefined { - return [...this.values()][this.size - 1]; - } - - random(): V | undefined { - const array = [...this.values()]; - return array[Math.floor(Math.random() * array.length)]; - } - - find(callback: (value: V, key: K) => boolean) { - for (const key of this.keys()) { - const value = this.get(key)!; - if (callback(value, key)) return value; - } - // If nothing matched - return; - } - - filter(callback: (value: V, key: K) => boolean) { - const relevant = new Collection(); - this.forEach((value, key) => { - if (callback(value, key)) relevant.set(key, value); - }); - - return relevant; - } - - map(callback: (value: V, key: K) => T) { - const results = []; - for (const key of this.keys()) { - const value = this.get(key)!; - results.push(callback(value, key)); - } - return results; - } - - some(callback: (value: V, key: K) => boolean) { - for (const key of this.keys()) { - const value = this.get(key)!; - if (callback(value, key)) return true; - } - - return false; - } - - every(callback: (value: V, key: K) => boolean) { - for (const key of this.keys()) { - const value = this.get(key)!; - if (!callback(value, key)) return false; - } - - return true; - } - - reduce(callback: (accumulator: T, value: V, key: K) => T, initialValue?: T): T { - let accumulator: T = initialValue!; - - for (const key of this.keys()) { - const value = this.get(key)!; - accumulator = callback(accumulator, value, key); - } - - return accumulator; - } -} - -export interface CollectionOptions { - maxSize?: number; -}