Source: Request.js

  1. const ee = require('event-emitter')
  2. const SortedCallbacks = require('./SortedCallbacks')
  3. /**
  4. * A compiled query
  5. * @typedef {Object} Request#SubRequest
  6. * @property {string} query - The compiled code
  7. * @property {object[]} parts - An entry for each part (separated by the 'out count' separator)
  8. * @property {int} parts[].properties - The properties which each returned map feature has set (TAGS, BBOX, ...)
  9. * @property {int} effort - Supposed "effort" of this query
  10. * @property {int} count - Count of discovered items
  11. * @property {Request} request - The request this compiled query belongs to
  12. */
  13. /**
  14. * An unspecified request
  15. * @param {OverpassFrontend} overpass
  16. * @param {object} options
  17. */
  18. class Request {
  19. constructor (overpass, data) {
  20. this.overpass = overpass
  21. for (const k in data) {
  22. this[k] = data[k]
  23. }
  24. if (!this.options) {
  25. this.options = {}
  26. }
  27. this.priority = 'priority' in this.options ? this.options.priority : 0
  28. const callbacks = new SortedCallbacks(this.options, this.featureCallback, this.finalCallback)
  29. this.featureCallback = callbacks.next.bind(callbacks)
  30. this.finalCallback = callbacks.final.bind(callbacks)
  31. this.count = 0
  32. this.callCount = 0
  33. this.timestampPreprocess = 0
  34. }
  35. /**
  36. * Request got aborted
  37. * @event Request#abort
  38. */
  39. /**
  40. * abort this request
  41. */
  42. abort () {
  43. this.aborted = true
  44. this.emit('abort')
  45. this.overpass._abortRequest(this)
  46. }
  47. /**
  48. * Request is finished
  49. * @event Request#finish
  50. * @param {Error|null} - null if no error occured
  51. */
  52. /**
  53. * request is finished
  54. * @param {Error|null} err - null if no error occured
  55. */
  56. finish (err) {
  57. if (!this.aborted) {
  58. this.finalCallback(err)
  59. }
  60. this.overpass._finishRequest(this)
  61. this.finished = true
  62. this.emit('finish', err)
  63. }
  64. /**
  65. * shall this Request be included in the current call?
  66. * @param {OverpassFrontend#Context} context - Current context
  67. * @return {boolean} - yes|no
  68. */
  69. willInclude (context) {
  70. return true
  71. }
  72. /**
  73. * @typedef {Object} Request#minMaxEffortResult
  74. * @property {number} Remaining minimal effort of this request
  75. * @property {number|null} Remaining maximum effort (or null if unknown)
  76. */
  77. /**
  78. * how much effort can a call to this request use
  79. * @return {Request#minMaxEffortResult} - minimum and maximum effort
  80. */
  81. minMaxEffort () {
  82. return { minEffort: 0, maxEffort: 0 }
  83. }
  84. /**
  85. * SubRequest got compiled
  86. * @event Request#subrequest-compiile
  87. * @param {Request#SubRequest} subRequest - the sub request
  88. */
  89. /**
  90. * compile the query
  91. * @param {OverpassFrontend#Context} context - Current context
  92. * @return {Request#SubRequest} - the compiled query
  93. */
  94. compileQuery (context) {
  95. const subRequest = this._compileQuery(context)
  96. this.emit('subrequest-compile', subRequest)
  97. this.callCount++
  98. return subRequest
  99. }
  100. /**
  101. * receive an object from OverpassFronted -> enter to cache, return to caller
  102. * @param {OverpassObject} ob - Object which has been received
  103. * @param {Request#SubRequest} subRequest - sub request which is being handled right now
  104. * @param {int} partIndex - Which part of the subRequest is being received
  105. */
  106. receiveObject (ob) {
  107. this.count++
  108. }
  109. /**
  110. * SubRequest got finished
  111. * @event Request#subrequest-finished
  112. * @param {Request#SubRequest} subRequest - the sub request
  113. */
  114. /**
  115. * the current subrequest is finished -> update caches, check whether request is finished
  116. * @param {Request#SubRequest} subRequest - the current sub request
  117. */
  118. finishSubRequest (subRequest) {
  119. this.emit('subrequest-finish', subRequest)
  120. }
  121. }
  122. ee(Request.prototype)
  123. module.exports = Request