|
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, isInteractive, isDocumentLoaded, |
|
7 getOuterId, isTopLevel } = 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 { on } = require("../event/core"); |
|
12 const { Sequence, seq, filter, object, pairs } = require("../util/sequence"); |
|
13 |
|
14 |
|
15 // Create lazy iterators from the regular arrays, although |
|
16 // once https://github.com/mozilla/addon-sdk/pull/1314 lands |
|
17 // `windows` will be transforme to lazy iterators. |
|
18 // When iterated over belowe sequences items will represent |
|
19 // state of windows at the time of iteration. |
|
20 const opened = seq(function*() { |
|
21 const items = windows(null, {includePrivates: true}); |
|
22 for (let item of items) |
|
23 yield [getOuterId(item), item]; |
|
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 // Signal represents delta for last opened top level window. |
|
33 const LastOpened = lift(Update, new InputPort({topic: "domwindowopened"})); |
|
34 exports.LastOpened = LastOpened; |
|
35 |
|
36 // Signal represents delta for last top level window close. |
|
37 const LastClosed = lift(Delete, new InputPort({topic: "domwindowclosed"})); |
|
38 exports.LastClosed = LastClosed; |
|
39 |
|
40 const windowFor = document => document && document.defaultView; |
|
41 |
|
42 // Signal represent delta for last top level window document becoming interactive. |
|
43 const InteractiveDoc = new InputPort({topic: "chrome-document-interactive"}); |
|
44 const InteractiveWin = lift(windowFor, InteractiveDoc); |
|
45 const LastInteractive = lift(Update, keepIf(isTopLevel, null, InteractiveWin)); |
|
46 exports.LastInteractive = LastInteractive; |
|
47 |
|
48 // Signal represent delta for last top level window loaded. |
|
49 const LoadedDoc = new InputPort({topic: "chrome-document-loaded"}); |
|
50 const LoadedWin = lift(windowFor, LoadedDoc); |
|
51 const LastLoaded = lift(Update, keepIf(isTopLevel, null, LoadedWin)); |
|
52 exports.LastLoaded = LastLoaded; |
|
53 |
|
54 |
|
55 const initialize = input => { |
|
56 if (!input.initialized) { |
|
57 input.value = object(...input.value); |
|
58 Input.start(input); |
|
59 input.initialized = true; |
|
60 } |
|
61 }; |
|
62 |
|
63 // Signal represents set of currently opened top level windows, updated |
|
64 // to new set any time window is opened or closed. |
|
65 const Opened = foldp(patch, opened, merges([LastOpened, LastClosed])); |
|
66 Opened[start] = initialize; |
|
67 exports.Opened = Opened; |
|
68 |
|
69 // Signal represents set of top level interactive windows, updated any |
|
70 // time new window becomes interactive or one get's closed. |
|
71 const Interactive = foldp(patch, interactive, merges([LastInteractive, |
|
72 LastClosed])); |
|
73 Interactive[start] = initialize; |
|
74 exports.Interactive = Interactive; |
|
75 |
|
76 // Signal represents set of top level loaded window, updated any time |
|
77 // new window becomes interactive or one get's closed. |
|
78 const Loaded = foldp(patch, loaded, merges([LastLoaded, LastClosed])); |
|
79 Loaded[start] = initialize; |
|
80 exports.Loaded = Loaded; |