123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
- /**
- * Created by n.atlas on 17/7/2015.
- */
- var cache = {};
- var callbackFun = function callbackFun(id) {
- if (cache[id]) return cache[id];
- var random = 99999;
- var callback = {
- fns: {},
- checks: {},
- add: function add(key, fn, code, check) {
- if (typeof key === "string") {
- key = [key];
- }
- for (var i in key) {
- if (!this.fns[key[i]]) this.fns[key[i]] = {};
- if (code) this.fns[key[i]][code] = fn;else this.fns[key[i]][random++] = fn;
- if (typeof check === "function") {
- this.checks[key[i] + code] = check;
- }
- }
- },
- remove: function remove(key, code) {
- if (code) delete this.fns[key][code];else delete this.fns[key];
- },
- match: function match(url) {
- var cs = [];
- for (var i in this.fns) {
- for (var reg in this.fns[i]) {
- if (url.match(new RegExp(reg))) {
- cs.push(this.fns[i][reg]);
- }
- }
- }
- for (var _i in cs) {
- cs[_i].apply(null);
- }
- },
- execute: function execute(k) {
- var args = [];
- for (var i = 1; i < arguments.length; i++) {
- args.push(arguments[i]);
- }
- var arr = [];
- for (var j in this.fns[k]) {
- arr.push(j);
- this.fns[k][j].apply(null, args);
- }
- return arr;
- },
- executeOnce: function executeOnce(k) {
- var arr = this.execute(k);
- this.remove(k);
- return arr;
- },
- executeAndCheck: function executeAndCheck(k) {
- if (!this.fns[k]) return;
- var arr = this.execute(k);
- for (var i in this.fns[k]) {
- if (typeof this.checks[k + i] === "function") if (this.checks[k + i]()) this.remove(k, i);
- }
- if (Object.keys(this.fns[k]) == 0) delete this.fns[k];
- return arr;
- },
- getKeys: function getKeys() {
- return Object.keys(this.fns);
- },
- dispose: function dispose() {
- this.fns = {};
- },
- length: function length() {
- return Object.keys(this.fns).length;
- }
- };
- cache[id] = callback;
- return callback;
- };
- window.CallbackJS = callbackFun;
- var _default = callbackFun;
- exports.default = _default;
|