runner.js 5.2 KB

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