All files LruCache.js

98.12% Statements 261/266
85.22% Branches 98/115
98.48% Functions 65/66
98.45% Lines 254/258
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654        1x   1x 1x 1x 1x   1x 13x 10x     3x 3x 3x 3x 2x 2x   3x         1x 13x 10x     3x 3x 3x         1x 19x 19x 19x 17x 17x 4x 3x       19x 15x   19x     1x 96x 20x   76x 76x                                                         1x 12x 12x 12x         12x 12x     12x 1x 1x   12x 1x 1x   12x 12x 12x 12x       1x 19x 19x 19x 19x 18x 18x 18x     1x     18x     19x 1x 1x 1x   1x 1x 1x         1x 1x 1x                   1x 26x 19x       26x 26x 1x 1x 1x 1x 1x 1x 1x         25x 25x     25x 25x 18x 18x 18x 18x           1x 33x 33x 33x         33x     16x     17x 17x     17x 51x     33x         1x   1x 63x 25x       1x 41x 3x       1x 3x 3x       1x 6x 2x       6x 3x 3x 3x 3x               1x 96x 23x     73x 73x 73x     73x         1x 48x 1x   47x 47x 64x 64x 64x 32x   64x 46x 1x     63x 63x 55x             8x 8x   63x 63x 45x   63x 63x 2x 2x   2x 2x                             3x 3x 3x 3x 3x               3x 4x                 3x 4x                         3x 48x                 3x                     3x 30x                 3x   3x 3x 3x   50x 47x 47x 12x     50x 50x 3x   50x 20x 12x 5x   7x 7x     7x 3x 3x 2x   1x   1x   1x   3x   3x 3x     4x 4x     8x 1x     7x     30x                     3x 10x                                             50x                             3x               3x 3x 1x     2x                       3x 6x 6x 2x 2x   2x   4x 4x 4x       4x 4x   4x               3x             3x               3x 41x 41x 41x 32x 32x 41x                     3x             3x             3x                 3x 11x 11x 11x 1x 1x   1x 1x           3x       1x                       1x 47x 47x 3x 3x   47x                   1x 1x 1x 3x        
import {LruMap} from "./LruMap";
 
/** @namespace @swarmy/lru-cache */
 
const DEFAULT_MAX_SIZE = 500;
 
let nextHandlerKey = 0;
const keyToChangedHandler = new Map();
const valueTypeToActiveChangeHandlerKeys = new Map();
const allTypesActiveChangeHandlerKeys = new Set();
 
const addToActiveHandlerKeys = (key, valueTypes) => {
  if (valueTypes === null || (Array.isArray(valueTypes) && valueTypes.length === 0)) {
    allTypesActiveChangeHandlerKeys.add(key);
  }
  else {
    const types = Array.isArray(valueTypes) ? valueTypes : [valueTypes];
    types.forEach(valueType => {
      let activeChangeHandlerKeys = valueTypeToActiveChangeHandlerKeys.get(valueType);
      if (typeof activeChangeHandlerKeys === "undefined") {
        activeChangeHandlerKeys = new Set();
        valueTypeToActiveChangeHandlerKeys.set(valueType, activeChangeHandlerKeys);
      }
      activeChangeHandlerKeys.add(key);
    });
  }
};
 
const removeFromActiveHandlerKeys = (key, valueTypes) => {
  if (valueTypes === null || (Array.isArray(valueTypes) && valueTypes.length === 0)) {
    allTypesActiveChangeHandlerKeys.delete(key);
  }
  else {
    const types = Array.isArray(valueTypes) ? valueTypes : [valueTypes];
    types.forEach(valueType => {
      valueTypeToActiveChangeHandlerKeys.get(valueType).delete(key);
    });
  }
};
 
const getActiveHandlerKeys = valueTypes => {
  const types = typeof valueTypes === "string" ? [valueTypes] : valueTypes;
  const result = new Set();
  types.forEach(valueType => {
    const activeChangeHandlerKeys = valueTypeToActiveChangeHandlerKeys.get(valueType);
    if (typeof activeChangeHandlerKeys !== "undefined") {
      activeChangeHandlerKeys.forEach(key => {
        result.add(key);
      });
    }
  });
  allTypesActiveChangeHandlerKeys.forEach(key => {
    result.add(key);
  });
  return result;
};
 
