mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 08:33:30 +01:00
Datastore cleanup (#1892)
* Start Store cleanup * wip store cleanup * fix iterables initiating datastores * handle the possibility of a datastore with no holds and no its own 'create' method * more cleanup (instances that need more than just client/data) * missed RoleStore extras * not sure how eslint didn't catch that tab... * avoid re-getting the channel we already have... * cleanup resolvers and refactor them into DataStores * ^ * and remove * fix some bugs * fix lint * fix documentation maybe? * formatting * fix presences * really fix presences this time * bad fix was bad... let;s see how bad this one is.. * forgot to save a file * make presence resolving take userresolveables too * fix tabs in jsdocs * fix bad fix from last night that caused issues, with better fix... * oops
This commit is contained in:
@@ -5,15 +5,45 @@ const Collection = require('../util/Collection');
|
||||
* @extends {Collection}
|
||||
*/
|
||||
class DataStore extends Collection {
|
||||
constructor(client, iterable) {
|
||||
constructor(client, iterable, holds) {
|
||||
super();
|
||||
Object.defineProperty(this, 'client', { value: client });
|
||||
Object.defineProperty(this, 'holds', { value: holds });
|
||||
if (iterable) for (const item of iterable) this.create(item);
|
||||
}
|
||||
|
||||
// Stubs
|
||||
create() { return undefined; }
|
||||
create(data, cache = true, { id, extras = [] } = {}) {
|
||||
const existing = this.get(id || data.id);
|
||||
if (existing) return existing;
|
||||
|
||||
const entry = this.holds ? new this.holds(this.client, data, ...extras) : data;
|
||||
if (cache) this.set(id || entry.id, entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
remove(key) { return this.delete(key); }
|
||||
|
||||
/**
|
||||
* Resolves a data entry to a data Object.
|
||||
* @param {string|Object} idOrInstance The id or instance of something in this datastore
|
||||
* @returns {?Object} An instance from this datastore
|
||||
*/
|
||||
resolve(idOrInstance) {
|
||||
if (idOrInstance instanceof this.holds) return idOrInstance;
|
||||
if (typeof idOrInstance === 'string') return this.get(idOrInstance) || null;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a data entry to a instance ID.
|
||||
* @param {string|Instance} idOrInstance The id or instance of something in this datastore
|
||||
* @returns {?string}
|
||||
*/
|
||||
resolveID(idOrInstance) {
|
||||
if (idOrInstance instanceof this.holds) return idOrInstance.id;
|
||||
if (typeof channel === 'string') return idOrInstance;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = DataStore;
|
||||
|
||||
Reference in New Issue
Block a user