Simple EventEmitter

emit and receive events!

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

  1. class EventEmitter {
  2. constructor() {
  3. this.registry = {}
  4. }
  5. addListener(e, r) {
  6. return this.registry[e] || (this.registry[e] = []), this.registry[e].push(r), {
  7. remove: () => this.removeListener(e, r)
  8. }
  9. }
  10. once(e, r, t) {
  11. const s = this.addListener(e, (...e) => {
  12. s.remove(), r.apply(t, e)
  13. });
  14. return s
  15. }
  16. removeAllListeners(e) {
  17. this.registry[e] = []
  18. }
  19. removeSubscription(e) {
  20. e.remove()
  21. }
  22. listeners(e) {
  23. return this.registry[e]
  24. }
  25. emit(e, ...r) {
  26. const t = this.registry[e];
  27. t && t.forEach(e => e(...r))
  28. }
  29. removeListener(e, r) {
  30. const t = this.registry[e];
  31. if (!t) return;
  32. const s = t.indexOf(r); - 1 !== s && (t.splice(s, 1), 0 === t.length && delete this.registry[e])
  33. }
  34. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址