summaryrefslogtreecommitdiff
path: root/includes/external/addressbook/node_modules/quick-lru/index.js
diff options
context:
space:
mode:
authorRaindropsSys <contact@minteck.org>2023-04-06 22:18:28 +0200
committerRaindropsSys <contact@minteck.org>2023-04-06 22:18:28 +0200
commit83354b2b88218090988dd6e526b0a2505b57e0f1 (patch)
treee3c73c38a122a78bb7e66fbb99056407edd9d4b9 /includes/external/addressbook/node_modules/quick-lru/index.js
parent47b8f2299a483024c4a6a8876af825a010954caa (diff)
downloadpluralconnect-83354b2b88218090988dd6e526b0a2505b57e0f1.tar.gz
pluralconnect-83354b2b88218090988dd6e526b0a2505b57e0f1.tar.bz2
pluralconnect-83354b2b88218090988dd6e526b0a2505b57e0f1.zip
Updated 5 files and added 1110 files (automated)
Diffstat (limited to 'includes/external/addressbook/node_modules/quick-lru/index.js')
-rw-r--r--includes/external/addressbook/node_modules/quick-lru/index.js123
1 files changed, 123 insertions, 0 deletions
diff --git a/includes/external/addressbook/node_modules/quick-lru/index.js b/includes/external/addressbook/node_modules/quick-lru/index.js
new file mode 100644
index 0000000..7d7032e
--- /dev/null
+++ b/includes/external/addressbook/node_modules/quick-lru/index.js
@@ -0,0 +1,123 @@
+'use strict';
+
+class QuickLRU {
+ constructor(options = {}) {
+ if (!(options.maxSize && options.maxSize > 0)) {
+ throw new TypeError('`maxSize` must be a number greater than 0');
+ }
+
+ this.maxSize = options.maxSize;
+ this.onEviction = options.onEviction;
+ this.cache = new Map();
+ this.oldCache = new Map();
+ this._size = 0;
+ }
+
+ _set(key, value) {
+ this.cache.set(key, value);
+ this._size++;
+
+ if (this._size >= this.maxSize) {
+ this._size = 0;
+
+ if (typeof this.onEviction === 'function') {
+ for (const [key, value] of this.oldCache.entries()) {
+ this.onEviction(key, value);
+ }
+ }
+
+ this.oldCache = this.cache;
+ this.cache = new Map();
+ }
+ }
+
+ get(key) {
+ if (this.cache.has(key)) {
+ return this.cache.get(key);
+ }
+
+ if (this.oldCache.has(key)) {
+ const value = this.oldCache.get(key);
+ this.oldCache.delete(key);
+ this._set(key, value);
+ return value;
+ }
+ }
+
+ set(key, value) {
+ if (this.cache.has(key)) {
+ this.cache.set(key, value);
+ } else {
+ this._set(key, value);
+ }
+
+ return this;
+ }
+
+ has(key) {
+ return this.cache.has(key) || this.oldCache.has(key);
+ }
+
+ peek(key) {
+ if (this.cache.has(key)) {
+ return this.cache.get(key);
+ }
+
+ if (this.oldCache.has(key)) {
+ return this.oldCache.get(key);
+ }
+ }
+
+ delete(key) {
+ const deleted = this.cache.delete(key);
+ if (deleted) {
+ this._size--;
+ }
+
+ return this.oldCache.delete(key) || deleted;
+ }
+
+ clear() {
+ this.cache.clear();
+ this.oldCache.clear();
+ this._size = 0;
+ }
+
+ * keys() {
+ for (const [key] of this) {
+ yield key;
+ }
+ }
+
+ * values() {
+ for (const [, value] of this) {
+ yield value;
+ }
+ }
+
+ * [Symbol.iterator]() {
+ for (const item of this.cache) {
+ yield item;
+ }
+
+ for (const item of this.oldCache) {
+ const [key] = item;
+ if (!this.cache.has(key)) {
+ yield item;
+ }
+ }
+ }
+
+ get size() {
+ let oldCacheSize = 0;
+ for (const key of this.oldCache.keys()) {
+ if (!this.cache.has(key)) {
+ oldCacheSize++;
+ }
+ }
+
+ return Math.min(this._size + oldCacheSize, this.maxSize);
+ }
+}
+
+module.exports = QuickLRU;