1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/lib/sdk/event/target.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,76 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +'use strict'; 1.9 + 1.10 +module.metadata = { 1.11 + "stability": "stable" 1.12 +}; 1.13 + 1.14 +const { on, once, off, setListeners } = require('./core'); 1.15 +const { method, chainable } = require('../lang/functional'); 1.16 +const { Class } = require('../core/heritage'); 1.17 + 1.18 +/** 1.19 + * `EventTarget` is an exemplar for creating an objects that can be used to 1.20 + * add / remove event listeners on them. Events on these objects may be emitted 1.21 + * via `emit` function exported by 'event/core' module. 1.22 + */ 1.23 +const EventTarget = Class({ 1.24 + /** 1.25 + * Method initializes `this` event source. It goes through properties of a 1.26 + * given `options` and registers listeners for the ones that look like an 1.27 + * event listeners. 1.28 + */ 1.29 + /** 1.30 + * Method initializes `this` event source. It goes through properties of a 1.31 + * given `options` and registers listeners for the ones that look like an 1.32 + * event listeners. 1.33 + */ 1.34 + initialize: function initialize(options) { 1.35 + setListeners(this, options); 1.36 + }, 1.37 + /** 1.38 + * Registers an event `listener` that is called every time events of 1.39 + * specified `type` are emitted. 1.40 + * @param {String} type 1.41 + * The type of event. 1.42 + * @param {Function} listener 1.43 + * The listener function that processes the event. 1.44 + * @example 1.45 + * worker.on('message', function (data) { 1.46 + * console.log('data received: ' + data) 1.47 + * }) 1.48 + */ 1.49 + on: chainable(method(on)), 1.50 + /** 1.51 + * Registers an event `listener` that is called once the next time an event 1.52 + * of the specified `type` is emitted. 1.53 + * @param {String} type 1.54 + * The type of the event. 1.55 + * @param {Function} listener 1.56 + * The listener function that processes the event. 1.57 + */ 1.58 + once: chainable(method(once)), 1.59 + /** 1.60 + * Removes an event `listener` for the given event `type`. 1.61 + * @param {String} type 1.62 + * The type of event. 1.63 + * @param {Function} listener 1.64 + * The listener function that processes the event. 1.65 + */ 1.66 + removeListener: function removeListener(type, listener) { 1.67 + // Note: We can't just wrap `off` in `method` as we do it for other methods 1.68 + // cause skipping a second or third argument will behave very differently 1.69 + // than intended. This way we make sure all arguments are passed and only 1.70 + // one listener is removed at most. 1.71 + off(this, type, listener); 1.72 + return this; 1.73 + }, 1.74 + off: function(type, listener) { 1.75 + off(this, type, listener); 1.76 + return this; 1.77 + } 1.78 +}); 1.79 +exports.EventTarget = EventTarget;