runner.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*!
  2. * @pixi/runner - v6.5.10
  3. * Compiled Thu, 06 Jul 2023 15:25:11 UTC
  4. *
  5. * @pixi/runner is licensed under the MIT License.
  6. * http://www.opensource.org/licenses/mit-license
  7. */
  8. this.PIXI = this.PIXI || {};
  9. var _pixi_runner = (function (exports) {
  10. 'use strict';
  11. /**
  12. * A Runner is a highly performant and simple alternative to signals. Best used in situations
  13. * where events are dispatched to many objects at high frequency (say every frame!)
  14. *
  15. *
  16. * like a signal..
  17. * ```
  18. * import { Runner } from '@pixi/runner';
  19. *
  20. * const myObject = {
  21. * loaded: new Runner('loaded')
  22. * }
  23. *
  24. * const listener = {
  25. * loaded: function(){
  26. * // thin
  27. * }
  28. * }
  29. *
  30. * myObject.loaded.add(listener);
  31. *
  32. * myObject.loaded.emit();
  33. * ```
  34. *
  35. * Or for handling calling the same function on many items
  36. * ```
  37. * import { Runner } from '@pixi/runner';
  38. *
  39. * const myGame = {
  40. * update: new Runner('update')
  41. * }
  42. *
  43. * const gameObject = {
  44. * update: function(time){
  45. * // update my gamey state
  46. * }
  47. * }
  48. *
  49. * myGame.update.add(gameObject);
  50. *
  51. * myGame.update.emit(time);
  52. * ```
  53. * @memberof PIXI
  54. */
  55. var Runner = /** @class */ (function () {
  56. /**
  57. * @param name - The function name that will be executed on the listeners added to this Runner.
  58. */
  59. function Runner(name) {
  60. this.items = [];
  61. this._name = name;
  62. this._aliasCount = 0;
  63. }
  64. /* eslint-disable jsdoc/require-param, jsdoc/check-param-names */
  65. /**
  66. * Dispatch/Broadcast Runner to all listeners added to the queue.
  67. * @param {...any} params - (optional) parameters to pass to each listener
  68. */
  69. /* eslint-enable jsdoc/require-param, jsdoc/check-param-names */
  70. Runner.prototype.emit = function (a0, a1, a2, a3, a4, a5, a6, a7) {
  71. if (arguments.length > 8) {
  72. throw new Error('max arguments reached');
  73. }
  74. var _a = this, name = _a.name, items = _a.items;
  75. this._aliasCount++;
  76. for (var i = 0, len = items.length; i < len; i++) {
  77. items[i][name](a0, a1, a2, a3, a4, a5, a6, a7);
  78. }
  79. if (items === this.items) {
  80. this._aliasCount--;
  81. }
  82. return this;
  83. };
  84. Runner.prototype.ensureNonAliasedItems = function () {
  85. if (this._aliasCount > 0 && this.items.length > 1) {
  86. this._aliasCount = 0;
  87. this.items = this.items.slice(0);
  88. }
  89. };
  90. /**
  91. * Add a listener to the Runner
  92. *
  93. * Runners do not need to have scope or functions passed to them.
  94. * All that is required is to pass the listening object and ensure that it has contains a function that has the same name
  95. * as the name provided to the Runner when it was created.
  96. *
  97. * Eg A listener passed to this Runner will require a 'complete' function.
  98. *
  99. * ```
  100. * import { Runner } from '@pixi/runner';
  101. *
  102. * const complete = new Runner('complete');
  103. * ```
  104. *
  105. * The scope used will be the object itself.
  106. * @param {any} item - The object that will be listening.
  107. */
  108. Runner.prototype.add = function (item) {
  109. if (item[this._name]) {
  110. this.ensureNonAliasedItems();
  111. this.remove(item);
  112. this.items.push(item);
  113. }
  114. return this;
  115. };
  116. /**
  117. * Remove a single listener from the dispatch queue.
  118. * @param {any} item - The listener that you would like to remove.
  119. */
  120. Runner.prototype.remove = function (item) {
  121. var index = this.items.indexOf(item);
  122. if (index !== -1) {
  123. this.ensureNonAliasedItems();
  124. this.items.splice(index, 1);
  125. }
  126. return this;
  127. };
  128. /**
  129. * Check to see if the listener is already in the Runner
  130. * @param {any} item - The listener that you would like to check.
  131. */
  132. Runner.prototype.contains = function (item) {
  133. return this.items.indexOf(item) !== -1;
  134. };
  135. /** Remove all listeners from the Runner */
  136. Runner.prototype.removeAll = function () {
  137. this.ensureNonAliasedItems();
  138. this.items.length = 0;
  139. return this;
  140. };
  141. /** Remove all references, don't use after this. */
  142. Runner.prototype.destroy = function () {
  143. this.removeAll();
  144. this.items = null;
  145. this._name = null;
  146. };
  147. Object.defineProperty(Runner.prototype, "empty", {
  148. /**
  149. * `true` if there are no this Runner contains no listeners
  150. * @readonly
  151. */
  152. get: function () {
  153. return this.items.length === 0;
  154. },
  155. enumerable: false,
  156. configurable: true
  157. });
  158. Object.defineProperty(Runner.prototype, "name", {
  159. /**
  160. * The name of the runner.
  161. * @readonly
  162. */
  163. get: function () {
  164. return this._name;
  165. },
  166. enumerable: false,
  167. configurable: true
  168. });
  169. return Runner;
  170. }());
  171. Object.defineProperties(Runner.prototype, {
  172. /**
  173. * Alias for `emit`
  174. * @memberof PIXI.Runner#
  175. * @method dispatch
  176. * @see PIXI.Runner#emit
  177. */
  178. dispatch: { value: Runner.prototype.emit },
  179. /**
  180. * Alias for `emit`
  181. * @memberof PIXI.Runner#
  182. * @method run
  183. * @see PIXI.Runner#emit
  184. */
  185. run: { value: Runner.prototype.emit },
  186. });
  187. exports.Runner = Runner;
  188. Object.defineProperty(exports, '__esModule', { value: true });
  189. return exports;
  190. })({});
  191. Object.assign(this.PIXI, _pixi_runner);
  192. //# sourceMappingURL=runner.js.map