class Sub {
events = {};
on(type, func) {
!Array.isArray(this.events[type]) ? (this.events[type] = []) : null;
let arr = this.events[type];
if (arr.includes(func)) return;
arr.push(func);
}
off(type, func) {
let arr = this.events[type];
let item = null;
if (!Array.isArray(this.events[type])) {
throw new TypeError(`${type} 自定义事件在事件池中并不存在!`);
}
for (let i = 0; i < arr.length; i++) {
item = arr[i];
if (item === func) {
// 这样只是让集合中当前项值变为null,但是集合的机构是不发生改变的「索引不变」;
// 下一次执行emit的时候,遇到当前项是null,我们再去把其移除掉即可;
arr[i] = null;
break;
}
}
}
emit(type, ...params) {
let arr = this.events[type];
let item = null;
for (let i = 0; i < arr.length; i++) {
item = arr[i];
if (typeof item === "function") {
item(...params);
continue;
}
//不是函数的值都移除掉即可
arr.splice(i, 1);
i--;
}
}
}
const sub = new Sub();
const fn1 = () => console.log(1);
const fn2 = () => console.log(2);
const fn3 = () => {
console.log(3);
sub.off("A", fn1);
sub.off("A", fn2);
};
const fn4 = () => console.log(4);
const fn5 = () => console.log(5);
const fn6 = () => console.log(6);
sub.on("A", fn1);
// sub.on("A", fn1);
sub.on("A", fn2);
sub.on("A", fn3);
sub.on("A", fn4);
sub.on("A", fn5);
sub.on("A", fn6);
setTimeout(() => {
sub.emit("A");
}, 1000);
setTimeout(() => {
sub.emit("A");
}, 2000);
手写发布订阅类
最新推荐文章于 2025-03-18 23:19:21 发布