1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/lib/sdk/window/events.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,46 @@ 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 +module.metadata = { 1.10 + "stability": "unstable" 1.11 +}; 1.12 + 1.13 +const { Ci } = require("chrome"); 1.14 +const { observe } = require("../event/chrome"); 1.15 +const { open } = require("../event/dom"); 1.16 +const { windows } = require("../window/utils"); 1.17 +const { filter, merge, map, expand } = require("../event/utils"); 1.18 + 1.19 +// Function registers single shot event listeners for relevant window events 1.20 +// that forward events to exported event stream. 1.21 +function eventsFor(window) { 1.22 + let interactive = open(window, "DOMContentLoaded", { capture: true }); 1.23 + let complete = open(window, "load", { capture: true }); 1.24 + let states = merge([interactive, complete]); 1.25 + let changes = filter(states, function({target}) target === window.document); 1.26 + return map(changes, function({type, target}) { 1.27 + return { type: type, target: target.defaultView } 1.28 + }); 1.29 +} 1.30 + 1.31 +// In addition to observing windows that are open we also observe windows 1.32 +// that are already already opened in case they're in process of loading. 1.33 +let opened = windows(null, { includePrivate: true }); 1.34 +let currentEvents = merge(opened.map(eventsFor)); 1.35 + 1.36 +// Register system event listeners for top level window open / close. 1.37 +function rename({type, target, data}) { 1.38 + return { type: rename[type], target: target, data: data } 1.39 +} 1.40 +rename.domwindowopened = "open"; 1.41 +rename.domwindowclosed = "close"; 1.42 + 1.43 +let openEvents = map(observe("domwindowopened"), rename); 1.44 +let closeEvents = map(observe("domwindowclosed"), rename); 1.45 +let futureEvents = expand(openEvents, function({target}) eventsFor(target)); 1.46 + 1.47 +let channel = merge([currentEvents, futureEvents, 1.48 + openEvents, closeEvents]); 1.49 +exports.events = channel;