michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: 'use strict'; michael@0: michael@0: module.metadata = { michael@0: "stability": "stable" michael@0: }; michael@0: michael@0: const { on, once, off, setListeners } = require('./core'); michael@0: const { method, chainable } = require('../lang/functional'); michael@0: const { Class } = require('../core/heritage'); michael@0: michael@0: /** michael@0: * `EventTarget` is an exemplar for creating an objects that can be used to michael@0: * add / remove event listeners on them. Events on these objects may be emitted michael@0: * via `emit` function exported by 'event/core' module. michael@0: */ michael@0: const EventTarget = Class({ michael@0: /** michael@0: * Method initializes `this` event source. It goes through properties of a michael@0: * given `options` and registers listeners for the ones that look like an michael@0: * event listeners. michael@0: */ michael@0: /** michael@0: * Method initializes `this` event source. It goes through properties of a michael@0: * given `options` and registers listeners for the ones that look like an michael@0: * event listeners. michael@0: */ michael@0: initialize: function initialize(options) { michael@0: setListeners(this, options); michael@0: }, michael@0: /** michael@0: * Registers an event `listener` that is called every time events of michael@0: * specified `type` are emitted. michael@0: * @param {String} type michael@0: * The type of event. michael@0: * @param {Function} listener michael@0: * The listener function that processes the event. michael@0: * @example michael@0: * worker.on('message', function (data) { michael@0: * console.log('data received: ' + data) michael@0: * }) michael@0: */ michael@0: on: chainable(method(on)), michael@0: /** michael@0: * Registers an event `listener` that is called once the next time an event michael@0: * of the specified `type` is emitted. michael@0: * @param {String} type michael@0: * The type of the event. michael@0: * @param {Function} listener michael@0: * The listener function that processes the event. michael@0: */ michael@0: once: chainable(method(once)), michael@0: /** michael@0: * Removes an event `listener` for the given event `type`. michael@0: * @param {String} type michael@0: * The type of event. michael@0: * @param {Function} listener michael@0: * The listener function that processes the event. michael@0: */ michael@0: removeListener: function removeListener(type, listener) { michael@0: // Note: We can't just wrap `off` in `method` as we do it for other methods michael@0: // cause skipping a second or third argument will behave very differently michael@0: // than intended. This way we make sure all arguments are passed and only michael@0: // one listener is removed at most. michael@0: off(this, type, listener); michael@0: return this; michael@0: }, michael@0: off: function(type, listener) { michael@0: off(this, type, listener); michael@0: return this; michael@0: } michael@0: }); michael@0: exports.EventTarget = EventTarget;