const hasActiveHandler = valueType => {
  if (allTypesActiveChangeHandlerKeys.size > 0) {
    return true;
  }
  const activeChangeHandlerKeys = valueTypeToActiveChangeHandlerKeys.get(valueType);
  return typeof activeChangeHandlerKeys === "undefined" ? false : activeChangeHandlerKeys.size > 0;
};
 
/**
 * Register a handler that is called when value(s) get updated/inserted/removed in/to/from a cache.
 * If the cache has already exceeded its maxSize, there is no way to know if a cache.set (or setAll) is an insert or
 * an update (because JS does not yet provide weak references), so an insert event can be insert or update.
 * The returned handle can be used to unregister the handler, or to deactivate/activate it (initial state is active).
 * @memberof @swarmy/lru-cache
 * @function
 * @param {function} changedHandler - A function that will be called with an object parameter of the following shape:
 *                   {
 *                     valueTypes: Set(),
 *                     <valueType>: {
 *                       inserts: [{key, value, alternateKeys, order}],
 *                       clearRemoves: [{key, value, alternateKeys, order}],
 *                       lruRemoves: [{key, value, alternateKeys, order}],
 *                       deleteRemoves: [{key}],
 *                     },
 *                     ...
 *                   }
 *                   The order can be used to determine e.g. if an entry was first inserted and then deleted, or first
 *                   deleted and then re-inserted (can happen in cache transactions).
 * @param {Array | string} valueTypes - An array or a single string specifying the cache value types the handler should be called for (default: null)
 *                 If null, it will be called for all object types
 *                 If not null and a bulk (transactional) change has multiple valueTypes of which only some are of
 *                 interest for the handler, then also the other types will be part of the argument (if at least one other active listener for the other types exist)
 * @return {object} handlerHandle - An object with methods unregister, activate, deactivate and isRegistered and with fields isActive, valueType and changedHandler
 */
export const registerCacheChangedHandler = (changedHandler, valueTypes = null) => {
  const key = nextHandlerKey;
  nextHandlerKey += 1;
  const handler = {
    changedHandler,
    valueTypes,
    isActive: true,
    unregister: () => {
      keyToChangedHandler.delete(key);
      removeFromActiveHandlerKeys(key, valueTypes);
    },
  };
  handler.activate = () => {
    handler.isActive = true;
    addToActiveHandlerKeys(key, valueTypes);
  };
  handler.deactivate = () => {
    handler.isActive = false;
    removeFromActiveHandlerKeys(key, valueTypes);
  };
  handler.isRegistered = () => keyToChangedHandler.has(key);
  keyToChangedHandler.set(key, handler);
  addToActiveHandlerKeys(key, valueTypes);
  return handler;
};
 
 
const handleTransactionChangeObject = changeObject => {
  const activeHandlerKeys = getActiveHandlerKeys(changeObject.valueTypes);
  const errors = [];
  let handled = 0;
  activeHandlerKeys.forEach(key => {
    const handler = keyToChangedHandler.get(key);
    try {
      handler.changedHandler(changeObject);
    }
    catch (error) {
      errors.push(error);
    }
    finally {
      handled += 1;
    }
  });
  if (errors.length > 0) {
    let message = "handleTransactionChangeObject: " + String(errors.length) + " of " + String(handled) + " handlers threw an error: ";
    errors.forEach(error => {
      message += error.message + ", ";
    });
    const error = new Error(message);
    error.errors = errors;
    throw error;
  }
};
 
 
let transactionChangeObject = null;
let changeOrder = 0;
let runningTransactions = 0;
 
/** Pass a callback or a promise. All cache changes happening inside the callback or promise will be batched into a single
 *  change object that will be dispatched to handlers after the callback/promise has finished. If this is called while there
 *  is already another transaction in progress, the two transactions will just be batched together.
 * @memberof @swarmy/lru-cache
 * @function
 * @param {function | Promise} callbackOrPromise - callback or promise to be executed within the transaction
 * @return {undefined} void
 */
export const cacheTransaction = callbackOrPromise => {
  if (transactionChangeObject === null) {
    transactionChangeObject = {
      valueTypes: new Set(),
    };
  }
  runningTransactions += 1;
  if (typeof callbackOrPromise.finally === "function") {
    callbackOrPromise.finally(() => {
      runningTransactions -= 1;
      Eif (runningTransactions === 0) {
        const changeObject = transactionChangeObject;
        transactionChangeObject = null;
        changeOrder = 0;
        handleTransactionChangeObject(changeObject);
      }
    });
  }
  else {
    try {
      callbackOrPromise();
    }
    finally {
      runningTransactions -= 1;
      if (runningTransactions === 0) {
        const changeObject = transactionChangeObject;
        transactionChangeObject = null;
        changeOrder = 0;
        handleTransactionChangeObject(changeObject);
      }
    }
  }
};
 
