const Collection = require("../util/Collection");
class DataStore extends Collection {
constructor(thunderAPI, iterable, holds) {
super();
Object.defineProperty(this, "thunderAPI", { value: thunderAPI });
Object.defineProperty(this, "holds", { value: holds });
if (iterable) for (const item of iterable) this.add(item);
}
add(data, cache = true, { name, extras = [] } = {}) {
const existing = this.get(name || data.name);
if (existing) return existing;
const entry = this.holds ? new this.holds(this.thunderAPI, data, ...extras) : data; // eslint-disable-line new-cap
if (cache) this.set(name || entry.id, entry);
return entry;
}
remove(key) { return this.delete(key); }
/**
* Resolves a data entry to a data Object.
* @param {string|Object} nameOrInstance The id or instance of something in this DataStore
* @returns {?Object} An instance from this DataStore
*/
resolve(nameOrInstance) {
if (nameOrInstance instanceof this.holds) return nameOrInstance;
if (typeof nameOrInstance === "string") return this.get(nameOrInstance) || null;
return null;
}
}
module.exports = DataStore;