addon-sdk/source/lib/sdk/util/dispatcher.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": "experimental"
michael@0 9 };
michael@0 10
michael@0 11 const method = require("method/core");
michael@0 12
michael@0 13 // Utility function that is just an enhancement over `method` to
michael@0 14 // allow predicate based dispatch in addition to polymorphic
michael@0 15 // dispatch. Unfortunately polymorphic dispatch does not quite
michael@0 16 // cuts it in the world of XPCOM where no types / classes exist
michael@0 17 // and all the XUL nodes share same type / prototype.
michael@0 18 // Probably this is more generic and belongs some place else, but
michael@0 19 // we can move it later once this will be relevant.
michael@0 20 let dispatcher = hint => {
michael@0 21 const base = method(hint);
michael@0 22 // Make a map for storing predicate, implementation mappings.
michael@0 23 let implementations = new Map();
michael@0 24
michael@0 25 // Dispatcher function goes through `predicate, implementation`
michael@0 26 // pairs to find predicate that matches first argument and
michael@0 27 // returns application of arguments on the associated
michael@0 28 // `implementation`. If no matching predicate is found delegates
michael@0 29 // to a `base` polymorphic function.
michael@0 30 let dispatch = (value, ...rest) => {
michael@0 31 for (let [predicate, implementation] of implementations) {
michael@0 32 if (predicate(value))
michael@0 33 return implementation(value, ...rest);
michael@0 34 }
michael@0 35
michael@0 36 return base(value, ...rest);
michael@0 37 };
michael@0 38
michael@0 39 // Expose base API.
michael@0 40 dispatch.define = base.define;
michael@0 41 dispatch.implement = base.implement;
michael@0 42 dispatch.toString = base.toString;
michael@0 43
michael@0 44 // Add a `when` function to allow extending function via
michael@0 45 // predicates.
michael@0 46 dispatch.when = (predicate, implementation) => {
michael@0 47 if (implementations.has(predicate))
michael@0 48 throw TypeError("Already implemented for the given predicate");
michael@0 49 implementations.set(predicate, implementation);
michael@0 50 };
michael@0 51
michael@0 52 return dispatch;
michael@0 53 };
michael@0 54
michael@0 55 exports.dispatcher = dispatcher;

mercurial