buddy

node MVC discord bot
Log | Files | Refs | README

BaseManager.js (2295B)


      1 'use strict';
      2 
      3 const Collection = require('../util/Collection');
      4 let Structures;
      5 
      6 /**
      7  * Manages the API methods of a data model and holds its cache.
      8  * @abstract
      9  */
     10 class BaseManager {
     11   constructor(client, iterable, holds, cacheType = Collection, ...cacheOptions) {
     12     if (!Structures) Structures = require('../util/Structures');
     13     /**
     14      * The data structure belonging to this manager
     15      * @name BaseManager#holds
     16      * @type {Function}
     17      * @private
     18      * @readonly
     19      */
     20     Object.defineProperty(this, 'holds', { value: Structures.get(holds.name) || holds });
     21 
     22     /**
     23      * The client that instantiated this Manager
     24      * @name BaseManager#client
     25      * @type {Client}
     26      * @readonly
     27      */
     28     Object.defineProperty(this, 'client', { value: client });
     29 
     30     /**
     31      * The type of Collection of the Manager
     32      * @type {Collection}
     33      */
     34     this.cacheType = cacheType;
     35 
     36     /**
     37      * Holds the cache for the data model
     38      * @type {Collection}
     39      */
     40     this.cache = new cacheType(...cacheOptions);
     41     if (iterable) for (const i of iterable) this.add(i);
     42   }
     43 
     44   add(data, cache = true, { id, extras = [] } = {}) {
     45     const existing = this.cache.get(id || data.id);
     46     if (existing && existing._patch && cache) existing._patch(data);
     47     if (existing) return existing;
     48 
     49     const entry = this.holds ? new this.holds(this.client, data, ...extras) : data;
     50     if (cache) this.cache.set(id || entry.id, entry);
     51     return entry;
     52   }
     53 
     54   /**
     55    * Resolves a data entry to a data Object.
     56    * @param {string|Object} idOrInstance The id or instance of something in this Manager
     57    * @returns {?Object} An instance from this Manager
     58    */
     59   resolve(idOrInstance) {
     60     if (idOrInstance instanceof this.holds) return idOrInstance;
     61     if (typeof idOrInstance === 'string') return this.cache.get(idOrInstance) || null;
     62     return null;
     63   }
     64 
     65   /**
     66    * Resolves a data entry to a instance ID.
     67    * @param {string|Object} idOrInstance The id or instance of something in this Manager
     68    * @returns {?Snowflake}
     69    */
     70   resolveID(idOrInstance) {
     71     if (idOrInstance instanceof this.holds) return idOrInstance.id;
     72     if (typeof idOrInstance === 'string') return idOrInstance;
     73     return null;
     74   }
     75 
     76   valueOf() {
     77     return this.cache;
     78   }
     79 }
     80 
     81 module.exports = BaseManager;