1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/lib/sdk/content/events.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,57 @@ 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 + 1.8 +"use strict"; 1.9 + 1.10 +module.metadata = { 1.11 + "stability": "experimental" 1.12 +}; 1.13 + 1.14 +const { Ci } = require("chrome"); 1.15 +const { open } = require("../event/dom"); 1.16 +const { observe } = require("../event/chrome"); 1.17 +const { filter, merge, map, expand } = require("../event/utils"); 1.18 +const { windows } = require("../window/utils"); 1.19 +const { events: windowEvents } = require("sdk/window/events"); 1.20 + 1.21 +// Note: Please note that even though pagehide event is included 1.22 +// it's not observable reliably since it's not always triggered 1.23 +// when closing tabs. Implementation can be imrpoved once that 1.24 +// event will be necessary. 1.25 +let TYPES = ["DOMContentLoaded", "load", "pageshow", "pagehide"]; 1.26 + 1.27 +let insert = observe("document-element-inserted"); 1.28 +let windowCreate = merge([ 1.29 + observe("content-document-global-created"), 1.30 + observe("chrome-document-global-created") 1.31 +]); 1.32 +let create = map(windowCreate, function({target, data, type}) { 1.33 + return { target: target.document, type: type, data: data } 1.34 +}); 1.35 + 1.36 +function streamEventsFrom({document}) { 1.37 + // Map supported event types to a streams of those events on the given 1.38 + // `window` for the inserted document and than merge these streams into 1.39 + // single form stream off all window state change events. 1.40 + let stateChanges = TYPES.map(function(type) { 1.41 + return open(document, type, { capture: true }); 1.42 + }); 1.43 + 1.44 + // Since load events on document occur for every loded resource 1.45 + return filter(merge(stateChanges), function({target}) { 1.46 + return target instanceof Ci.nsIDOMDocument 1.47 + }) 1.48 +} 1.49 +exports.streamEventsFrom = streamEventsFrom; 1.50 + 1.51 +let opened = windows(null, { includePrivate: true }); 1.52 +let state = merge(opened.map(streamEventsFrom)); 1.53 + 1.54 + 1.55 +let futureReady = filter(windowEvents, function({type}) 1.56 + type === "DOMContentLoaded"); 1.57 +let futureWindows = map(futureReady, function({target}) target); 1.58 +let futureState = expand(futureWindows, streamEventsFrom); 1.59 + 1.60 +exports.events = merge([insert, create, state, futureState]);