class Publisher {
  constructor() {
    this.subscribers = new Map();
  }

  subscribe(type, fn) {
    if (!this.subscribers.has(type)) {
      this.subscribers.set(type, []);
    }
    this.subscribers.get(type).push(fn);
  }

  unsubscribe(type, fn) {
    const subscribers = this.subscribers.get(type);
    if (subscribers) {
      const index = subscribers.indexOf(fn);
      if (index !== -1) {
        subscribers.splice(index, 1);
      }
    }
  }

  publish(type, ...data) {
    const subscribers = this.subscribers.get(type);
    if (subscribers) {
      subscribers.forEach(fn => fn(...data));
    }
  }
}

// 创建单例对象
const publisherInstance = new Publisher();
export default publisherInstance;
