Request.js 5.5 KB

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