runner.mjs 5.1 KB

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