|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 "use strict"; |
|
5 |
|
6 const { windows, isBrowser, isInteractive, isDocumentLoaded, |
|
7 getOuterId } = require("../window/utils"); |
|
8 const { InputPort } = require("./system"); |
|
9 const { lift, merges, foldp, keepIf, start, Input } = require("../event/utils"); |
|
10 const { patch } = require("diffpatcher/index"); |
|
11 const { Sequence, seq, filter, object, pairs } = require("../util/sequence"); |
|
12 |
|
13 |
|
14 // Create lazy iterators from the regular arrays, although |
|
15 // once https://github.com/mozilla/addon-sdk/pull/1314 lands |
|
16 // `windows` will be transforme to lazy iterators. |
|
17 // When iterated over belowe sequences items will represent |
|
18 // state of windows at the time of iteration. |
|
19 const opened = seq(function*() { |
|
20 const items = windows("navigator:browser", {includePrivates: true}); |
|
21 for (let item of items) { |
|
22 yield [getOuterId(item), item]; |
|
23 } |
|
24 }); |
|
25 const interactive = filter(([_, window]) => isInteractive(window), opened); |
|
26 const loaded = filter(([_, window]) => isDocumentLoaded(window), opened); |
|
27 |
|
28 // Helper function that converts given argument to a delta. |
|
29 const Update = window => window && object([getOuterId(window), window]); |
|
30 const Delete = window => window && object([getOuterId(window), null]); |
|
31 |
|
32 |
|
33 // Signal represents delta for last top level window close. |
|
34 const LastClosed = lift(Delete, |
|
35 keepIf(isBrowser, null, |
|
36 new InputPort({topic: "domwindowclosed"}))); |
|
37 exports.LastClosed = LastClosed; |
|
38 |
|
39 const windowFor = document => document && document.defaultView; |
|
40 |
|
41 // Signal represent delta for last top level window document becoming interactive. |
|
42 const InteractiveDoc = new InputPort({topic: "chrome-document-interactive"}); |
|
43 const InteractiveWin = lift(windowFor, InteractiveDoc); |
|
44 const LastInteractive = lift(Update, keepIf(isBrowser, null, InteractiveWin)); |
|
45 exports.LastInteractive = LastInteractive; |
|
46 |
|
47 // Signal represent delta for last top level window loaded. |
|
48 const LoadedDoc = new InputPort({topic: "chrome-document-loaded"}); |
|
49 const LoadedWin = lift(windowFor, LoadedDoc); |
|
50 const LastLoaded = lift(Update, keepIf(isBrowser, null, LoadedWin)); |
|
51 exports.LastLoaded = LastLoaded; |
|
52 |
|
53 |
|
54 const initialize = input => { |
|
55 if (!input.initialized) { |
|
56 input.value = object(...input.value); |
|
57 Input.start(input); |
|
58 input.initialized = true; |
|
59 } |
|
60 }; |
|
61 |
|
62 // Signal represents set of top level interactive windows, updated any |
|
63 // time new window becomes interactive or one get's closed. |
|
64 const Interactive = foldp(patch, interactive, merges([LastInteractive, |
|
65 LastClosed])); |
|
66 Interactive[start] = initialize; |
|
67 exports.Interactive = Interactive; |
|
68 |
|
69 // Signal represents set of top level loaded window, updated any time |
|
70 // new window becomes interactive or one get's closed. |
|
71 const Loaded = foldp(patch, loaded, merges([LastLoaded, LastClosed])); |
|
72 Loaded[start] = initialize; |
|
73 exports.Loaded = Loaded; |