callbacks.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * DevExtreme (core/utils/callbacks.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var Callback = function(options) {
  11. this._options = options || {};
  12. this._list = [];
  13. this._queue = [];
  14. this._firing = false;
  15. this._fired = false;
  16. this._firingIndexes = []
  17. };
  18. Callback.prototype._fireCore = function(context, args) {
  19. var firingIndexes = this._firingIndexes;
  20. var list = this._list;
  21. var stopOnFalse = this._options.stopOnFalse;
  22. var step = firingIndexes.length;
  23. for (firingIndexes[step] = 0; firingIndexes[step] < list.length; firingIndexes[step]++) {
  24. var result = list[firingIndexes[step]].apply(context, args);
  25. if (false === result && stopOnFalse) {
  26. break
  27. }
  28. }
  29. firingIndexes.pop()
  30. };
  31. Callback.prototype.add = function(fn) {
  32. if ("function" === typeof fn && (!this._options.unique || !this.has(fn))) {
  33. this._list.push(fn)
  34. }
  35. return this
  36. };
  37. Callback.prototype.remove = function(fn) {
  38. var list = this._list;
  39. var firingIndexes = this._firingIndexes;
  40. var index = list.indexOf(fn);
  41. if (index > -1) {
  42. list.splice(index, 1);
  43. if (this._firing && firingIndexes.length) {
  44. for (var step = 0; step < firingIndexes.length; step++) {
  45. if (index <= firingIndexes[step]) {
  46. firingIndexes[step]--
  47. }
  48. }
  49. }
  50. }
  51. return this
  52. };
  53. Callback.prototype.has = function(fn) {
  54. var list = this._list;
  55. return fn ? list.indexOf(fn) > -1 : !!list.length
  56. };
  57. Callback.prototype.empty = function(fn) {
  58. this._list = [];
  59. return this
  60. };
  61. Callback.prototype.fireWith = function(context, args) {
  62. var queue = this._queue;
  63. args = args || [];
  64. args = args.slice ? args.slice() : args;
  65. if (this._options.syncStrategy) {
  66. this._firing = true;
  67. this._fireCore(context, args)
  68. } else {
  69. queue.push([context, args]);
  70. if (this._firing) {
  71. return
  72. }
  73. this._firing = true;
  74. while (queue.length) {
  75. var memory = queue.shift();
  76. this._fireCore(memory[0], memory[1])
  77. }
  78. }
  79. this._firing = false;
  80. this._fired = true;
  81. return this
  82. };
  83. Callback.prototype.fire = function() {
  84. this.fireWith(this, arguments)
  85. };
  86. Callback.prototype.fired = function() {
  87. return this._fired
  88. };
  89. var Callbacks = function(options) {
  90. return new Callback(options)
  91. };
  92. module.exports = Callbacks;