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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/tab/events.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,70 @@
     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 +// This module provides temporary shim until Bug 843901 is shipped.
    1.11 +// It basically registers tab event listeners on all windows that get
    1.12 +// opened and forwards them through observer notifications.
    1.13 +
    1.14 +module.metadata = {
    1.15 +  "stability": "experimental"
    1.16 +};
    1.17 +
    1.18 +const { Ci } = require("chrome");
    1.19 +const { windows, isInteractive } = require("../window/utils");
    1.20 +const { events } = require("../browser/events");
    1.21 +const { open } = require("../event/dom");
    1.22 +const { filter, map, merge, expand } = require("../event/utils");
    1.23 +const isFennec = require("sdk/system/xul-app").is("Fennec");
    1.24 +
    1.25 +// Module provides event stream (in nodejs style) that emits data events
    1.26 +// for all the tab events that happen in running firefox. At the moment
    1.27 +// it does it by registering listeners on all browser windows and then
    1.28 +// forwarding events when they occur to a stream. This will become obsolete
    1.29 +// once Bug 843901 is fixed, and we'll just leverage observer notifications.
    1.30 +
    1.31 +// Set of tab events that this module going to aggregate and expose.
    1.32 +const TYPES = ["TabOpen","TabClose","TabSelect","TabMove","TabPinned",
    1.33 +               "TabUnpinned"];
    1.34 +
    1.35 +// Utility function that given a browser `window` returns stream of above
    1.36 +// defined tab events for all tabs on the given window.
    1.37 +function tabEventsFor(window) {
    1.38 +  // Map supported event types to a streams of those events on the given
    1.39 +  // `window` and than merge these streams into single form stream off
    1.40 +  // all events.
    1.41 +  let channels = TYPES.map(function(type) open(window, type));
    1.42 +  return merge(channels);
    1.43 +}
    1.44 +
    1.45 +// Filter DOMContentLoaded events from all the browser events.
    1.46 +let readyEvents = filter(events, function(e) e.type === "DOMContentLoaded");
    1.47 +// Map DOMContentLoaded events to it's target browser windows.
    1.48 +let futureWindows = map(readyEvents, function(e) e.target);
    1.49 +// Expand all browsers that will become interactive to supported tab events
    1.50 +// on these windows. Result will be a tab events from all tabs of all windows
    1.51 +// that will become interactive.
    1.52 +let eventsFromFuture = expand(futureWindows, tabEventsFor);
    1.53 +
    1.54 +// Above covers only windows that will become interactive in a future, but some
    1.55 +// windows may already be interactive so we pick those and expand to supported
    1.56 +// tab events for them too.
    1.57 +let interactiveWindows = windows("navigator:browser", { includePrivate: true }).
    1.58 +                         filter(isInteractive);
    1.59 +let eventsFromInteractive = merge(interactiveWindows.map(tabEventsFor));
    1.60 +
    1.61 +
    1.62 +// Finally merge stream of tab events from future windows and current windows
    1.63 +// to cover all tab events on all windows that will open.
    1.64 +let allEvents = merge([eventsFromInteractive, eventsFromFuture]);
    1.65 +
    1.66 +// Map events to Fennec format if necessary
    1.67 +exports.events = map(allEvents, function (event) {
    1.68 +  return !isFennec ? event : {
    1.69 +    type: event.type,
    1.70 +    target: event.target.ownerDocument.defaultView.BrowserApp
    1.71 +            .getTabForBrowser(event.target)
    1.72 +  };
    1.73 +});

mercurial