summaryrefslogtreecommitdiffstats
path: root/toolkit/jetpack/sdk/input/browser.js
diff options
context:
space:
mode:
authorMatt A. Tobin <email@mattatobin.com>2018-02-09 06:46:43 -0500
committerMatt A. Tobin <email@mattatobin.com>2018-02-09 06:46:43 -0500
commitac46df8daea09899ce30dc8fd70986e258c746bf (patch)
tree2750d3125fc253fd5b0671e4bd268eff1fd97296 /toolkit/jetpack/sdk/input/browser.js
parent8cecf8d5208f3945b35f879bba3015bb1a11bec6 (diff)
downloadUXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar.gz
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar.lz
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar.xz
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.zip
Move Add-on SDK source to toolkit/jetpack
Diffstat (limited to 'toolkit/jetpack/sdk/input/browser.js')
-rw-r--r--toolkit/jetpack/sdk/input/browser.js73
1 files changed, 73 insertions, 0 deletions
diff --git a/toolkit/jetpack/sdk/input/browser.js b/toolkit/jetpack/sdk/input/browser.js
new file mode 100644
index 000000000..daea875bf
--- /dev/null
+++ b/toolkit/jetpack/sdk/input/browser.js
@@ -0,0 +1,73 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { windows, isBrowser, isInteractive, isDocumentLoaded,
+ getOuterId } = require("../window/utils");
+const { InputPort } = require("./system");
+const { lift, merges, foldp, keepIf, start, Input } = require("../event/utils");
+const { patch } = require("diffpatcher/index");
+const { Sequence, seq, filter, object, pairs } = require("../util/sequence");
+
+
+// Create lazy iterators from the regular arrays, although
+// once https://github.com/mozilla/addon-sdk/pull/1314 lands
+// `windows` will be transforme to lazy iterators.
+// When iterated over belowe sequences items will represent
+// state of windows at the time of iteration.
+const opened = seq(function*() {
+ const items = windows("navigator:browser", {includePrivate: true});
+ for (let item of items) {
+ yield [getOuterId(item), item];
+ }
+});
+const interactive = filter(([_, window]) => isInteractive(window), opened);
+const loaded = filter(([_, window]) => isDocumentLoaded(window), opened);
+
+// Helper function that converts given argument to a delta.
+const Update = window => window && object([getOuterId(window), window]);
+const Delete = window => window && object([getOuterId(window), null]);
+
+
+// Signal represents delta for last top level window close.
+const LastClosed = lift(Delete,
+ keepIf(isBrowser, null,
+ new InputPort({topic: "domwindowclosed"})));
+exports.LastClosed = LastClosed;
+
+const windowFor = document => document && document.defaultView;
+
+// Signal represent delta for last top level window document becoming interactive.
+const InteractiveDoc = new InputPort({topic: "chrome-document-interactive"});
+const InteractiveWin = lift(windowFor, InteractiveDoc);
+const LastInteractive = lift(Update, keepIf(isBrowser, null, InteractiveWin));
+exports.LastInteractive = LastInteractive;
+
+// Signal represent delta for last top level window loaded.
+const LoadedDoc = new InputPort({topic: "chrome-document-loaded"});
+const LoadedWin = lift(windowFor, LoadedDoc);
+const LastLoaded = lift(Update, keepIf(isBrowser, null, LoadedWin));
+exports.LastLoaded = LastLoaded;
+
+
+const initialize = input => {
+ if (!input.initialized) {
+ input.value = object(...input.value);
+ Input.start(input);
+ input.initialized = true;
+ }
+};
+
+// Signal represents set of top level interactive windows, updated any
+// time new window becomes interactive or one get's closed.
+const Interactive = foldp(patch, interactive, merges([LastInteractive,
+ LastClosed]));
+Interactive[start] = initialize;
+exports.Interactive = Interactive;
+
+// Signal represents set of top level loaded window, updated any time
+// new window becomes interactive or one get's closed.
+const Loaded = foldp(patch, loaded, merges([LastLoaded, LastClosed]));
+Loaded[start] = initialize;
+exports.Loaded = Loaded;