michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: module.metadata = { michael@0: "stability": "unstable" michael@0: }; michael@0: michael@0: const { Ci } = require("chrome"); michael@0: const { observe } = require("../event/chrome"); michael@0: const { open } = require("../event/dom"); michael@0: const { windows } = require("../window/utils"); michael@0: const { filter, merge, map, expand } = require("../event/utils"); michael@0: michael@0: // Function registers single shot event listeners for relevant window events michael@0: // that forward events to exported event stream. michael@0: function eventsFor(window) { michael@0: let interactive = open(window, "DOMContentLoaded", { capture: true }); michael@0: let complete = open(window, "load", { capture: true }); michael@0: let states = merge([interactive, complete]); michael@0: let changes = filter(states, function({target}) target === window.document); michael@0: return map(changes, function({type, target}) { michael@0: return { type: type, target: target.defaultView } michael@0: }); michael@0: } michael@0: michael@0: // In addition to observing windows that are open we also observe windows michael@0: // that are already already opened in case they're in process of loading. michael@0: let opened = windows(null, { includePrivate: true }); michael@0: let currentEvents = merge(opened.map(eventsFor)); michael@0: michael@0: // Register system event listeners for top level window open / close. michael@0: function rename({type, target, data}) { michael@0: return { type: rename[type], target: target, data: data } michael@0: } michael@0: rename.domwindowopened = "open"; michael@0: rename.domwindowclosed = "close"; michael@0: michael@0: let openEvents = map(observe("domwindowopened"), rename); michael@0: let closeEvents = map(observe("domwindowclosed"), rename); michael@0: let futureEvents = expand(openEvents, function({target}) eventsFor(target)); michael@0: michael@0: let channel = merge([currentEvents, futureEvents, michael@0: openEvents, closeEvents]); michael@0: exports.events = channel;