Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
// Reactor.js
(function () {
  /*global adventurejs A*/
  "use strict";

  /**
   * @class adventurejs.Reactor
   * @ajsinternal
   * @ajsnavheading FrameworkReference
   * @param {Game} game A reference to the game instance.
   * @summary Manages event listening for custom events.
   * @classdesc
   * <p>
   * <strong>Reactor</strong> manages event listening for custom
   * events, by creating
   * a DOM element to do the listening, and then hooking into
   * the element's DOM methods.
   * Reactor is created automatically
   * by {@link adventurejs.Game|Game}. This is an internal class that
   * authors should not need to construct or modify.
   * </p>
   */

  //http://jsfiddle.net/2vqof4zs/156/

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

      /**
       * Create a text node / HTML element to act as a listener for
       * custom events.
       * @var {HTMLElement} adventurejs.Reactor#target
       * @default {}
       */
      this.target = document.createTextNode(null);

      /**
       * Give the text node / HTML element a reference back to the Game.
       * @var {Object} adventurejs.Reactor.target#game
       * @default {}
       */
      this.target.game = game;

      /**
       * Hook game.reactor to target.addEventListener.
       * @var {Object} adventurejs.Reactor#addEventListener
       * @default {}
       */
      this.addEventListener = this.target.addEventListener.bind(this.target);

      /**
       * Hook game.reactor to target.removeEventListener.
       * @var {Object} adventurejs.Reactor#removeEventListener
       * @default {}
       */
      this.removeEventListener = this.target.removeEventListener.bind(
        this.target
      );

      /**
       * Hook game.reactor to target.dispatchEvent.
       * @var {Object} adventurejs.Reactor#dispatchEvent
       * @default {}
       */
      this.dispatchEvent = this.target.dispatchEvent.bind(this.target);
    }
  }
  adventurejs.Reactor = Reactor;
})();