events.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. module.metadata = {
  6. "stability": "experimental"
  7. };
  8. const { Ci } = require("chrome");
  9. const { open } = require("../event/dom");
  10. const { observe } = require("../event/chrome");
  11. const { filter, merge, map, expand } = require("../event/utils");
  12. const { windows } = require("../window/utils");
  13. const { events: windowEvents } = require("sdk/window/events");
  14. // Note: Please note that even though pagehide event is included
  15. // it's not observable reliably since it's not always triggered
  16. // when closing tabs. Implementation can be imrpoved once that
  17. // event will be necessary.
  18. let TYPES = ["DOMContentLoaded", "load", "pageshow", "pagehide"];
  19. let insert = observe("document-element-inserted");
  20. let windowCreate = merge([
  21. observe("content-document-global-created"),
  22. observe("chrome-document-global-created")
  23. ]);
  24. let create = map(windowCreate, function({target, data, type}) {
  25. return { target: target.document, type: type, data: data }
  26. });
  27. function streamEventsFrom({document}) {
  28. // Map supported event types to a streams of those events on the given
  29. // `window` for the inserted document and than merge these streams into
  30. // single form stream off all window state change events.
  31. let stateChanges = TYPES.map(function(type) {
  32. return open(document, type, { capture: true });
  33. });
  34. // Since load events on document occur for every loded resource
  35. return filter(merge(stateChanges), function({target}) {
  36. return target instanceof Ci.nsIDOMDocument
  37. })
  38. }
  39. exports.streamEventsFrom = streamEventsFrom;
  40. let opened = windows(null, { includePrivate: true });
  41. let state = merge(opened.map(streamEventsFrom));
  42. let futureReady = filter(windowEvents, function({type})
  43. type === "DOMContentLoaded");
  44. let futureWindows = map(futureReady, function({target}) target);
  45. let futureState = expand(futureWindows, streamEventsFrom);
  46. exports.events = merge([insert, create, state, futureState]);