history.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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": "unstable",
  7. "engines": {
  8. "Firefox": "*"
  9. }
  10. };
  11. /*
  12. * Requiring hosts so they can subscribe to client messages
  13. */
  14. require('./host/host-bookmarks');
  15. require('./host/host-tags');
  16. require('./host/host-query');
  17. const { Cc, Ci } = require('chrome');
  18. const { Class } = require('../core/heritage');
  19. const { events, send } = require('../addon/events');
  20. const { defer, reject, all } = require('../core/promise');
  21. const { uuid } = require('../util/uuid');
  22. const { flatten } = require('../util/array');
  23. const { has, extend, merge, pick } = require('../util/object');
  24. const { emit } = require('../event/core');
  25. const { defer: async } = require('../lang/functional');
  26. const { EventTarget } = require('../event/target');
  27. const {
  28. urlQueryParser, createQuery, createQueryOptions
  29. } = require('./utils');
  30. /*
  31. * Constant used by nsIHistoryQuery; 0 is a history query
  32. * https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsINavHistoryQueryOptions
  33. */
  34. const HISTORY_QUERY = 0;
  35. let search = function query (queries, options) {
  36. queries = [].concat(queries);
  37. let emitter = EventTarget();
  38. let queryObjs = queries.map(createQuery.bind(null, HISTORY_QUERY));
  39. let optionsObj = createQueryOptions(HISTORY_QUERY, options);
  40. // Can remove after `Promise.jsm` is implemented in Bug 881047,
  41. // which will guarantee next tick execution
  42. async(() => {
  43. send('sdk-places-query', {
  44. query: queryObjs,
  45. options: optionsObj
  46. }).then(results => {
  47. results.map(item => emit(emitter, 'data', item));
  48. emit(emitter, 'end', results);
  49. }, reason => {
  50. emit(emitter, 'error', reason);
  51. emit(emitter, 'end', []);
  52. });
  53. })();
  54. return emitter;
  55. };
  56. exports.search = search;