Simple EventEmitter

emit and receive events!

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/426233/948861/Simple%20EventEmitter.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

class EventEmitter {
    constructor() {
        this.registry = {}
    }
    addListener(e, r) {
        return this.registry[e] || (this.registry[e] = []), this.registry[e].push(r), {
            remove: () => this.removeListener(e, r)
        }
    }
    once(e, r, t) {
        const s = this.addListener(e, (...e) => {
            s.remove(), r.apply(t, e)
        });
        return s
    }
    removeAllListeners(e) {
        this.registry[e] = []
    }
    removeSubscription(e) {
        e.remove()
    }
    listeners(e) {
        return this.registry[e]
    }
    emit(e, ...r) {
        const t = this.registry[e];
        t && t.forEach(e => e(...r))
    }
    removeListener(e, r) {
        const t = this.registry[e];
        if (!t) return;
        const s = t.indexOf(r); - 1 !== s && (t.splice(s, 1), 0 === t.length && delete this.registry[e])
    }
}