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