Request.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import defaultValue from "./defaultValue.js";
  2. import defined from "./defined.js";
  3. import RequestState from "./RequestState.js";
  4. import RequestType from "./RequestType.js";
  5. /**
  6. * Stores information for making a request. In general this does not need to be constructed directly.
  7. *
  8. * @alias Request
  9. * @constructor
  10. * @param {Object} [options] An object with the following properties:
  11. * @param {String} [options.url] The url to request.
  12. * @param {Request.RequestCallback} [options.requestFunction] The function that makes the actual data request.
  13. * @param {Request.CancelCallback} [options.cancelFunction] The function that is called when the request is cancelled.
  14. * @param {Request.PriorityCallback} [options.priorityFunction] The function that is called to update the request's priority, which occurs once per frame.
  15. * @param {Number} [options.priority=0.0] The initial priority of the request.
  16. * @param {Boolean} [options.throttle=false] Whether to throttle and prioritize the request. If false, the request will be sent immediately. If true, the request will be throttled and sent based on priority.
  17. * @param {Boolean} [options.throttleByServer=false] Whether to throttle the request by server.
  18. * @param {RequestType} [options.type=RequestType.OTHER] The type of request.
  19. */
  20. function Request(options) {
  21. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  22. const throttleByServer = defaultValue(options.throttleByServer, false);
  23. const throttle = defaultValue(options.throttle, false);
  24. /**
  25. * The URL to request.
  26. *
  27. * @type {String}
  28. */
  29. this.url = options.url;
  30. /**
  31. * The function that makes the actual data request.
  32. *
  33. * @type {Request.RequestCallback}
  34. */
  35. this.requestFunction = options.requestFunction;
  36. /**
  37. * The function that is called when the request is cancelled.
  38. *
  39. * @type {Request.CancelCallback}
  40. */
  41. this.cancelFunction = options.cancelFunction;
  42. /**
  43. * The function that is called to update the request's priority, which occurs once per frame.
  44. *
  45. * @type {Request.PriorityCallback}
  46. */
  47. this.priorityFunction = options.priorityFunction;
  48. /**
  49. * Priority is a unit-less value where lower values represent higher priority.
  50. * For world-based objects, this is usually the distance from the camera.
  51. * A request that does not have a priority function defaults to a priority of 0.
  52. *
  53. * If priorityFunction is defined, this value is updated every frame with the result of that call.
  54. *
  55. * @type {Number}
  56. * @default 0.0
  57. */
  58. this.priority = defaultValue(options.priority, 0.0);
  59. /**
  60. * Whether to throttle and prioritize the request. If false, the request will be sent immediately. If true, the
  61. * request will be throttled and sent based on priority.
  62. *
  63. * @type {Boolean}
  64. * @readonly
  65. *
  66. * @default false
  67. */
  68. this.throttle = throttle;
  69. /**
  70. * Whether to throttle the request by server. Browsers typically support about 6-8 parallel connections
  71. * for HTTP/1 servers, and an unlimited amount of connections for HTTP/2 servers. Setting this value
  72. * to <code>true</code> is preferable for requests going through HTTP/1 servers.
  73. *
  74. * @type {Boolean}
  75. * @readonly
  76. *
  77. * @default false
  78. */
  79. this.throttleByServer = throttleByServer;
  80. /**
  81. * Type of request.
  82. *
  83. * @type {RequestType}
  84. * @readonly
  85. *
  86. * @default RequestType.OTHER
  87. */
  88. this.type = defaultValue(options.type, RequestType.OTHER);
  89. /**
  90. * A key used to identify the server that a request is going to. It is derived from the url's authority and scheme.
  91. *
  92. * @type {String}
  93. *
  94. * @private
  95. */
  96. this.serverKey = undefined;
  97. /**
  98. * The current state of the request.
  99. *
  100. * @type {RequestState}
  101. * @readonly
  102. */
  103. this.state = RequestState.UNISSUED;
  104. /**
  105. * The requests's deferred promise.
  106. *
  107. * @type {Object}
  108. *
  109. * @private
  110. */
  111. this.deferred = undefined;
  112. /**
  113. * Whether the request was explicitly cancelled.
  114. *
  115. * @type {Boolean}
  116. *
  117. * @private
  118. */
  119. this.cancelled = false;
  120. }
  121. /**
  122. * Mark the request as cancelled.
  123. *
  124. * @private
  125. */
  126. Request.prototype.cancel = function () {
  127. this.cancelled = true;
  128. };
  129. /**
  130. * Duplicates a Request instance.
  131. *
  132. * @param {Request} [result] The object onto which to store the result.
  133. *
  134. * @returns {Request} The modified result parameter or a new Resource instance if one was not provided.
  135. */
  136. Request.prototype.clone = function (result) {
  137. if (!defined(result)) {
  138. return new Request(this);
  139. }
  140. result.url = this.url;
  141. result.requestFunction = this.requestFunction;
  142. result.cancelFunction = this.cancelFunction;
  143. result.priorityFunction = this.priorityFunction;
  144. result.priority = this.priority;
  145. result.throttle = this.throttle;
  146. result.throttleByServer = this.throttleByServer;
  147. result.type = this.type;
  148. result.serverKey = this.serverKey;
  149. // These get defaulted because the cloned request hasn't been issued
  150. result.state = this.RequestState.UNISSUED;
  151. result.deferred = undefined;
  152. result.cancelled = false;
  153. return result;
  154. };
  155. /**
  156. * The function that makes the actual data request.
  157. * @callback Request.RequestCallback
  158. * @returns {Promise<void>} A promise for the requested data.
  159. */
  160. /**
  161. * The function that is called when the request is cancelled.
  162. * @callback Request.CancelCallback
  163. */
  164. /**
  165. * The function that is called to update the request's priority, which occurs once per frame.
  166. * @callback Request.PriorityCallback
  167. * @returns {Number} The updated priority value.
  168. */
  169. export default Request;