addon-sdk/source/lib/sdk/windows/observer.js

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

mercurial