All files / engine/Source/Core Request.js

100% Statements 33/33
100% Branches 12/12
100% Functions 3/3
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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