const handleChange = (valueType, keyValueAlternateKeys, fieldNameAdd, fieldNamesUnchanged) => {
  let changeObject = transactionChangeObject;
  const batchChanges = changeObject !== null;
  Iif (changeObject === null) {
    changeObject = {
      valueTypes: new Set(),
    };
  }
  if (changeObject.valueTypes.has(valueType)) {
    // Copying the original entry is not just done to add the order, but is mandatory to get the value
    // at the point of change and not the current cache value in the change event!
    changeObject[valueType][fieldNameAdd].push({...keyValueAlternateKeys, order: changeOrder++});
  }
  else {
    changeObject.valueTypes.add(valueType);
    changeObject[valueType] = {
      [fieldNameAdd]: [{...keyValueAlternateKeys, order: changeOrder++}],
    };
    fieldNamesUnchanged.forEach(fieldName => {
      changeObject[valueType][fieldName] = [];
    })
  }
  Iif (!batchChanges) {
    handleTransactionChangeObject(changeObject);
  }
};
 
let handleChanges = true;
 
const handleInsert = (valueType, keyValueAlternateKeys) => {
  if (handleChanges) {
    handleChange(valueType, keyValueAlternateKeys, "inserts", ["clearRemoves", "lruRemoves", "deleteRemoves"]);
  }
};
 
const handleClearRemove = (valueType, keyValueAlternateKeys) => {
  if (handleChanges) {
    handleChange(valueType, keyValueAlternateKeys, "clearRemoves", ["inserts", "lruRemoves", "deleteRemoves"]);
  }
};
 
const handleLruRemove = (valueType, keyValueAlternateKeys) => {
  Eif (handleChanges) {
    handleChange(valueType, keyValueAlternateKeys, "lruRemoves", ["clearRemoves", "inserts", "deleteRemoves"]);
  }
};
 
const handleDeleteRemove = (valueType, keyValueAlternateKeys) => {
  if (handleChanges) {
    handleChange(valueType, keyValueAlternateKeys, "deleteRemoves", ["clearRemoves", "lruRemoves", "inserts"]);
  }
};
 
const asyncWrap = syncFunction => (...args) => new Promise((resolve, reject) => {
  setTimeout(() => {
    try {
      const result = syncFunction(...args);
      resolve(result);
    }
    catch (e) {
      reject(e);
    }
  }, 0);
});
 
const wrapInTransaction = (valueType, transactionCallback) => {
  if (hasActiveHandler(valueType)) {
    cacheTransaction(transactionCallback);
  }
  else {
    try {
      handleChanges = false;
      transactionCallback();
    }
    finally {
      handleChanges = true;
    }
  }
};
 
const setAll = (valueType, lruMap, alternateKeyToKey, keyValueAlternateKeysArray, dispatchLruRemoves) => {
  if (!Array.isArray(keyValueAlternateKeysArray)) {
    throw new Error("LruCache::setAll: keyValueAlternateKeysArray must be an array");
  }
  wrapInTransaction(valueType, () => {
    keyValueAlternateKeysArray.forEach(({key, value, alternateKeys}) => {
      let entry = lruMap.getWithoutLruChange(key);
      let altKeys = Array.isArray(alternateKeys) ? alternateKeys : [];
      if (altKeys.length === 0 && typeof alternateKeys === "string") {
        altKeys = [alternateKeys];
      }
      altKeys.forEach(altKey => {
        if (alternateKeyToKey.has(altKey) && alternateKeyToKey.get(altKey) !== key) {
          throw new Error("LruCache::setAll: alternate key '" + altKey + "' is given for key '" + key + "' and value type '" + valueType + "' but is already used for key '" + alternateKeyToKey.get(altKey) + "'");
        }
      });
      altKeys = new Set(altKeys);
      if (typeof entry === "undefined") {
        entry = {
          key,
          value,
          alternateKeys: altKeys,
        };
      }
      else {
        entry.value = value;
        entry.alternateKeys = new Set([...entry.alternateKeys, ...altKeys]);
      }
      const removed = lruMap.set(key, entry);
      altKeys.forEach(altKey => {
        alternateKeyToKey.set(altKey, key);
      });
      handleInsert(valueType, entry);
      if (removed !== null) {
        removed.value.alternateKeys.forEach(altKey => {
          alternateKeyToKey.delete(altKey);
        });
        Eif (dispatchLruRemoves) {
          handleLruRemove(valueType, removed.value);
        }
      }
    });
  });
};
 
