-
Notifications
You must be signed in to change notification settings - Fork 0
/
xcall.source.js
97 lines (93 loc) · 3.89 KB
/
xcall.source.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.$call = {}));
})(this, (function (exports) { 'use strict';
var XCall = (function () {
function XCall() {
this._callBackMap = {};
}
XCall.getInstance = function () {
if (!this.instance) {
this.instance = new XCall();
}
return this.instance;
};
XCall.hasCallBack = function (eventKey, callBack) {
var _a, _b;
if (!XCall.getInstance()._callBackMap[eventKey]) {
return false;
}
return (_b = (_a = XCall.getInstance()._callBackMap[eventKey]) === null || _a === void 0 ? void 0 : _a.includes(callBack)) !== null && _b !== void 0 ? _b : false;
};
XCall.existEvent = function (eventKey) {
var _a;
if (!XCall.getInstance()._callBackMap[eventKey]) {
return false;
}
return !!((_a = XCall.getInstance()._callBackMap[eventKey]) === null || _a === void 0 ? void 0 : _a.length);
};
XCall.addCallBack = function (eventKey, callback) {
var _a;
if (!XCall.getInstance()._callBackMap[eventKey]) {
XCall.getInstance()._callBackMap[eventKey] = [];
}
if (!XCall.hasCallBack(eventKey, callback)) {
(_a = XCall.getInstance()._callBackMap[eventKey]) === null || _a === void 0 ? void 0 : _a.push(callback);
}
};
XCall.setOnceEvent = function (eventKey, callback) {
function _callback() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
callback === null || callback === void 0 ? void 0 : callback.apply(null, args);
XCall.deleteEvent(eventKey);
}
XCall.addCallBack(eventKey, _callback);
};
XCall.removeCallBack = function (eventKey, callback) {
var _a;
if (!XCall.getInstance()._callBackMap[eventKey]) {
return;
}
XCall.getInstance()._callBackMap[eventKey] = (_a = XCall.getInstance()._callBackMap[eventKey]) === null || _a === void 0 ? void 0 : _a.filter(function (callBackFunc) { return callBackFunc != callback; });
if (!XCall.getInstance()._callBackMap[eventKey].length) {
XCall.deleteEvent(eventKey);
}
};
XCall.deleteEvent = function (eventKey) {
if (!XCall.getInstance()._callBackMap[eventKey]) {
return;
}
Reflect.deleteProperty(XCall.getInstance()._callBackMap, eventKey);
};
XCall.getCount = function (eventKey) {
if (!eventKey) {
return Object.keys(XCall.getInstance()._callBackMap).length;
}
if (!XCall.getInstance()._callBackMap[eventKey]) {
return 0;
}
return XCall.getInstance()._callBackMap[eventKey].length;
};
XCall.dispatch = function (eventKey) {
var _this = this;
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
if (!XCall.getInstance()._callBackMap[eventKey]) {
throw new Error("\u672A\u627E\u5230\u56DE\u8C03\u4E8B\u4EF6 ".concat(eventKey, " \u7684\u76D1\u542C"));
}
XCall.getInstance()._callBackMap[eventKey].forEach(function (callBackFunc) {
callBackFunc === null || callBackFunc === void 0 ? void 0 : callBackFunc.apply(_this, args);
});
};
return XCall;
}());
exports.XCall = XCall;
exports["default"] = XCall;
Object.defineProperty(exports, '__esModule', { value: true });
}));