Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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/. */
5 'use strict';
7 module.metadata = {
8 "stability": "stable"
9 };
11 const { on, once, off, setListeners } = require('./core');
12 const { method, chainable } = require('../lang/functional');
13 const { Class } = require('../core/heritage');
15 /**
16 * `EventTarget` is an exemplar for creating an objects that can be used to
17 * add / remove event listeners on them. Events on these objects may be emitted
18 * via `emit` function exported by 'event/core' module.
19 */
20 const EventTarget = Class({
21 /**
22 * Method initializes `this` event source. It goes through properties of a
23 * given `options` and registers listeners for the ones that look like an
24 * event listeners.
25 */
26 /**
27 * Method initializes `this` event source. It goes through properties of a
28 * given `options` and registers listeners for the ones that look like an
29 * event listeners.
30 */
31 initialize: function initialize(options) {
32 setListeners(this, options);
33 },
34 /**
35 * Registers an event `listener` that is called every time events of
36 * specified `type` are emitted.
37 * @param {String} type
38 * The type of event.
39 * @param {Function} listener
40 * The listener function that processes the event.
41 * @example
42 * worker.on('message', function (data) {
43 * console.log('data received: ' + data)
44 * })
45 */
46 on: chainable(method(on)),
47 /**
48 * Registers an event `listener` that is called once the next time an event
49 * of the specified `type` is emitted.
50 * @param {String} type
51 * The type of the event.
52 * @param {Function} listener
53 * The listener function that processes the event.
54 */
55 once: chainable(method(once)),
56 /**
57 * Removes an event `listener` for the given event `type`.
58 * @param {String} type
59 * The type of event.
60 * @param {Function} listener
61 * The listener function that processes the event.
62 */
63 removeListener: function removeListener(type, listener) {
64 // Note: We can't just wrap `off` in `method` as we do it for other methods
65 // cause skipping a second or third argument will behave very differently
66 // than intended. This way we make sure all arguments are passed and only
67 // one listener is removed at most.
68 off(this, type, listener);
69 return this;
70 },
71 off: function(type, listener) {
72 off(this, type, listener);
73 return this;
74 }
75 });
76 exports.EventTarget = EventTarget;