All files / engine/Source/DataSources DataSourceCollection.js

95.74% Statements 90/94
84.37% Branches 27/32
100% Functions 22/22
95.65% Lines 88/92

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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336                        159x 159x 159x 159x     1x                 1768x                         791x                         621x                         181x                         1x   69x 1x       68x 68x 68x     68x 64x 64x   68x                       1x 9x   9x 9x 8x 8x   8x       8x     1x               1x 183x   183x 183x 48x 48x   48x 9x     183x                 1x 3x                 1x 5x                 1x   92x 1x       91x                 1x   1x         1x 3x           12x         12x     12x         12x       6x 6x 6x 6x   6x 2x     4x 4x 4x   4x                     1x 3x 3x                     1x 3x 3x                     1x 3x 3x 1x   2x 2x   2x                             1x 3x 3x 1x   2x 2x   2x                       1x 2x                                   1x 145x 145x      
import defined from "../Core/defined.js";
import destroyObject from "../Core/destroyObject.js";
import DeveloperError from "../Core/DeveloperError.js";
import Event from "../Core/Event.js";
import CesiumMath from "../Core/Math.js";
 
/**
 * A collection of {@link DataSource} instances.
 * @alias DataSourceCollection
 * @constructor
 */
function DataSourceCollection() {
  this._dataSources = [];
  this._dataSourceAdded = new Event();
  this._dataSourceRemoved = new Event();
  this._dataSourceMoved = new Event();
}
 
Object.defineProperties(DataSourceCollection.prototype, {
  /**
   * Gets the number of data sources in this collection.
   * @memberof DataSourceCollection.prototype
   * @type {number}
   * @readonly
   */
  length: {
    get: function () {
      return this._dataSources.length;
    },
  },
 
  /**
   * An event that is raised when a data source is added to the collection.
   * Event handlers are passed the data source that was added.
   * @memberof DataSourceCollection.prototype
   * @type {Event}
   * @readonly
   */
  dataSourceAdded: {
    get: function () {
      return this._dataSourceAdded;
    },
  },
 
  /**
   * An event that is raised when a data source is removed from the collection.
   * Event handlers are passed the data source that was removed.
   * @memberof DataSourceCollection.prototype
   * @type {Event}
   * @readonly
   */
  dataSourceRemoved: {
    get: function () {
      return this._dataSourceRemoved;
    },
  },
 
  /**
   * An event that is raised when a data source changes position in the collection.  Event handlers are passed the data source
   * that was moved, its new index after the move, and its old index prior to the move.
   * @memberof DataSourceCollection.prototype
   * @type {Event}
   * @readonly
   */
  dataSourceMoved: {
    get: function () {
      return this._dataSourceMoved;
    },
  },
});
 
/**
 * Adds a data source to the collection.
 *
 * @param {DataSource|Promise<DataSource>} dataSource A data source or a promise to a data source to add to the collection.
 *                                        When passing a promise, the data source will not actually be added
 *                                        to the collection until the promise resolves successfully.
 * @returns {Promise<DataSource>} A Promise that resolves once the data source has been added to the collection.
 */
DataSourceCollection.prototype.add = function (dataSource) {
  //>>includeStart('debug', pragmas.debug);
  if (!defined(dataSource)) {
    throw new DeveloperError("dataSource is required.");
  }
  //>>includeEnd('debug');
 
  const that = this;
  const dataSources = this._dataSources;
  return Promise.resolve(dataSource).then(function (value) {
    //Only add the data source if removeAll has not been called
    //Since it was added.
    if (dataSources === that._dataSources) {
      that._dataSources.push(value);
      that._dataSourceAdded.raiseEvent(that, value);
    }
    return value;
  });
};
 
/**
 * Removes a data source from this collection, if present.
 *
 * @param {DataSource} dataSource The data source to remove.
 * @param {boolean} [destroy=false] Whether to destroy the data source in addition to removing it.
 * @returns {boolean} true if the data source was in the collection and was removed,
 *                    false if the data source was not in the collection.
 */
DataSourceCollection.prototype.remove = function (dataSource, destroy) {
  destroy = destroy ?? false;
 
  const index = this._dataSources.indexOf(dataSource);
  if (index !== -1) {
    this._dataSources.splice(index, 1);
    this._dataSourceRemoved.raiseEvent(this, dataSource);
 
    Iif (destroy && typeof dataSource.destroy === "function") {
      dataSource.destroy();
    }
 
    return true;
  }
 
  return false;
};
 
/**
 * Removes all data sources from this collection.
 *
 * @param {boolean} [destroy=false] whether to destroy the data sources in addition to removing them.
 */
DataSourceCollection.prototype.removeAll = function (destroy) {
  destroy = destroy ?? false;
 
  const dataSources = this._dataSources;
  for (let i = 0, len = dataSources.length; i < len; ++i) {
    const dataSource = dataSources[i];
    this._dataSourceRemoved.raiseEvent(this, dataSource);
 
    if (destroy && typeof dataSource.destroy === "function") {
      dataSource.destroy();
    }
  }
  this._dataSources = [];
};
 
/**
 * Checks to see if the collection contains a given data source.
 *
 * @param {DataSource} dataSource The data source to check for.
 * @returns {boolean} true if the collection contains the data source, false otherwise.
 */
