chat/modules/NormalizedMap.jsm
author Ping Chen <remotenonsense@gmail.com>
Mon, 14 Jun 2021 13:29:26 +0300
changeset 32814 81f4842d2dd7e44befef8edc478a014ffff14f19
parent 29426 7853102282e6bc50b8a2317e86b5185b0c5c250d
permissions -rw-r--r--
Bug 1715713 - Prevent showing multiple newmailalert.xhtml notification windows. r=mkmelin This patch makes two changes: 1. If a newmailalert.xhtml window is already shown, save the folder and show a new notification only after the current notification is closed. 2. Pass new message keys to newmailalert.js. Previously, newmailalert.js receives a root folder and scans all subfolders for NEW messages, which is unnecessary and may incorrectly include old messages. Differential Revision: https://phabricator.services.mozilla.com/D117617

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const EXPORTED_SYMBOLS = ["NormalizedMap"];

/*
 * A Map that automatically normalizes keys before accessing the values.
 *
 * The constructor takes two parameters:
 *  aNormalize:   A function which takes a string and returns the "normalized"
 *                version of it.
 *  aIterable:    A iterable to prefill the map with, keys will be normalized.
 *
 * Returns a Map object that will automatically run aNormalize on any operations
 * involving keys.
 */
class NormalizedMap extends Map {
  constructor(aNormalize, aIterable = []) {
    if (typeof aNormalize != "function") {
      throw new Error("NormalizedMap must have a normalize function!");
    }
    // Create the wrapped Map; use the provided iterable after normalizing the
    // keys.
    let entries = [...aIterable].map(([key, val]) => [aNormalize(key), val]);
    super(entries);
    // Note: In derived classes, super() must be called before using 'this'.
    this._normalize = aNormalize;
  }

  // Dummy normalize function.
  _normalize(aKey) {
    return aKey;
  }

  // Anything that accepts a key as an input needs to be manually overridden.
  delete(key) {
    return super.delete(this._normalize(key));
  }
  get(key) {
    return super.get(this._normalize(key));
  }
  has(key) {
    return super.has(this._normalize(key));
  }
  set(key, val) {
    super.set(this._normalize(key), val);
    return this;
  }
}