mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 08:33:30 +01:00
feat: remove datastores and implement Managers (#3696)
* Initial commit: add 5 initial managers - Base manager - GuildChannelManager - MessageManager - PresenceManager - Reaction Manager - Added LimitedCollection * Add GuildEmojiManager, various fixes * Modify some managers and add guildmembermanager * Initial integration * Delete old stores * Integration part two, removed LRUCollection - Most of the integration has been finished - TODO typings - Removed LRUCollection, needless sweeping * Typings + stuff i somehow missed in ChannelManager * LimitedCollection typings/ final changes * Various jsdoc and syntactical fixes, Removed Util.mixin() * tslint fix * Grammatical and logical changes * Delete temporary file placed by mistake * Grammatical changes * Add missing type * Update jsdoc examples * fix: ChannelManager#remove should call cache#delete not cache#remove * fix recursive require * Fix missed cache in util * fix: more missed cache * Remove accidental _fetchMany change from #3645 * fix: use .cache.delete() over .remove() * fix: missing cache in ReactionCollector * fix: missed cache in client * fix: members is a collection not a manager Co-Authored-By: Sugden <28943913+NotSugden@users.noreply.github.com> * fix: various docs and cache fixes * fix: missed cache * fix: missing _roles * Final testing and debugging * LimitedCollection: return the Collection instead of undefined on .set * Add cache to BaseManager in typings * Commit fixes i forgot to stage yesterday * Update invite events * Account for new commit * fix: MessageReactionRemoveAll should call .cache.clear() * fix: add .cache at various places, correct return type * docs: remove mentions of 'store' * Add extra documented properties to typings Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com> Co-authored-by: SpaceEEC <spaceeec@yahoo.com>
This commit is contained in:
77
src/managers/BaseManager.js
Normal file
77
src/managers/BaseManager.js
Normal file
@@ -0,0 +1,77 @@
|
||||
'use strict';
|
||||
|
||||
const Collection = require('../util/Collection');
|
||||
let Structures;
|
||||
|
||||
/**
|
||||
* Manages the API methods of a data model and holds its cache.
|
||||
* @abstract
|
||||
*/
|
||||
class BaseManager {
|
||||
constructor(client, iterable, holds, cacheType = Collection, ...cacheOptions) {
|
||||
if (!Structures) Structures = require('../util/Structures');
|
||||
/**
|
||||
* The data structure belonging to this manager
|
||||
* @name BaseManager#holds
|
||||
* @type {Function}
|
||||
* @private
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(this, 'holds', { value: Structures.get(holds.name) || holds });
|
||||
|
||||
/**
|
||||
* The client that instantiated this Manager
|
||||
* @name BaseManager#client
|
||||
* @type {Client}
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(this, 'client', { value: client });
|
||||
|
||||
/**
|
||||
* The type of Collection of the Manager
|
||||
* @type {Collection}
|
||||
*/
|
||||
this.cacheType = cacheType;
|
||||
|
||||
/**
|
||||
* Holds the cache for the data model
|
||||
* @type {?Collection}
|
||||
*/
|
||||
this.cache = new cacheType(...cacheOptions);
|
||||
if (iterable) for (const i of iterable) this.add(i);
|
||||
}
|
||||
|
||||
add(data, cache = true, { id, extras = [] } = {}) {
|
||||
const existing = this.cache.get(id || data.id);
|
||||
if (existing && existing._patch && cache) existing._patch(data);
|
||||
if (existing) return existing;
|
||||
|
||||
const entry = this.holds ? new this.holds(this.client, data, ...extras) : data;
|
||||
if (cache) this.cache.set(id || entry.id, entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a data entry to a data Object.
|
||||
* @param {string|Object} idOrInstance The id or instance of something in this Manager
|
||||
* @returns {?Object} An instance from this Manager
|
||||
*/
|
||||
resolve(idOrInstance) {
|
||||
if (idOrInstance instanceof this.holds) return idOrInstance;
|
||||
if (typeof idOrInstance === 'string') return this.cache.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 Manager
|
||||
* @returns {?Snowflake}
|
||||
*/
|
||||
resolveID(idOrInstance) {
|
||||
if (idOrInstance instanceof this.holds) return idOrInstance.id;
|
||||
if (typeof idOrInstance === 'string') return idOrInstance;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BaseManager;
|
||||
Reference in New Issue
Block a user