/** Cannot be instantiated directly! Use 'getCache' to get a cache instance.
 *  By default, cache events are dispatched only for inserts/updates and deletes.
 *  To dispatch also LRU removes and/or clear removes, use the corresponding setters.
 * @class LruCache
 * @param {string} valueType - The value type of this cache
 * @param {maxSize} maxSize - The maximum number of entries for the given value type (default: 500)
 */
function LruCache(valueType, maxSize = DEFAULT_MAX_SIZE) {
  const self = this instanceof LruCache ? this : Object.create(LruCache.prototype);
  const lruMap = LruMap(maxSize);
  const alternateKeyToKey = new Map();
  let dispatchLruRemoves = false;
  let dispatchClearRemoves = false;
 
  /** Set whether the cache should also dispatch events for LRU removes
   * @memberof LruCache
   * @function
   * @param {boolean} newValue - true, if LRU removes should be dispatched
   * @returns {undefined} void
   */
  self.dispatchLruRemoves = newValue => {
    dispatchLruRemoves = newValue;
  };
 
  /** Set whether the cache should also dispatch events for clear removes
   * @memberof LruCache
   * @function
   * @param {boolean} newValue - true, if clear removes should be dispatched
   * @returns {undefined} void
   */
  self.dispatchClearRemoves = newValue => {
    dispatchClearRemoves = newValue;
  };
 
  /** Insert or update multiple cache entries.
   *  If alternate keys are provided and an already existing entry already has alternate keys, these will be extended.
   *  A corresponding cache changed event will be dispatched.
   *  If inserts lead to cache max size being exceeded and dispatchLruRemoves is set to true, the cache change event will contain both, inserts and removes.
   *  It is even possible that an entry from the inserts is also contained in the removes.
   * @memberof LruCache
   * @function
   * @param {Array} keyValueAlternateKeysArray - array of objects with 'key', 'value' and optional 'alternateKeys'
   * @returns {undefined} void
   */
  self.setAll = keyValueAlternateKeysArray => {
    setAll(valueType, lruMap, alternateKeyToKey, keyValueAlternateKeysArray, dispatchLruRemoves);
  };
 
  /** Like 'setAll', but returning a Promise that is executed in another event loop.
   * @memberof LruCache
   * @function
   * @param {Array} keyValueAlternateKeysArray - array of objects with 'key', 'value' and optional 'alternateKeys'
   * @returns {Promise}
   */
  self.setAllAsync = asyncWrap(self.setAll);
 
  /** Insert or update a cache entry.
   *  If alternate keys are provided and an already existing entry already has alternate keys, these will be extended.
   *  A corresponding cache changed event will be dispatched.
   *  If an insert leads to cache max size being exceeded and dispatchLruRemoves is set to true, the cache change event will contain both, insert and remove.
   * @memberof LruCache
   * @function
   * @param {object} keyValueAlternateKeys - object with 'key' and 'value' and optional 'alternateKeys'
   * @returns {undefined} void
   */
  self.set = keyValueAlternateKeys => {
    self.setAll([keyValueAlternateKeys]);
  };
 
  /** Like 'set', but returning a Promise that is executed in another event loop.
   * @memberof LruCache
   * @function
   * @param {object} keyValueAlternateKeys - object with 'key' and 'value' and optional 'alternateKeys'
   * @returns {Promise}
   */
  self.setAsync = asyncWrap(self.set);
 
  let entryGetter = null;
  const keyToPromise = new Map();
  const internalGetter = (key, getter, useEntryGetter = false, notFromCache = false, customEntryGetter = null) => {
    let entry; // eslint-disable-line init-declarations
    if (!notFromCache) {
      entry = getter(key);
      if (typeof entry === "undefined" && alternateKeyToKey.has(key)) {
        entry = getter(alternateKeyToKey.get(key));
      }
    }
    let usedEntryGetter = entryGetter;
    if (customEntryGetter !== null) {
      usedEntryGetter = customEntryGetter;
    }
    if (typeof entry === "undefined") {
      if (useEntryGetter && usedEntryGetter !== null) {
        if (keyToPromise.has(key)) {
          return keyToPromise.get(key);
        }
        const keyValueAlternateKeys = usedEntryGetter(key);
        Iif (!keyValueAlternateKeys) {
          return entry;
        }
        if (typeof keyValueAlternateKeys.then === "function") {
          const promise = keyValueAlternateKeys.then(keyValueAlternateKeysResolved => {
            if (!keyValueAlternateKeysResolved) {
              return keyValueAlternateKeysResolved;
            }
            Eif (keyToPromise.has(key)) {
              // The condition is necessary, because meanwhile there might have been a self.delete(key)
              self.set(keyValueAlternateKeysResolved);
            }
            return keyValueAlternateKeysResolved.value;
          }).finally(() => {
            keyToPromise.delete(key); // important to use key and not keyValueAlternateKeysResolved.key, because key could also be an alternate key!
          });
          keyToPromise.set(key, promise);
          return promise;
        }
        else {
          self.set(keyValueAlternateKeys);
          return keyValueAlternateKeys.value;
        }
      }
      else if (notFromCache) {
        throw new Error("called get with notFromCache, but no entry getter was set");
      }
      else {
        return entry;
      }
    }
    return entry.value;
  };
 
  /** Set a getter that can be used to retrieve a cache entry (keyValueAlternateKeys-object) by key in
   *  case it is not yet in the cache.
   *  For values that might be called by alternate key, the getter should also be able to handle this.
   * @memberof LruCache
   * @function
   * @param {function} newEntryGetter - function that takes a key as argument and returns corresponding entry or promise
   * @returns {undefined} void
   */
  self.setEntryGetter = newEntryGetter => {
    entryGetter = newEntryGetter;
  };
 
  /** Get value from cache by either its key or one of its alternate keys (if exists).
   *  If the value is not found in the cache and an entry-getter was set via setEntryGetter, then:
   *     - If the entry getter returns a Promise, a Promise resolving to the value will be returned.
   *       When the Promise resolves, the entry will be set to the cache. Until the Promise is not resolved,
   *       subsequent calls to get will return the same Promise.
   *     - If the entry getter returns a cache entry (keyValueAlternateKeys-object), this will be set to the cache
   *       and the value will be returned.
   *     - If the entry getter returns null or undefined, undefined will be returned.
   *  If the key is not in the cache and no entry getter is set, undefined will be returned.
   *  Makes the corresponding entry the most recently used (use 'getWithoutLruChange' to avoid this).
   * @memberof LruCache
   * @function
   * @param {string} keyOrAlternateKey - The key or alternate key of the value
   * @param {boolean} notFromCache - If true and an entry getter is set, then the value will not be taken from the
   *                                 cache, but from the entry getter. If no entry getter is set, an error will be
   *                                 thrown. (default: false)
   * @param {function} customEntryGetter - function that takes a key as argument and returns corresponding entry or
   *                                       promised entry. Has precedence over entry gettter set via setEntryGetter.
   * @returns {value | Promise | undefined} value, promised value or undefined
   */
  self.get = (keyOrAlternateKey, notFromCache = false, customEntryGetter = null) => internalGetter(keyOrAlternateKey, lruMap.get, true, notFromCache, customEntryGetter);
 
  /** Like 'get', but not making the corresponding entry the most recently used.
   *  If the value is retrieved via entry getter, it will of course still become the
   *  most recently used.
   * @memberof LruCache
   * @function
   * @param {string} keyOrAlternateKey - The key or alternate key of the value
   * @param {boolean} notFromCache - If true and an entry getter is set, then the value will not be taken from the
   *                                 cache, but from the entry getter. If no entry getter is set, an error will be
   *                                 thrown. (default: false)
   * @param {function} customEntryGetter - function that takes a key as argument and returns corresponding entry or
   *                                       promised entry. Has precedence over entry gettter set via setEntryGetter.
   * @returns {value | Promise | undefined} value, promised value or undefined
   */
  self.getWithoutLruChange = (keyOrAlternateKey, notFromCache = false, customEntryGetter = null) => internalGetter(keyOrAlternateKey, lruMap.getWithoutLruChange, true, notFromCache, customEntryGetter);
 
  /** Return whether the cache contains an entry for the given key or alternate key
   * @memberof LruCache
   * @function
   * @param {string} keyOrAlternateKey - The entry key or alternate key
   * @return {boolean} true, if the given key or alternate key is in the cache
   */
  self.has = keyOrAlternateKey => {
    if (lruMap.has(keyOrAlternateKey)) {
      return true;
    }
    else {
      return alternateKeyToKey.has(keyOrAlternateKey);
    }
  };
 
  /** Delete entry from cache by key.
   *  Here, no alternate key can be used.
   *  A corresponding cache changed event will be dispatched.
   * @memberof LruCache
   * @function
   * @param {string} key - The key of the to be deleted value
   * @returns {boolean} true, if the key was in the cache.
   */
  self.delete = key => {
    const entry = lruMap.getWithoutLruChange(key);
    if (typeof entry === "undefined") {
      wrapInTransaction(valueType, () => {
        handleDeleteRemove(valueType, {key});
      });
      return false;
    }
    lruMap.delete(entry.key);
    entry.alternateKeys.forEach(altKey => {
      alternateKeyToKey.delete(altKey);
    });
    // It is important to wrap even single actions to get consistent behavior,
    // e.g. to always reset 'order', even after a delete
    wrapInTransaction(valueType, () => {
      handleDeleteRemove(valueType, {key});
    });
    return true;
  };
 
  /** Iterate over the cache from oldest to newest entry.
   *  The given callback gets a cache entry as argument (an object with 'key', 'value' and 'alternateKeys').
   * @memberof LruCache
   * @function
   */
  self.forEach = lruMap.forEach;
 
  /** Get an Array with all cache entries.
   * @memberof LruCache
   * @function
   * @returns {Array} cache entries (objects with 'key', 'value' and 'alternateKeys')
   */
  self.getEntries = () => lruMap.map(entry => entry);
 
  /** Clear the cache.
   *  A corresponding cache changed event will be dispatched.
   * @memberof LruCache
   * @function
   * @returns {undefined} void
   */
  self.clear = () => {
    const keyValueArray = lruMap.clear();
    alternateKeyToKey.clear();
    if (dispatchClearRemoves) {
      wrapInTransaction(valueType, () => {
        keyValueArray.forEach(keyValuePair => {
          handleClearRemove(valueType, keyValuePair.value);
        });
      });
    }
  };
 
  /** Get the number of currently cached objects.
   * @memberof LruCache
   * @function
   * @returns {int} current number of entries in this cache
   */
  self.getSize = lruMap.getSize;
 
  /** Get the value type of this cache.
   * @memberof LruCache
   * @function
   * @return {string} the value type
   */
  self.getValueType = () => valueType;
 
  /** Return the current max size of this cache.
   * @memberof LruCache
   * @function
   * @returns {int} max size of this cache
   */
  self.getMaxSize = lruMap.getMaxSize;
 
  /** Set a new max size for this cache.
   *  If this leads to the removal of cache entries and dispatchLruRemoves is set true, a corresponding cache changed event will be dispatched.
   * @memberof LruCache
   * @function
   * @param {int} newMaxSize - the new max number of entries for this cache.
   * @returns {undefined} void
   */
  self.setMaxSize = newMaxSize => {
    const keyValueArray = lruMap.setMaxSize(newMaxSize);
    wrapInTransaction(valueType, () => {
      keyValueArray.forEach(keyValuePair => {
        keyValuePair.value.alternateKeys.forEach(altKey => {
          alternateKeyToKey.delete(altKey);
        });
        Eif (dispatchLruRemoves) {
          handleLruRemove(valueType, keyValuePair.value);
        }
      });
    });
  };
 
  return self;
}
 
 
const valueTypeToCache = new Map();
 
 
/**
 * Get a LruCache for the given valueType.
 * If a cache for this type already exists, the existing cache instance will be returned (LruCache is a singleton per value type).
 * @memberof @swarmy/lru-cache
 * @function
 * @param {string} valueType - The type of object being cached.
 * @returns {LruCache} - A lru-cache object.
 * @see LruCache
 */
export const getCache = valueType => {
  let lruCache = valueTypeToCache.get(valueType);
  if (typeof lruCache === "undefined") {
    lruCache = LruCache(valueType);
    valueTypeToCache.set(valueType, lruCache);
  }
  return lruCache;
};
 
/**
 * Clear (invalidate) all existing caches.
 * Will dispatch a single change event with all changes batched together.
 * @memberof @swarmy/lru-cache
 * @function
 * @returns {void}
 */
export const clearAllCaches = () => {
  cacheTransaction(() => {
    valueTypeToCache.forEach(cache => {
      cache.clear();
    });
  });
};