UNPKG

@confis/discordapiwrapper

Version:

A fast and lightweight discord api wrapper.

121 lines (113 loc) 5.3 kB
/****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */ function __classPrivateFieldGet(receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; var _Manager_cache; /** * Manages a cache of objects that extend the Base class. */ class Manager { constructor() { _Manager_cache.set(this, []); } get length() { return __classPrivateFieldGet(this, _Manager_cache, "f").length; } get array() { return __classPrivateFieldGet(this, _Manager_cache, "f"); } /** * Gets the element at the specified index. * @param index The index of the element to get. * @returns The element at the specified index, or null if the index is out of bounds. */ getByIndex(index) { return __classPrivateFieldGet(this, _Manager_cache, "f")[index]; } /** * Returns the elements of an array that meet the condition specified in a callback function. * * @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the cache. */ filter(predicate) { return __classPrivateFieldGet(this, _Manager_cache, "f").filter(predicate); } /** * Adds a new element to the cache or multiple elements if an array is provided. * @param data The element(s) to add to the cache. * @returns The added element. */ cache(data) { if (!Array.isArray(data)) { if (__classPrivateFieldGet(this, _Manager_cache, "f").some(a => a.id === data.id)) { const index = __classPrivateFieldGet(this, _Manager_cache, "f").findIndex((a) => a.id === data.id); __classPrivateFieldGet(this, _Manager_cache, "f").splice(index, 1); __classPrivateFieldGet(this, _Manager_cache, "f").splice(index, 0, data); } else { __classPrivateFieldGet(this, _Manager_cache, "f").push(data); } return __classPrivateFieldGet(this, _Manager_cache, "f").find((a) => a.id === data.id); } else { for (const part of data) { if (__classPrivateFieldGet(this, _Manager_cache, "f").some(a => a.id === part.id)) { const index = __classPrivateFieldGet(this, _Manager_cache, "f").findIndex((a) => a.id === part.id); __classPrivateFieldGet(this, _Manager_cache, "f").splice(index, 1); __classPrivateFieldGet(this, _Manager_cache, "f").splice(index, 0, part); } else { __classPrivateFieldGet(this, _Manager_cache, "f").push(part); } } } } /** * Removes the element with the specified ID from the cache. * @param id The ID of the element to remove. */ delete(id) { const index = __classPrivateFieldGet(this, _Manager_cache, "f").findIndex((a) => a.id === id); if (index > -1) __classPrivateFieldGet(this, _Manager_cache, "f").splice(index, 1); } /** * Gets the element with the specified ID from the cache. * @param id The ID of the element to get. * @returns The element with the specified ID, or undefined if the element is not found. */ get(id) { return __classPrivateFieldGet(this, _Manager_cache, "f").find((a) => a.id === id); } /** * Updates the element with the specified ID in the cache. * @param id The ID of the element to update. * @param data The data to update the element with. */ update(id, data) { const index = __classPrivateFieldGet(this, _Manager_cache, "f").findIndex((a) => a.id === id); if (index > -1) { __classPrivateFieldGet(this, _Manager_cache, "f")[index]._patch(data); } } } _Manager_cache = new WeakMap(); exports.Manager = Manager;