addon-sdk/source/lib/sdk/event/core.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": "unstable"
michael@0 9 };
michael@0 10
michael@0 11 const UNCAUGHT_ERROR = 'An error event was emitted for which there was no listener.';
michael@0 12 const BAD_LISTENER = 'The event listener must be a function.';
michael@0 13
michael@0 14 const { ns } = require('../core/namespace');
michael@0 15
michael@0 16 const event = ns();
michael@0 17
michael@0 18 const EVENT_TYPE_PATTERN = /^on([A-Z]\w+$)/;
michael@0 19 exports.EVENT_TYPE_PATTERN = EVENT_TYPE_PATTERN;
michael@0 20
michael@0 21 // Utility function to access given event `target` object's event listeners for
michael@0 22 // the specific event `type`. If listeners for this type does not exists they
michael@0 23 // will be created.
michael@0 24 const observers = function observers(target, type) {
michael@0 25 if (!target) throw TypeError("Event target must be an object");
michael@0 26 let listeners = event(target);
michael@0 27 return type in listeners ? listeners[type] : listeners[type] = [];
michael@0 28 };
michael@0 29
michael@0 30 /**
michael@0 31 * Registers an event `listener` that is called every time events of
michael@0 32 * specified `type` is emitted on the given event `target`.
michael@0 33 * @param {Object} target
michael@0 34 * Event target object.
michael@0 35 * @param {String} type
michael@0 36 * The type of event.
michael@0 37 * @param {Function} listener
michael@0 38 * The listener function that processes the event.
michael@0 39 */
michael@0 40 function on(target, type, listener) {
michael@0 41 if (typeof(listener) !== 'function')
michael@0 42 throw new Error(BAD_LISTENER);
michael@0 43
michael@0 44 let listeners = observers(target, type);
michael@0 45 if (!~listeners.indexOf(listener))
michael@0 46 listeners.push(listener);
michael@0 47 }
michael@0 48 exports.on = on;
michael@0 49
michael@0 50 /**
michael@0 51 * Registers an event `listener` that is called only the next time an event
michael@0 52 * of the specified `type` is emitted on the given event `target`.
michael@0 53 * @param {Object} target
michael@0 54 * Event target object.
michael@0 55 * @param {String} type
michael@0 56 * The type of the event.
michael@0 57 * @param {Function} listener
michael@0 58 * The listener function that processes the event.
michael@0 59 */
michael@0 60 function once(target, type, listener) {
michael@0 61 on(target, type, function observer(...args) {
michael@0 62 off(target, type, observer);
michael@0 63 listener.apply(target, args);
michael@0 64 });
michael@0 65 }
michael@0 66 exports.once = once;
michael@0 67
michael@0 68 /**
michael@0 69 * Execute each of the listeners in order with the supplied arguments.
michael@0 70 * All the exceptions that are thrown by listeners during the emit
michael@0 71 * are caught and can be handled by listeners of 'error' event. Thrown
michael@0 72 * exceptions are passed as an argument to an 'error' event listener.
michael@0 73 * If no 'error' listener is registered exception will be logged into an
michael@0 74 * error console.
michael@0 75 * @param {Object} target
michael@0 76 * Event target object.
michael@0 77 * @param {String} type
michael@0 78 * The type of event.
michael@0 79 * @params {Object|Number|String|Boolean} args
michael@0 80 * Arguments that will be passed to listeners.
michael@0 81 */
michael@0 82 function emit (target, type, ...args) {
michael@0 83 let state = observers(target, type);
michael@0 84 let listeners = state.slice();
michael@0 85 let count = listeners.length;
michael@0 86 let index = 0;
michael@0 87
michael@0 88 // If error event and there are no handlers then print error message
michael@0 89 // into a console.
michael@0 90 if (count === 0 && type === 'error') console.exception(args[0]);
michael@0 91 while (index < count) {
michael@0 92 try {
michael@0 93 let listener = listeners[index];
michael@0 94 // Dispatch only if listener is still registered.
michael@0 95 if (~state.indexOf(listener))
michael@0 96 listener.apply(target, args);
michael@0 97 }
michael@0 98 catch (error) {
michael@0 99 // If exception is not thrown by a error listener and error listener is
michael@0 100 // registered emit `error` event. Otherwise dump exception to the console.
michael@0 101 if (type !== 'error') emit(target, 'error', error);
michael@0 102 else console.exception(error);
michael@0 103 }
michael@0 104 index++;
michael@0 105 }
michael@0 106 // Also emit on `"*"` so that one could listen for all events.
michael@0 107 if (type !== '*') emit(target, '*', type, ...args);
michael@0 108 }
michael@0 109 exports.emit = emit;
michael@0 110
michael@0 111 /**
michael@0 112 * Removes an event `listener` for the given event `type` on the given event
michael@0 113 * `target`. If no `listener` is passed removes all listeners of the given
michael@0 114 * `type`. If `type` is not passed removes all the listeners of the given
michael@0 115 * event `target`.
michael@0 116 * @param {Object} target
michael@0 117 * The event target object.
michael@0 118 * @param {String} type
michael@0 119 * The type of event.
michael@0 120 * @param {Function} listener
michael@0 121 * The listener function that processes the event.
michael@0 122 */
michael@0 123 function off(target, type, listener) {
michael@0 124 let length = arguments.length;
michael@0 125 if (length === 3) {
michael@0 126 let listeners = observers(target, type);
michael@0 127 let index = listeners.indexOf(listener);
michael@0 128 if (~index)
michael@0 129 listeners.splice(index, 1);
michael@0 130 }
michael@0 131 else if (length === 2) {
michael@0 132 observers(target, type).splice(0);
michael@0 133 }
michael@0 134 else if (length === 1) {
michael@0 135 let listeners = event(target);
michael@0 136 Object.keys(listeners).forEach(type => delete listeners[type]);
michael@0 137 }
michael@0 138 }
michael@0 139 exports.off = off;
michael@0 140
michael@0 141 /**
michael@0 142 * Returns a number of event listeners registered for the given event `type`
michael@0 143 * on the given event `target`.
michael@0 144 */
michael@0 145 function count(target, type) {
michael@0 146 return observers(target, type).length;
michael@0 147 }
michael@0 148 exports.count = count;
michael@0 149
michael@0 150 /**
michael@0 151 * Registers listeners on the given event `target` from the given `listeners`
michael@0 152 * dictionary. Iterates over the listeners and if property name matches name
michael@0 153 * pattern `onEventType` and property is a function, then registers it as
michael@0 154 * an `eventType` listener on `target`.
michael@0 155 *
michael@0 156 * @param {Object} target
michael@0 157 * The type of event.
michael@0 158 * @param {Object} listeners
michael@0 159 * Dictionary of listeners.
michael@0 160 */
michael@0 161 function setListeners(target, listeners) {
michael@0 162 Object.keys(listeners || {}).forEach(key => {
michael@0 163 let match = EVENT_TYPE_PATTERN.exec(key);
michael@0 164 let type = match && match[1].toLowerCase();
michael@0 165 if (!type) return;
michael@0 166
michael@0 167 let listener = listeners[key];
michael@0 168 if (typeof(listener) === 'function')
michael@0 169 on(target, type, listener);
michael@0 170 });
michael@0 171 }
michael@0 172 exports.setListeners = setListeners;

mercurial