summaryrefslogtreecommitdiff
path: root/node_modules/discord.js/src/managers/BaseManager.js
diff options
context:
space:
mode:
author8cy <[email protected]>2020-04-30 15:46:16 -0700
committer8cy <[email protected]>2020-04-30 15:46:16 -0700
commit3a4deac89054021b56ad5bd8005b2044cc085c98 (patch)
tree3dd6af8503e497e46180b6b5231674f36bdce9f2 /node_modules/discord.js/src/managers/BaseManager.js
downloaduppity-3a4deac89054021b56ad5bd8005b2044cc085c98.tar.xz
uppity-3a4deac89054021b56ad5bd8005b2044cc085c98.zip
Up, up, uppity.
Diffstat (limited to 'node_modules/discord.js/src/managers/BaseManager.js')
-rw-r--r--node_modules/discord.js/src/managers/BaseManager.js81
1 files changed, 81 insertions, 0 deletions
diff --git a/node_modules/discord.js/src/managers/BaseManager.js b/node_modules/discord.js/src/managers/BaseManager.js
new file mode 100644
index 0000000..c11d191
--- /dev/null
+++ b/node_modules/discord.js/src/managers/BaseManager.js
@@ -0,0 +1,81 @@
+'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|Object} 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;
+ }
+
+ valueOf() {
+ return this.cache;
+ }
+}
+
+module.exports = BaseManager;