addon-sdk/source/lib/sdk/input/window.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/window.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,80 @@
     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, isInteractive, isDocumentLoaded,
    1.10 +        getOuterId, isTopLevel } = 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 { on } = require("../event/core");
    1.15 +const { Sequence,  seq, filter, object, pairs } = require("../util/sequence");
    1.16 +
    1.17 +
    1.18 +// Create lazy iterators from the regular arrays, although
    1.19 +// once https://github.com/mozilla/addon-sdk/pull/1314 lands
    1.20 +// `windows` will be transforme to lazy iterators.
    1.21 +// When iterated over belowe sequences items will represent
    1.22 +// state of windows at the time of iteration.
    1.23 +const opened = seq(function*() {
    1.24 +  const items = windows(null, {includePrivates: true});
    1.25 +  for (let item of items)
    1.26 +    yield [getOuterId(item), item];
    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 +// Signal represents delta for last opened top level window.
    1.36 +const LastOpened = lift(Update, new InputPort({topic: "domwindowopened"}));
    1.37 +exports.LastOpened = LastOpened;
    1.38 +
    1.39 +// Signal represents delta for last top level window close.
    1.40 +const LastClosed = lift(Delete, new InputPort({topic: "domwindowclosed"}));
    1.41 +exports.LastClosed = LastClosed;
    1.42 +
    1.43 +const windowFor = document => document && document.defaultView;
    1.44 +
    1.45 +// Signal represent delta for last top level window document becoming interactive.
    1.46 +const InteractiveDoc = new InputPort({topic: "chrome-document-interactive"});
    1.47 +const InteractiveWin = lift(windowFor, InteractiveDoc);
    1.48 +const LastInteractive = lift(Update, keepIf(isTopLevel, null, InteractiveWin));
    1.49 +exports.LastInteractive = LastInteractive;
    1.50 +
    1.51 +// Signal represent delta for last top level window loaded.
    1.52 +const LoadedDoc = new InputPort({topic: "chrome-document-loaded"});
    1.53 +const LoadedWin = lift(windowFor, LoadedDoc);
    1.54 +const LastLoaded = lift(Update, keepIf(isTopLevel, null, LoadedWin));
    1.55 +exports.LastLoaded = LastLoaded;
    1.56 +
    1.57 +
    1.58 +const initialize = input => {
    1.59 +  if (!input.initialized) {
    1.60 +    input.value = object(...input.value);
    1.61 +    Input.start(input);
    1.62 +    input.initialized = true;
    1.63 +  }
    1.64 +};
    1.65 +
    1.66 +// Signal represents set of currently opened top level windows, updated
    1.67 +// to new set any time window is opened or closed.
    1.68 +const Opened = foldp(patch, opened, merges([LastOpened, LastClosed]));
    1.69 +Opened[start] = initialize;
    1.70 +exports.Opened = Opened;
    1.71 +
    1.72 +// Signal represents set of top level interactive windows, updated any
    1.73 +// time new window becomes interactive or one get's closed.
    1.74 +const Interactive = foldp(patch, interactive, merges([LastInteractive,
    1.75 +                                                      LastClosed]));
    1.76 +Interactive[start] = initialize;
    1.77 +exports.Interactive = Interactive;
    1.78 +
    1.79 +// Signal represents set of top level loaded window, updated any time
    1.80 +// new window becomes interactive or one get's closed.
    1.81 +const Loaded = foldp(patch, loaded, merges([LastLoaded, LastClosed]));
    1.82 +Loaded[start] = initialize;
    1.83 +exports.Loaded = Loaded;

mercurial