DataSourceCollection.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*global define*/
  2. define([
  3. '../Core/defaultValue',
  4. '../Core/defined',
  5. '../Core/defineProperties',
  6. '../Core/destroyObject',
  7. '../Core/DeveloperError',
  8. '../Core/Event'
  9. ], function(
  10. defaultValue,
  11. defined,
  12. defineProperties,
  13. destroyObject,
  14. DeveloperError,
  15. Event) {
  16. "use strict";
  17. /**
  18. * A collection of {@link DataSource} instances.
  19. * @alias DataSourceCollection
  20. * @constructor
  21. */
  22. var DataSourceCollection = function() {
  23. this._dataSources = [];
  24. this._dataSourceAdded = new Event();
  25. this._dataSourceRemoved = new Event();
  26. };
  27. defineProperties(DataSourceCollection.prototype, {
  28. /**
  29. * Gets the number of data sources in this collection.
  30. * @memberof DataSourceCollection.prototype
  31. * @type {Number}
  32. * @readonly
  33. */
  34. length : {
  35. get : function() {
  36. return this._dataSources.length;
  37. }
  38. },
  39. /**
  40. * An event that is raised when a data source is added to the collection.
  41. * Event handlers are passed the data source that was added.
  42. * @memberof DataSourceCollection.prototype
  43. * @type {Event}
  44. * @readonly
  45. */
  46. dataSourceAdded : {
  47. get : function() {
  48. return this._dataSourceAdded;
  49. }
  50. },
  51. /**
  52. * An event that is raised when a data source is removed from the collection.
  53. * Event handlers are passed the data source that was removed.
  54. * @memberof DataSourceCollection.prototype
  55. * @type {Event}
  56. * @readonly
  57. */
  58. dataSourceRemoved : {
  59. get : function() {
  60. return this._dataSourceRemoved;
  61. }
  62. }
  63. });
  64. /**
  65. * Adds a data source to the collection.
  66. *
  67. * @param {DataSource} dataSource The data source to add.
  68. */
  69. DataSourceCollection.prototype.add = function(dataSource) {
  70. //>>includeStart('debug', pragmas.debug);
  71. if (!defined(dataSource)) {
  72. throw new DeveloperError('dataSource is required.');
  73. }
  74. //>>includeEnd('debug');
  75. this._dataSources.push(dataSource);
  76. this._dataSourceAdded.raiseEvent(this, dataSource);
  77. };
  78. /**
  79. * Removes a data source from this collection, if present.
  80. *
  81. * @param {DataSource} dataSource The data source to remove.
  82. * @param {Boolean} [destroy=false] Whether to destroy the data source in addition to removing it.
  83. * @returns {Boolean} true if the data source was in the collection and was removed,
  84. * false if the data source was not in the collection.
  85. */
  86. DataSourceCollection.prototype.remove = function(dataSource, destroy) {
  87. destroy = defaultValue(destroy, false);
  88. var index = this._dataSources.indexOf(dataSource);
  89. if (index !== -1) {
  90. this._dataSources.splice(index, 1);
  91. this._dataSourceRemoved.raiseEvent(this, dataSource);
  92. if (destroy && typeof dataSource.destroy === 'function') {
  93. dataSource.destroy();
  94. }
  95. return true;
  96. }
  97. return false;
  98. };
  99. /**
  100. * Removes all data sources from this collection.
  101. *
  102. * @param {Boolean} [destroy=false] whether to destroy the data sources in addition to removing them.
  103. */
  104. DataSourceCollection.prototype.removeAll = function(destroy) {
  105. destroy = defaultValue(destroy, false);
  106. var dataSources = this._dataSources;
  107. for (var i = 0, len = dataSources.length; i < len; ++i) {
  108. var dataSource = dataSources[i];
  109. this._dataSourceRemoved.raiseEvent(this, dataSource);
  110. if (destroy && typeof dataSource.destroy === 'function') {
  111. dataSource.destroy();
  112. }
  113. }
  114. dataSources.length = 0;
  115. };
  116. /**
  117. * Checks to see if the collection contains a given data source.
  118. *
  119. * @param {DataSource} dataSource The data source to check for.
  120. * @returns {Boolean} true if the collection contains the data source, false otherwise.
  121. */
  122. DataSourceCollection.prototype.contains = function(dataSource) {
  123. return this.indexOf(dataSource) !== -1;
  124. };
  125. /**
  126. * Determines the index of a given data source in the collection.
  127. *
  128. * @param {DataSource} dataSource The data source to find the index of.
  129. * @returns {Number} The index of the data source in the collection, or -1 if the data source does not exist in the collection.
  130. */
  131. DataSourceCollection.prototype.indexOf = function(dataSource) {
  132. return this._dataSources.indexOf(dataSource);
  133. };
  134. /**
  135. * Gets a data source by index from the collection.
  136. *
  137. * @param {Number} index the index to retrieve.
  138. */
  139. DataSourceCollection.prototype.get = function(index) {
  140. //>>includeStart('debug', pragmas.debug);
  141. if (!defined(index)) {
  142. throw new DeveloperError('index is required.');
  143. }
  144. //>>includeEnd('debug');
  145. return this._dataSources[index];
  146. };
  147. /**
  148. * Returns true if this object was destroyed; otherwise, false.
  149. * If this object was destroyed, it should not be used; calling any function other than
  150. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  151. *
  152. * @returns {Boolean} true if this object was destroyed; otherwise, false.
  153. *
  154. * @see DataSourceCollection#destroy
  155. */
  156. DataSourceCollection.prototype.isDestroyed = function() {
  157. return false;
  158. };
  159. /**
  160. * Destroys the resources held by all data sources in this collection. Explicitly destroying this
  161. * object allows for deterministic release of WebGL resources, instead of relying on the garbage
  162. * collector. Once this object is destroyed, it should not be used; calling any function other than
  163. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  164. * assign the return value (<code>undefined</code>) to the object as done in the example.
  165. *
  166. * @returns {undefined}
  167. *
  168. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  169. *
  170. * @see DataSourceCollection#isDestroyed
  171. *
  172. * @example
  173. * dataSourceCollection = dataSourceCollection && dataSourceCollection.destroy();
  174. */
  175. DataSourceCollection.prototype.destroy = function() {
  176. this.removeAll(true);
  177. return destroyObject(this);
  178. };
  179. return DataSourceCollection;
  180. });