addon-sdk/source/lib/sdk/deprecated/events.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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": "deprecated"
michael@0 9 };
michael@0 10
michael@0 11 const ERROR_TYPE = 'error',
michael@0 12 UNCAUGHT_ERROR = 'An error event was dispatched for which there was'
michael@0 13 + ' no listener.',
michael@0 14 BAD_LISTENER = 'The event listener must be a function.';
michael@0 15 /**
michael@0 16 * This object is used to create an `EventEmitter` that, useful for composing
michael@0 17 * objects that emit events. It implements an interface like `EventTarget` from
michael@0 18 * DOM Level 2, which is implemented by Node objects in implementations that
michael@0 19 * support the DOM Event Model.
michael@0 20 * @see http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget
michael@0 21 * @see http://nodejs.org/api.html#EventEmitter
michael@0 22 * @see http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/events/EventDispatcher.html
michael@0 23 */
michael@0 24 const eventEmitter = {
michael@0 25 /**
michael@0 26 * Registers an event `listener` that is called every time events of
michael@0 27 * specified `type` are emitted.
michael@0 28 * @param {String} type
michael@0 29 * The type of event.
michael@0 30 * @param {Function} listener
michael@0 31 * The listener function that processes the event.
michael@0 32 * @example
michael@0 33 * worker.on('message', function (data) {
michael@0 34 * console.log('data received: ' + data)
michael@0 35 * })
michael@0 36 */
michael@0 37 on: function on(type, listener) {
michael@0 38 if ('function' !== typeof listener)
michael@0 39 throw new Error(BAD_LISTENER);
michael@0 40 let listeners = this._listeners(type);
michael@0 41 if (0 > listeners.indexOf(listener))
michael@0 42 listeners.push(listener);
michael@0 43 // Use of `_public` is required by the legacy traits code that will go away
michael@0 44 // once bug-637633 is fixed.
michael@0 45 return this._public || this;
michael@0 46 },
michael@0 47
michael@0 48 /**
michael@0 49 * Registers an event `listener` that is called once the next time an event
michael@0 50 * of the specified `type` is emitted.
michael@0 51 * @param {String} type
michael@0 52 * The type of the event.
michael@0 53 * @param {Function} listener
michael@0 54 * The listener function that processes the event.
michael@0 55 */
michael@0 56 once: function once(type, listener) {
michael@0 57 this.on(type, function selfRemovableListener() {
michael@0 58 this.removeListener(type, selfRemovableListener);
michael@0 59 listener.apply(this, arguments);
michael@0 60 });
michael@0 61 },
michael@0 62
michael@0 63 /**
michael@0 64 * Unregister `listener` for the specified event type.
michael@0 65 * @param {String} type
michael@0 66 * The type of event.
michael@0 67 * @param {Function} listener
michael@0 68 * The listener function that processes the event.
michael@0 69 */
michael@0 70 removeListener: function removeListener(type, listener) {
michael@0 71 if ('function' !== typeof listener)
michael@0 72 throw new Error(BAD_LISTENER);
michael@0 73 let listeners = this._listeners(type),
michael@0 74 index = listeners.indexOf(listener);
michael@0 75 if (0 <= index)
michael@0 76 listeners.splice(index, 1);
michael@0 77 // Use of `_public` is required by the legacy traits code, that will go away
michael@0 78 // once bug-637633 is fixed.
michael@0 79 return this._public || this;
michael@0 80 },
michael@0 81
michael@0 82 /**
michael@0 83 * Hash of listeners on this EventEmitter.
michael@0 84 */
michael@0 85 _events: null,
michael@0 86
michael@0 87 /**
michael@0 88 * Returns an array of listeners for the specified event `type`. This array
michael@0 89 * can be manipulated, e.g. to remove listeners.
michael@0 90 * @param {String} type
michael@0 91 * The type of event.
michael@0 92 */
michael@0 93 _listeners: function listeners(type) {
michael@0 94 let events = this._events || (this._events = {});
michael@0 95 return (events.hasOwnProperty(type) && events[type]) || (events[type] = []);
michael@0 96 },
michael@0 97
michael@0 98 /**
michael@0 99 * Execute each of the listeners in order with the supplied arguments.
michael@0 100 * Returns `true` if listener for this event was called, `false` if there are
michael@0 101 * no listeners for this event `type`.
michael@0 102 *
michael@0 103 * All the exceptions that are thrown by listeners during the emit
michael@0 104 * are caught and can be handled by listeners of 'error' event. Thrown
michael@0 105 * exceptions are passed as an argument to an 'error' event listener.
michael@0 106 * If no 'error' listener is registered exception will propagate to a
michael@0 107 * caller of this method.
michael@0 108 *
michael@0 109 * **It's recommended to have a default 'error' listener in all the complete
michael@0 110 * composition that in worst case may dump errors to the console.**
michael@0 111 *
michael@0 112 * @param {String} type
michael@0 113 * The type of event.
michael@0 114 * @params {Object|Number|String|Boolean}
michael@0 115 * Arguments that will be passed to listeners.
michael@0 116 * @returns {Boolean}
michael@0 117 */
michael@0 118 _emit: function _emit(type, event) {
michael@0 119 let args = Array.slice(arguments);
michael@0 120 // Use of `_public` is required by the legacy traits code that will go away
michael@0 121 // once bug-637633 is fixed.
michael@0 122 args.unshift(this._public || this);
michael@0 123 return this._emitOnObject.apply(this, args);
michael@0 124 },
michael@0 125
michael@0 126 /**
michael@0 127 * A version of _emit that lets you specify the object on which listeners are
michael@0 128 * called. This is a hack that is sometimes necessary when such an object
michael@0 129 * (exports, for example) cannot be an EventEmitter for some reason, but other
michael@0 130 * object(s) managing events for the object are EventEmitters. Once bug
michael@0 131 * 577782 is fixed, this method shouldn't be necessary.
michael@0 132 *
michael@0 133 * @param {object} targetObj
michael@0 134 * The object on which listeners will be called.
michael@0 135 * @param {string} type
michael@0 136 * The event name.
michael@0 137 * @param {value} event
michael@0 138 * The first argument to pass to listeners.
michael@0 139 * @param {value} ...
michael@0 140 * More arguments to pass to listeners.
michael@0 141 * @returns {boolean}
michael@0 142 */
michael@0 143 _emitOnObject: function _emitOnObject(targetObj, type, event /* , ... */) {
michael@0 144 let listeners = this._listeners(type).slice(0);
michael@0 145 // If there is no 'error' event listener then throw.
michael@0 146 if (type === ERROR_TYPE && !listeners.length)
michael@0 147 console.exception(event);
michael@0 148 if (!listeners.length)
michael@0 149 return false;
michael@0 150 let params = Array.slice(arguments, 2);
michael@0 151 for each (let listener in listeners) {
michael@0 152 try {
michael@0 153 listener.apply(targetObj, params);
michael@0 154 } catch(e) {
michael@0 155 // Bug 726967: Ignore exceptions being throws while notifying the error
michael@0 156 // in order to avoid infinite loops.
michael@0 157 if (type !== ERROR_TYPE)
michael@0 158 this._emit(ERROR_TYPE, e);
michael@0 159 else
michael@0 160 console.exception("Exception in error event listener " + e);
michael@0 161 }
michael@0 162 }
michael@0 163 return true;
michael@0 164 },
michael@0 165
michael@0 166 /**
michael@0 167 * Removes all the event listeners for the specified event `type`.
michael@0 168 * @param {String} type
michael@0 169 * The type of event.
michael@0 170 */
michael@0 171 _removeAllListeners: function _removeAllListeners(type) {
michael@0 172 if (typeof type == "undefined") {
michael@0 173 this._events = null;
michael@0 174 return this;
michael@0 175 }
michael@0 176
michael@0 177 this._listeners(type).splice(0);
michael@0 178 return this;
michael@0 179 }
michael@0 180 };
michael@0 181 exports.EventEmitter = require("./traits").Trait.compose(eventEmitter);
michael@0 182 exports.EventEmitterTrait = require('./light-traits').Trait(eventEmitter);

mercurial