|
@@ -1,71 +1,67 @@
|
|
|
+const objToString = (param: any) => Object.prototype.toString.call(param);
|
|
|
+
|
|
|
class Observer {
|
|
|
- public handle
|
|
|
+ private handle: Map<string, [Function?]>
|
|
|
constructor() {
|
|
|
- this.handle = {};
|
|
|
+ this.handle = new Map()
|
|
|
};
|
|
|
|
|
|
on(type = '', callback: Function) {
|
|
|
|
|
|
if (typeof type !== 'string') {
|
|
|
- const msg = `on type must is string, now is ${Object.prototype.toString.call(type)}`;
|
|
|
+ const msg = `on type must is string, now is ${objToString(type)}`;
|
|
|
throw new Error(msg);
|
|
|
};
|
|
|
if (typeof callback !== 'function') {
|
|
|
- const msg = `callback must is function, now is ${Object.prototype.toString.call(callback)}`;
|
|
|
+ const msg = `callback must is function, now is ${objToString(callback)}`;
|
|
|
throw new Error(msg);
|
|
|
};
|
|
|
|
|
|
- if (this.handle[type] !== '[object Array]') {
|
|
|
- this.handle[type] = [];
|
|
|
+ if (!this.handle.has(type)) {
|
|
|
+ this.handle.set(type, []);
|
|
|
};
|
|
|
- this.handle[type].push(callback);
|
|
|
+ this.handle.get(type)?.push(callback);
|
|
|
};
|
|
|
|
|
|
- emit(type = '', ...params) {
|
|
|
+ emit (type = '', ...params: []) {
|
|
|
if (typeof type !== 'string') {
|
|
|
- const msg = `emit type must is string, now is ${Object.prototype.toString.call(type)}`;
|
|
|
+ const msg = `emit type must is string, now is ${objToString(type)}`;
|
|
|
throw new Error(msg);
|
|
|
};
|
|
|
- if (!this.handle[type]) {
|
|
|
+ if (!this.handle.get(type)?.length) {
|
|
|
const msg = `${type} is not exist`;
|
|
|
throw new Error(msg);
|
|
|
};
|
|
|
- const forHandle = (el) => {
|
|
|
- if (Object.prototype.toString.call(el) === '[object Function]') {
|
|
|
- el(...params);
|
|
|
+ const forHandle = (el: Function | undefined) => {
|
|
|
+ if (objToString(el) === '[object Function]') {
|
|
|
+ el && el(...params);
|
|
|
} else {
|
|
|
throw new Error(`${type} is not function`);
|
|
|
};
|
|
|
}
|
|
|
- this.handle[type].forEach(forHandle.bind(this));
|
|
|
+ this.handle.get(type)?.forEach(forHandle.bind(this));
|
|
|
}
|
|
|
|
|
|
destory(type = '') {
|
|
|
if (typeof type !== 'string') {
|
|
|
- const msg = `emit type must is string, now is ${Object.prototype.toString.call(type)}`;
|
|
|
+ const msg = `emit type must is string, now is ${objToString(type)}`;
|
|
|
throw new Error(msg);
|
|
|
};
|
|
|
- if (!this.handle[type] || !this.handle[type].length) {
|
|
|
+ if (!this.handle.get(type)?.length) {
|
|
|
throw new Error(`${type} is not register`);
|
|
|
};
|
|
|
- delete this.handle[type];
|
|
|
+ this.handle.delete(type);
|
|
|
}
|
|
|
|
|
|
list(type = '') {
|
|
|
if (!type) {
|
|
|
throw new Error(`${type} is not exist`);
|
|
|
};
|
|
|
- if (this.handle[type]) {
|
|
|
- return true;
|
|
|
+ if (this.handle.has(type)) {
|
|
|
+ return this.handle.get(type);
|
|
|
};
|
|
|
return null;
|
|
|
};
|
|
|
};
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-module.exports = new Observer();
|
|
|
+export default new Observer();
|