harmony/src/managers/base.ts

95 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-04-04 09:29:56 +00:00
import type { Client } from '../client/mod.ts'
2021-04-30 12:37:01 +00:00
import { Base } from '../structures/base.ts'
2020-11-08 07:57:24 +00:00
import { Collection } from '../utils/collection.ts'
2020-12-03 04:06:41 +00:00
/**
* Managers handle caching data. And also some REST Methods as required.
*
* You should not be making Managers yourself.
*/
2021-04-30 12:37:01 +00:00
export class BaseManager<T, T2> extends Base {
/** Caches Name or Key used to differentiate caches */
cacheName: string
/** Which data type does this cache have */
DataType: any
2020-12-02 12:29:52 +00:00
constructor(client: Client, cacheName: string, DataType: any) {
2021-04-30 12:37:01 +00:00
super(client)
this.cacheName = cacheName
this.DataType = DataType
}
/** Gets raw value from a cache (payload) */
2020-12-02 12:29:52 +00:00
async _get(key: string): Promise<T | undefined> {
return this.client.cache.get(this.cacheName, key)
}
/** Gets a value from Cache */
2020-12-02 12:29:52 +00:00
async get(key: string): Promise<T2 | undefined> {
const raw = await this._get(key)
if (raw === undefined) return
return new this.DataType(this.client, raw)
}
/** Sets a value to Cache */
2020-12-02 12:29:52 +00:00
async set(key: string, value: T): Promise<any> {
return this.client.cache.set(this.cacheName, key, value)
}
/** Deletes a key from Cache */
2020-12-28 12:35:08 +00:00
async _delete(key: string): Promise<boolean> {
return this.client.cache.delete(this.cacheName, key)
}
/** Gets an Array of values from Cache */
2020-12-19 07:58:40 +00:00
async array(): Promise<T2[]> {
2020-11-25 11:53:40 +00:00
let arr = await (this.client.cache.array(this.cacheName) as T[])
if (arr === undefined) arr = []
2020-12-02 12:29:52 +00:00
return arr.map((e) => new this.DataType(this.client, e)) as any
}
/** Gets a Collection of values from Cache */
2020-12-02 12:29:52 +00:00
async collection(): Promise<Collection<string, T2>> {
2020-11-03 09:21:29 +00:00
const arr = await this.array()
2020-11-03 15:19:20 +00:00
if (arr === undefined) return new Collection()
2020-11-03 09:21:29 +00:00
const collection = new Collection()
for (const elem of arr) {
2020-11-03 09:21:29 +00:00
// @ts-expect-error
collection.set(elem.id, elem)
}
return collection
}
async *[Symbol.asyncIterator](): AsyncIterableIterator<T2> {
const arr = (await this.array()) ?? []
const { readable, writable } = new TransformStream()
2021-04-04 13:46:34 +00:00
const writer = writable.getWriter()
arr.forEach((el: unknown) => writer.write(el))
writer.close()
2021-03-19 11:54:48 +00:00
yield* readable
}
2021-03-19 11:37:57 +00:00
async fetch(...args: unknown[]): Promise<T2 | undefined> {
return undefined
}
/** Try to get value from cache, if not found then fetch */
async resolve(key: string): Promise<T2 | undefined> {
const cacheValue = await this.get(key)
if (cacheValue !== undefined) return cacheValue
else {
const fetchValue = await this.fetch(key).catch(() => undefined)
if (fetchValue !== undefined) return fetchValue
}
}
/** Deletes everything from Cache */
2020-12-02 12:29:52 +00:00
flush(): any {
return this.client.cache.deleteCache(this.cacheName)
}
2021-04-30 12:37:01 +00:00
[Deno.customInspect](): string {
return `Manager(${this.cacheName})`
}
}