Nâng CaoJavaScript iconJavaScript

Implement simple pub/sub system cho micro-frontends?

Pub/sub bus cho phép micro-frontends communicate mà không cần direct coupling.

js
const bus = {
  subs: {},
  subscribe(event, fn) {
    (this.subs[event] ||= []).push(fn);
    return () => { this.subs[event] = this.subs[event].filter(f => f !== fn); };
  },
  publish(event, data) { this.subs[event]?.forEach(fn => fn(data)); }
};

Dùng cho cross-app communication.

Thêm: typed events, wildcard, once listener.

Xem toàn bộ JavaScript cùng filter theo level & chủ đề con.

Mở danh sách JavaScript