tor-browser

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

object-command.js (1710B)


      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 "use strict";
      6 
      7 /**
      8 * The ObjectCommand helps inspecting and managing lifecycle
      9 * of all inspected JavaScript objects.
     10 */
     11 class ObjectCommand {
     12  constructor({ commands, _descriptorFront, _watcherFront }) {
     13    this.#commands = commands;
     14  }
     15  // eslint-disable-next-line no-unused-private-class-members
     16  #commands = null;
     17 
     18  /**
     19   * Release a set of object actors all at once.
     20   *
     21   * @param {Array<ObjectFront>} frontsToRelease
     22   *        List of fronts for the object to release.
     23   */
     24  async releaseObjects(frontsToRelease) {
     25    // First group all object fronts per target
     26    const actorsPerTarget = new Map();
     27    const promises = [];
     28    for (const frontToRelease of frontsToRelease) {
     29      const { targetFront } = frontToRelease;
     30      // If the front is already destroyed, its target front will be nullified.
     31      if (!targetFront) {
     32        continue;
     33      }
     34 
     35      let actorIDsToRemove = actorsPerTarget.get(targetFront);
     36      if (!actorIDsToRemove) {
     37        actorIDsToRemove = [];
     38        actorsPerTarget.set(targetFront, actorIDsToRemove);
     39      }
     40      actorIDsToRemove.push(frontToRelease.actorID);
     41      frontToRelease.destroy();
     42    }
     43 
     44    // Then release all fronts by bulk per target
     45    for (const [targetFront, actorIDs] of actorsPerTarget) {
     46      const objectsManagerFront = await targetFront.getFront("objects-manager");
     47      promises.push(objectsManagerFront.releaseObjects(actorIDs));
     48    }
     49 
     50    await Promise.all(promises);
     51  }
     52 }
     53 
     54 module.exports = ObjectCommand;