observer.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. "use strict";
  5. module.metadata = {
  6. "stability": "unstable"
  7. };
  8. const { EventEmitterTrait: EventEmitter } = require("../deprecated/events");
  9. const { WindowTracker, windowIterator } = require("../deprecated/window-utils");
  10. const { DOMEventAssembler } = require("../deprecated/events/assembler");
  11. const { Trait } = require("../deprecated/light-traits");
  12. // Event emitter objects used to register listeners and emit events on them
  13. // when they occur.
  14. const observer = Trait.compose(DOMEventAssembler, EventEmitter).create({
  15. /**
  16. * Method is implemented by `EventEmitter` and is used just for emitting
  17. * events on registered listeners.
  18. */
  19. _emit: Trait.required,
  20. /**
  21. * Events that are supported and emitted by the module.
  22. */
  23. supportedEventsTypes: [ "activate", "deactivate" ],
  24. /**
  25. * Function handles all the supported events on all the windows that are
  26. * observed. Method is used to proxy events to the listeners registered on
  27. * this event emitter.
  28. * @param {Event} event
  29. * Keyboard event being emitted.
  30. */
  31. handleEvent: function handleEvent(event) {
  32. this._emit(event.type, event.target, event);
  33. }
  34. });
  35. // Using `WindowTracker` to track window events.
  36. WindowTracker({
  37. onTrack: function onTrack(chromeWindow) {
  38. observer._emit("open", chromeWindow);
  39. observer.observe(chromeWindow);
  40. },
  41. onUntrack: function onUntrack(chromeWindow) {
  42. observer._emit("close", chromeWindow);
  43. observer.ignore(chromeWindow);
  44. }
  45. });
  46. exports.observer = observer;