callbackjs.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. /**
  7. * Created by n.atlas on 17/7/2015.
  8. */
  9. var cache = {};
  10. var callbackFun = function callbackFun(id) {
  11. if (cache[id]) return cache[id];
  12. var random = 99999;
  13. var callback = {
  14. fns: {},
  15. checks: {},
  16. add: function add(key, fn, code, check) {
  17. if (typeof key === "string") {
  18. key = [key];
  19. }
  20. for (var i in key) {
  21. if (!this.fns[key[i]]) this.fns[key[i]] = {};
  22. if (code) this.fns[key[i]][code] = fn;else this.fns[key[i]][random++] = fn;
  23. if (typeof check === "function") {
  24. this.checks[key[i] + code] = check;
  25. }
  26. }
  27. },
  28. remove: function remove(key, code) {
  29. if (code) delete this.fns[key][code];else delete this.fns[key];
  30. },
  31. match: function match(url) {
  32. var cs = [];
  33. for (var i in this.fns) {
  34. for (var reg in this.fns[i]) {
  35. if (url.match(new RegExp(reg))) {
  36. cs.push(this.fns[i][reg]);
  37. }
  38. }
  39. }
  40. for (var _i in cs) {
  41. cs[_i].apply(null);
  42. }
  43. },
  44. execute: function execute(k) {
  45. var args = [];
  46. for (var i = 1; i < arguments.length; i++) {
  47. args.push(arguments[i]);
  48. }
  49. var arr = [];
  50. for (var j in this.fns[k]) {
  51. arr.push(j);
  52. this.fns[k][j].apply(null, args);
  53. }
  54. return arr;
  55. },
  56. executeOnce: function executeOnce(k) {
  57. var arr = this.execute(k);
  58. this.remove(k);
  59. return arr;
  60. },
  61. executeAndCheck: function executeAndCheck(k) {
  62. if (!this.fns[k]) return;
  63. var arr = this.execute(k);
  64. for (var i in this.fns[k]) {
  65. if (typeof this.checks[k + i] === "function") if (this.checks[k + i]()) this.remove(k, i);
  66. }
  67. if (Object.keys(this.fns[k]) == 0) delete this.fns[k];
  68. return arr;
  69. },
  70. getKeys: function getKeys() {
  71. return Object.keys(this.fns);
  72. },
  73. dispose: function dispose() {
  74. this.fns = {};
  75. },
  76. length: function length() {
  77. return Object.keys(this.fns).length;
  78. }
  79. };
  80. cache[id] = callback;
  81. return callback;
  82. };
  83. window.CallbackJS = callbackFun;
  84. var _default = callbackFun;
  85. exports.default = _default;