DataSourceCollection.prototype.contains = function (dataSource) {
  return this.indexOf(dataSource) !== -1;
};
 
/**
 * Determines the index of a given data source in the collection.
 *
 * @param {DataSource} dataSource The data source to find the index of.
 * @returns {number} The index of the data source in the collection, or -1 if the data source does not exist in the collection.
 */
DataSourceCollection.prototype.indexOf = function (dataSource) {
  return this._dataSources.indexOf(dataSource);
};
 
/**
 * Gets a data source by index from the collection.
 *
 * @param {number} index the index to retrieve.
 * @returns {DataSource} The data source at the specified index.
 */
DataSourceCollection.prototype.get = function (index) {
  //>>includeStart('debug', pragmas.debug);
  if (!defined(index)) {
    throw new DeveloperError("index is required.");
  }
  //>>includeEnd('debug');
 
  return this._dataSources[index];
};
 
/**
 * Gets a data source by name from the collection.
 *
 * @param {string} name The name to retrieve.
 * @returns {DataSource[]} A list of all data sources matching the provided name.
 */
DataSourceCollection.prototype.getByName = function (name) {
  //>>includeStart('debug', pragmas.debug);
  Iif (!defined(name)) {
    throw new DeveloperError("name is required.");
  }
  //>>includeEnd('debug');
 
  return this._dataSources.filter(function (dataSource) {
    return dataSource.name === name;
  });
};
 
function getIndex(dataSources, dataSource) {
  //>>includeStart('debug', pragmas.debug);
  Iif (!defined(dataSource)) {
    throw new DeveloperError("dataSource is required.");
  }
  //>>includeEnd('debug');
 
  const index = dataSources.indexOf(dataSource);
 
  //>>includeStart('debug', pragmas.debug);
  Iif (index === -1) {
    throw new DeveloperError("dataSource is not in this collection.");
  }
  //>>includeEnd('debug');
 
  return index;
}
 
function swapDataSources(collection, i, j) {
  const arr = collection._dataSources;
  const length = arr.length - 1;
  i = CesiumMath.clamp(i, 0, length);
  j = CesiumMath.clamp(j, 0, length);
 
  if (i === j) {
    return;
  }
 
  const temp = arr[i];
  arr[i] = arr[j];
  arr[j] = temp;
 
  collection.dataSourceMoved.raiseEvent(temp, j, i);
}
 
/**
 * Raises a data source up one position in the collection.
 *
 * @param {DataSource} dataSource The data source to move.
 *
 * @exception {DeveloperError} dataSource is not in this collection.
 * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
 */
DataSourceCollection.prototype.raise = function (dataSource) {
  const index = getIndex(this._dataSources, dataSource);
  swapDataSources(this, index, index + 1);
};
 
/**
 * Lowers a data source down one position in the collection.
 *
 * @param {DataSource} dataSource The data source to move.
 *
 * @exception {DeveloperError} dataSource is not in this collection.
 * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
 */
DataSourceCollection.prototype.lower = function (dataSource) {
  const index = getIndex(this._dataSources, dataSource);
  swapDataSources(this, index, index - 1);
};
 
/**
 * Raises a data source to the top of the collection.
 *
 * @param {DataSource} dataSource The data source to move.
 *
 * @exception {DeveloperError} dataSource is not in this collection.
 * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
 */
DataSourceCollection.prototype.raiseToTop = function (dataSource) {
  const index = getIndex(this._dataSources, dataSource);
  if (index === this._dataSources.length - 1) {
    return;
  }
  this._dataSources.splice(index, 1);
  this._dataSources.push(dataSource);
 
  this.dataSourceMoved.raiseEvent(
    dataSource,
    this._dataSources.length - 1,
    index,
  );
};
 
/**
 * Lowers a data source to the bottom of the collection.
 *
 * @param {DataSource} dataSource The data source to move.
 *
 * @exception {DeveloperError} dataSource is not in this collection.
 * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
 */
DataSourceCollection.prototype.lowerToBottom = function (dataSource) {
  const index = getIndex(this._dataSources, dataSource);
  if (index === 0) {
    return;
  }
  this._dataSources.splice(index, 1);
  this._dataSources.splice(0, 0, dataSource);
 
  this.dataSourceMoved.raiseEvent(dataSource, 0, index);
};
 
/**
 * Returns true if this object was destroyed; otherwise, false.
 * If this object was destroyed, it should not be used; calling any function other than
 * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
 *
 * @returns {boolean} true if this object was destroyed; otherwise, false.
 *
 * @see DataSourceCollection#destroy
 */
DataSourceCollection.prototype.isDestroyed = function () {
  return false;
};
 
/**
 * Destroys the resources held by all data sources in this collection.  Explicitly destroying this
 * object allows for deterministic release of WebGL resources, instead of relying on the garbage
 * collector. Once this object is destroyed, it should not be used; calling any function other than
 * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.  Therefore,
 * assign the return value (<code>undefined</code>) to the object as done in the example.
 *
 * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
 *
 *
 * @example
 * dataSourceCollection = dataSourceCollection && dataSourceCollection.destroy();
 *
 * @see DataSourceCollection#isDestroyed
 */
DataSourceCollection.prototype.destroy = function () {
  this.removeAll(true);
  return destroyObject(this);
};
export default DataSourceCollection;