js 最简单的发布订阅模式
2021-03-27 07:26
标签:auto val UNC cti ble 发布订阅 console 最简 OLE 执行后: js 最简单的发布订阅模式 标签:auto val UNC cti ble 发布订阅 console 最简 OLE 原文地址:https://www.cnblogs.com/ajanuw/p/12633112.htmllet _subscriber: any;
function autorun(subscriber: Function) {
_subscriber = subscriber;
_subscriber();
_subscriber = null;
}
class Observer {
#list: Function[] = []; // 订阅者
private get _last() {
if (!this.#list.length) return null;
return this.#list[this.#list.length - 1];
}
// 添加订阅者
add() {
if (this._last !== _subscriber) {
this.#list.push(_subscriber);
}
}
// 发布时,把订阅者挨着挨着call
publish() {
this.#list.forEach((it: any) => it());
}
}
function observable(data: any) {
const o: Map
λ ts-node index.ts
ajanuw
hello ajanuw...
suou
hello suou...
suou
hello suou......