All files event-bus.js

81.81% Statements 99/121
68.62% Branches 35/51
83.33% Functions 30/36
85.71% Lines 84/98

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200          1x       16x   16x             16x 16x 16x 16x 16x   16x       4x 4x     4x 3x   4x   4x 1x   3x   4x       1x                                   26x 26x 26x 26x   26x 26x 26x 26x 26x   26x 26x 26x   26x 26x                 26x         4x 4x 5x 4x       1x 1x 1x 1x       4x 4x 4x 4x 4x       15x 15x 15x               1x 1x   2x 2x 2x             4x 5x   1x 1x     4x 4x 4x       26x       52x 5x           26x 26x   4x 4x 4x   4x   4x       26x 26x         1x 1x         1x 1x 2x 2x 1x 1x 1x         1x  
/**
 * Event Bus - 事件总线系统
 * 版本:v1.0.0
 */
 
const EventEmitter = require('events');
 
class EventBus extends EventEmitter {
  constructor(options = {}) {
    super();
    
    this.options = {
      maxListeners: 100,
      historySize: options.historySize || 1000,
      enableHistory: options.enableHistory !== false,
      ...options
    };
    
    this.interceptors = { before: [], after: [] };
    this.history = [];
    this.historyIndex = 0;
    this.contexts = new Map();
    this.listenerMap = new Map(); // 追踪原始监听器到包装监听器的映射
    
    this.setMaxListeners(this.options.maxListeners);
  }
 
  on(event, listener, options = {}) {
    const { priority = 0, once = false } = options;
    const wrappedListener = this._wrapListener(listener, { event, priority, once });
    
    // 保存映射
    if (!this.listenerMap.has(event)) {
      this.listenerMap.set(event, []);
    }
    this.listenerMap.get(event).push({ original: listener, wrapped: wrappedListener });
    
    if (once) {
      super.once(event, wrappedListener);
    } else {
      super.on(event, wrappedListener);
    }
    return this;
  }
 
  once(event, listener, options = {}) {
    return this.on(event, listener, { ...options, once: true });
  }
 
  off(event, listener) {
    // 找到包装的监听器
    const listeners = this.listenerMap.get(event) || [];
    const mapping = listeners.find(m => m.original === listener);
    
    if (mapping) {
      super.removeListener(event, mapping.wrapped);
      // 移除映射
      const index = listeners.indexOf(mapping);
      if (index > -1) listeners.splice(index, 1);
    }
    return this;
  }
 
  async emit(event, payload, context = {}) {
    const eventId = this._generateEventId();
    const timestamp = new Date().toISOString();
    const eventRecord = { id: eventId, event, payload, context, timestamp, status: 'pending', duration: 0 };
    const startTime = Date.now();
    
    try {
      await this._executeInterceptors('before', event, payload, context);
      this.contexts.set(eventId, { ...context, eventId, timestamp });
      const result = await this._triggerEvent(event, payload, context);
      await this._executeInterceptors('after', event, payload, context, result);
      
      eventRecord.status = 'completed';
      eventRecord.result = result;
      eventRecord.duration = Date.now() - startTime;
      
      Eif (this.options.enableHistory) this._addToHistory(eventRecord);
      return result;
    } catch (error) {
      eventRecord.status = 'failed';
      eventRecord.error = error.message;
      eventRecord.duration = Date.now() - startTime;
      if (this.options.enableHistory) this._addToHistory(eventRecord);
      throw error;
    } finally {
      // 延迟清理上下文,让测试可以获取
      setTimeout(() => this.contexts.delete(eventId), 100);
    }
  }
 
  addBeforeInterceptor(interceptor, options = {}) {
    const { priority = 0 } = options;
    this.interceptors.before.push({ interceptor, priority });
    this.interceptors.before.sort((a, b) => b.priority - a.priority);
    return this;
  }
 
  addAfterInterceptor(interceptor, options = {}) {
    const { priority = 0 } = options;
    this.interceptors.after.push({ interceptor, priority });
    this.interceptors.after.sort((a, b) => b.priority - a.priority);
    return this;
  }
 
  getHistory(filter = {}) {
    let records = [...this.history];
    if (filter.event) records = records.filter(r => r.event === filter.event);
    Iif (filter.status) records = records.filter(r => r.status === filter.status);
    const limit = filter.limit || this.options.historySize;
    return records.slice(-limit);
  }
 
  clearHistory() {
    this.history = [];
    this.historyIndex = 0;
    return this;
  }
 
  getContext(eventId) {
    return this.contexts.get(eventId) || null;
  }
 
  getStats() {
    const history = this.history;
    return {
      totalEvents: history.length,
      completedEvents: history.filter(r => r.status === 'completed').length,
      failedEvents: history.filter(r => r.status === 'failed').length,
      avgDuration: history.length > 0 ? history.reduce((sum, r) => sum + r.duration, 0) / history.length : 0,
      listenerCount: this.listenerCount ? this.listenerCount() : 0,
      contextCount: this.contexts.size
    };
  }
 
  _wrapListener(listener, meta) {
    const wrapped = async (...args) => {
      try { return await listener(...args); }
      catch (error) {
        console.error(`[EventBus] 监听器错误 (${meta.event}):`, error);
        throw error;
      }
    };
    wrapped._originalListener = listener;
    wrapped._meta = meta;
    return wrapped;
  }
 
  _generateEventId() {
    return `evt_${Date.now()}_${++this.historyIndex}`;
  }
 
  async _executeInterceptors(type, event, payload, context, result) {
    for (const { interceptor } of this.interceptors[type]) {
      try { await interceptor({ event, payload, context, result, type }); }
      catch (error) { console.error(`[EventBus] ${type}拦截器错误:`, error); }
    }
  }
 
  async _triggerEvent(event, payload, context) {
    const listeners = this.listeners(event);
    if (listeners.length === 0) return { event, payload, handled: false };
    
    const results = await Promise.allSettled(listeners.map(listener => listener(payload, context)));
    const fulfilled = results.filter(r => r.status === 'fulfilled').map(r => r.value);
    const rejected = results.filter(r => r.status === 'rejected').map(r => r.reason);
    
    if (rejected.length > 0) console.warn(`[EventBus] ${rejected.length} 个监听器失败`);
    
    return { event, payload, handled: true, results: fulfilled, errors: rejected.map(e => e.message) };
  }
 
  _addToHistory(record) {
    this.history.push(record);
    Iif (this.history.length > this.options.historySize) this.history.shift();
  }
}
 
function loggingInterceptor() {
  return async ({ event, payload, type }) => {
    console.log(`[EventBus:Logging] ${type} - ${event}`, { payloadSize: JSON.stringify(payload).length });
  };
}
 
function performanceInterceptor() {
  const timings = new Map();
  return async ({ event, type, context }) => {
    const key = `${event}_${context.eventId}`;
    if (type === 'before') timings.set(key, Date.now());
    else Eif (type === 'after' && timings.has(key)) {
      console.log(`[EventBus:Performance] ${event} 耗时:${Date.now() - timings.get(key)}ms`);
      timings.delete(key);
    }
  };
}
 
module.exports = { EventBus, loggingInterceptor, performanceInterceptor };