Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// Reactor.js

(function () {
  /*global adventurejs A*/

  /**
   * @class adventurejs.Reactor
   * @ajsinternal
   * @ajsnavheading FrameworkReference
   * @param {Game} game A reference to the game instance.
   * @summary Modern custom event system using EventTarget.
   * @classdesc
   * <p>
   * <strong>Reactor</strong> contains utility methods for
   * the event system. Reactor is created automatically by
   * {@link adventurejs.Game|Game}. This is an internal class
   * that authors should not need to construct or modify.
   * </p>
   */
  class Reactor extends EventTarget {
    constructor(game) {
      super();

      /**
       * A reference back to the main {@link adventurejs.Game|Game} object.
       * @var {Object} adventurejs.Reactor#game
       * @default {}
       */
      this.game = game;
    }

    /**
     * Dispatches a custom event with access to the game instance.
     * @param {string} type The event name.
     * @param {Object} [detail={}] Optional additional event data.
     */
    emit(type, detail = {}) {
      const event = new CustomEvent(type, {
        bubbles: false,
        cancelable: true,
        detail: {
          ...detail,
          game: this.game,
        },
      });
      this.dispatchEvent(event);
    }
  }
  adventurejs.Reactor = Reactor;
})();