2021-04-04 09:29:56 +00:00
|
|
|
import type { Client } from '../client/mod.ts'
|
2020-11-08 07:57:24 +00:00
|
|
|
import { Collection } from '../utils/collection.ts'
|
2020-10-31 11:45:33 +00:00
|
|
|
|
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.
|
|
|
|
*/
|
2020-10-31 11:45:33 +00:00
|
|
|
export class BaseManager<T, T2> {
|
|
|
|
client: Client
|
2020-12-05 02:24:08 +00:00
|
|
|
/** Caches Name or Key used to differentiate caches */
|
2020-10-31 11:45:33 +00:00
|
|
|
cacheName: string
|
2020-11-07 13:05:37 +00:00
|
|
|
/** Which data type does this cache have */
|
2020-11-02 06:58:23 +00:00
|
|
|
DataType: any
|
2020-10-31 11:45:33 +00:00
|
|
|
|
2020-12-02 12:29:52 +00:00
|
|
|
constructor(client: Client, cacheName: string, DataType: any) {
|
2020-10-31 11:45:33 +00:00
|
|
|
this.client = client
|
|
|
|
this.cacheName = cacheName
|
2020-11-02 06:58:23 +00:00
|
|
|
this.DataType = DataType
|
2020-10-31 11:45:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 02:24:08 +00:00
|
|
|
/** Gets raw value from a cache (payload) */
|
2020-12-02 12:29:52 +00:00
|
|
|
async _get(key: string): Promise<T | undefined> {
|
2020-11-02 06:58:23 +00:00
|
|
|
return this.client.cache.get(this.cacheName, key)
|
2020-10-31 11:45:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 02:24:08 +00:00
|
|
|
/** Gets a value from Cache */
|
2020-12-02 12:29:52 +00:00
|
|
|
async get(key: string): Promise<T2 | undefined> {
|
2020-11-01 11:22:09 +00:00
|
|
|
const raw = await this._get(key)
|
2020-11-02 06:58:23 +00:00
|
|
|
if (raw === undefined) return
|
|
|
|
return new this.DataType(this.client, raw)
|
2020-10-31 11:45:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 02:24:08 +00:00
|
|
|
/** Sets a value to Cache */
|
2020-12-02 12:29:52 +00:00
|
|
|
async set(key: string, value: T): Promise<any> {
|
2020-10-31 11:45:33 +00:00
|
|
|
return this.client.cache.set(this.cacheName, key, value)
|
|
|
|
}
|
|
|
|
|
2020-12-05 02:24:08 +00:00
|
|
|
/** Deletes a key from Cache */
|
2020-12-28 12:35:08 +00:00
|
|
|
async _delete(key: string): Promise<boolean> {
|
2020-10-31 11:45:33 +00:00
|
|
|
return this.client.cache.delete(this.cacheName, key)
|
|
|
|
}
|
2020-11-01 13:42:00 +00:00
|
|
|
|
2020-12-05 02:24:08 +00:00
|
|
|
/** 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
|
2020-11-01 13:42:00 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 02:24:08 +00:00
|
|
|
/** 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()
|
2020-11-01 13:42:00 +00:00
|
|
|
for (const elem of arr) {
|
2020-11-03 09:21:29 +00:00
|
|
|
// @ts-expect-error
|
2020-11-01 13:42:00 +00:00
|
|
|
collection.set(elem.id, elem)
|
|
|
|
}
|
|
|
|
return collection
|
|
|
|
}
|
2020-11-02 12:08:38 +00:00
|
|
|
|
2021-02-25 10:45:13 +00:00
|
|
|
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-02-25 10:45:13 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-05 02:24:08 +00:00
|
|
|
/** Deletes everything from Cache */
|
2020-12-02 12:29:52 +00:00
|
|
|
flush(): any {
|
2020-11-02 12:08:38 +00:00
|
|
|
return this.client.cache.deleteCache(this.cacheName)
|
|
|
|
}
|
2020-11-02 06:58:23 +00:00
|
|
|
}
|