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