tor-browser

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

eventemitter.sys.mjs (2289B)


      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 import { Module } from "chrome://remote/content/shared/messagehandler/Module.sys.mjs";
      6 
      7 class EventEmitterModule extends Module {
      8  #isSubscribed;
      9  #subscribedEvents;
     10 
     11  constructor(messageHandler) {
     12    super(messageHandler);
     13    this.#isSubscribed = false;
     14    this.#subscribedEvents = new Set();
     15  }
     16 
     17  destroy() {}
     18 
     19  /**
     20   * Commands
     21   */
     22 
     23  emitTestEvent() {
     24    if (this.#isSubscribed) {
     25      const text = `event from ${this.messageHandler.contextId}`;
     26      this.emitEvent("eventemitter.testEvent", { text });
     27    }
     28 
     29    // Emit another event consistently for monitoring during the test.
     30    this.emitEvent("eventemitter.monitoringEvent", {});
     31  }
     32 
     33  isSubscribed() {
     34    return this.#isSubscribed;
     35  }
     36 
     37  _applySessionData(params) {
     38    const { category } = params;
     39    if (category === "event") {
     40      const filteredSessionData = params.sessionData.filter(item =>
     41        this.messageHandler.matchesContext(item.contextDescriptor)
     42      );
     43      for (const event of this.#subscribedEvents.values()) {
     44        const hasSessionItem = filteredSessionData.some(
     45          item => item.value === event
     46        );
     47        // If there are no session items for this context, we should unsubscribe from the event.
     48        if (!hasSessionItem) {
     49          this.#unsubscribeEvent(event);
     50        }
     51      }
     52 
     53      // Subscribe to all events, which have an item in SessionData
     54      for (const { value } of filteredSessionData) {
     55        this.#subscribeEvent(value);
     56      }
     57    }
     58  }
     59 
     60  #subscribeEvent(event) {
     61    if (event === "eventemitter.testEvent") {
     62      if (this.#isSubscribed) {
     63        throw new Error("Already subscribed to eventemitter.testEvent");
     64      }
     65      this.#isSubscribed = true;
     66      this.#subscribedEvents.add(event);
     67    }
     68  }
     69 
     70  #unsubscribeEvent(event) {
     71    if (event === "eventemitter.testEvent") {
     72      if (!this.#isSubscribed) {
     73        throw new Error("Not subscribed to eventemitter.testEvent");
     74      }
     75      this.#isSubscribed = false;
     76      this.#subscribedEvents.delete(event);
     77    }
     78  }
     79 }
     80 
     81 export const eventemitter = EventEmitterModule;