addon-sdk/source/lib/sdk/input/browser.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/input/browser.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,73 @@
     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 +"use strict";
     1.8 +
     1.9 +const { windows, isBrowser, isInteractive, isDocumentLoaded,
    1.10 +        getOuterId } = require("../window/utils");
    1.11 +const { InputPort } = require("./system");
    1.12 +const { lift, merges, foldp, keepIf, start, Input } = require("../event/utils");
    1.13 +const { patch } = require("diffpatcher/index");
    1.14 +const { Sequence, seq, filter, object, pairs } = require("../util/sequence");
    1.15 +
    1.16 +
    1.17 +// Create lazy iterators from the regular arrays, although
    1.18 +// once https://github.com/mozilla/addon-sdk/pull/1314 lands
    1.19 +// `windows` will be transforme to lazy iterators.
    1.20 +// When iterated over belowe sequences items will represent
    1.21 +// state of windows at the time of iteration.
    1.22 +const opened = seq(function*() {
    1.23 +  const items = windows("navigator:browser", {includePrivates: true});
    1.24 +  for (let item of items) {
    1.25 +      yield [getOuterId(item), item];
    1.26 +  }
    1.27 +});
    1.28 +const interactive = filter(([_, window]) => isInteractive(window), opened);
    1.29 +const loaded = filter(([_, window]) => isDocumentLoaded(window), opened);
    1.30 +
    1.31 +// Helper function that converts given argument to a delta.
    1.32 +const Update = window => window && object([getOuterId(window), window]);
    1.33 +const Delete = window => window && object([getOuterId(window), null]);
    1.34 +
    1.35 +
    1.36 +// Signal represents delta for last top level window close.
    1.37 +const LastClosed = lift(Delete,
    1.38 +                        keepIf(isBrowser, null,
    1.39 +                               new InputPort({topic: "domwindowclosed"})));
    1.40 +exports.LastClosed = LastClosed;
    1.41 +
    1.42 +const windowFor = document => document && document.defaultView;
    1.43 +
    1.44 +// Signal represent delta for last top level window document becoming interactive.
    1.45 +const InteractiveDoc = new InputPort({topic: "chrome-document-interactive"});
    1.46 +const InteractiveWin = lift(windowFor, InteractiveDoc);
    1.47 +const LastInteractive = lift(Update, keepIf(isBrowser, null, InteractiveWin));
    1.48 +exports.LastInteractive = LastInteractive;
    1.49 +
    1.50 +// Signal represent delta for last top level window loaded.
    1.51 +const LoadedDoc = new InputPort({topic: "chrome-document-loaded"});
    1.52 +const LoadedWin = lift(windowFor, LoadedDoc);
    1.53 +const LastLoaded = lift(Update, keepIf(isBrowser, null, LoadedWin));
    1.54 +exports.LastLoaded = LastLoaded;
    1.55 +
    1.56 +
    1.57 +const initialize = input => {
    1.58 +  if (!input.initialized) {
    1.59 +    input.value = object(...input.value);
    1.60 +    Input.start(input);
    1.61 +    input.initialized = true;
    1.62 +  }
    1.63 +};
    1.64 +
    1.65 +// Signal represents set of top level interactive windows, updated any
    1.66 +// time new window becomes interactive or one get's closed.
    1.67 +const Interactive = foldp(patch, interactive, merges([LastInteractive,
    1.68 +                                                      LastClosed]));
    1.69 +Interactive[start] = initialize;
    1.70 +exports.Interactive = Interactive;
    1.71 +
    1.72 +// Signal represents set of top level loaded window, updated any time
    1.73 +// new window becomes interactive or one get's closed.
    1.74 +const Loaded = foldp(patch, loaded, merges([LastLoaded, LastClosed]));
    1.75 +Loaded[start] = initialize;
    1.76 +exports.Loaded = Loaded;

mercurial