tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

WeakMapMap.js (2901B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 /**
      6 * WeakMapMap is a weakmap collection dual-keyed using an object and a string.
      7 * This is useful for keeping data compartmentalized e.g. grouped by tab.
      8 *
      9 * It's name comes from the internal structure which maps a WeakMap to a map,
     10 * which contains the target data.
     11 *
     12 * Usage:
     13 *   const myWeakMapMap = new WeakMapMap();
     14 *   const key = { randomObject: true };
     15 *   myWeakMapMap.set(key, "text1", "Some value1");
     16 *   myWeakMapMap.set(key, "text2", "Some value2");
     17 *   myWeakMapMap.get(key, "text1"); // Returns "Some value1"
     18 *   myWeakMapMap.get(key, "text2"); // Returns "Some value2"
     19 *   myWeakMapMap.has(key, "text1"); // Returns true
     20 *   myWeakMapMap.has(key, "notakey"); // Returns false
     21 */
     22 
     23 "use strict";
     24 
     25 class WeakMapMap {
     26  constructor() {
     27    this.clear();
     28  }
     29 
     30  /**
     31   * Returns the value associated to the key and nestedKey, or undefined if
     32   * there is none.
     33   *
     34   * @param {object} key
     35   *        The key associated with the desired value.
     36   * @param {string} nestedKey
     37   *        The nested key associated with the desired value.
     38   */
     39  get(key, nestedKey) {
     40    if (!this.has(key, nestedKey)) {
     41      return undefined;
     42    }
     43 
     44    return this.store.get(key).get(nestedKey);
     45  }
     46 
     47  /**
     48   * Returns the value associated to the key and nestedKey, or undefined if
     49   * there is none.
     50   *
     51   * @param {object} key
     52   *        The key associated with the desired value.
     53   * @param {string} nestedKey
     54   *        The nested key associated with the desired value.
     55   */
     56  has(key, nestedKey) {
     57    const hasKey = this.store.has(key);
     58 
     59    return hasKey && this.store.get(key).has(nestedKey);
     60  }
     61 
     62  /**
     63   *
     64   * @param {object} key
     65   *        The key associated with the value.
     66   * @param {string} nestedKey
     67   *        The nested key associated with the value.
     68   * @param {any} value
     69   *        The value to add.
     70   */
     71  set(key, nestedKey, value) {
     72    if (!this.store.has(key)) {
     73      this.store.set(key, new Map());
     74    }
     75 
     76    const innerMap = this.store.get(key);
     77    innerMap.set(nestedKey, value);
     78  }
     79 
     80  /**
     81   * Removes the value associated to the key and nestedKey.
     82   *
     83   * @param {object} key
     84   *        The key associated with the desired value.
     85   * @param {string} nestedKey
     86   *        The nested key associated with the desired value.
     87   *
     88   * @returns True if an element in the store has been removed successfully.
     89   *          False if the key is not found in the store.
     90   */
     91  delete(key, nestedKey) {
     92    if (!this.store.has(key)) {
     93      return false;
     94    }
     95 
     96    return this.store.get(key).delete(nestedKey);
     97  }
     98 
     99  /**
    100   * Clear the store.
    101   */
    102  clear() {
    103    this.store = new WeakMap();
    104  }
    105 }
    106 
    107 module.exports = WeakMapMap;