UNPKG

peepee

Version:

Visual Programming Language Where You Connect Ports Of One EventEmitter to Ports Of Another EventEmitter

72 lines (57 loc) 2.74 kB
Certainly! Below is the updated implementation of the `Correlator` class that extends the `EventEmitter` class. This allows the `Correlator` to directly emit events and manage its own subscriptions while still providing the correlation functionality. ```javascript const EventEmitter = require('events'); class Correlator extends EventEmitter { constructor() { super(); this.correlationData = {}; this.sep = ':'; // Separator for event name and correlation ID } // Start correlating events correlate(correlationEventName, ...eventNames) { eventNames.forEach(eventName => { const [event, correlationField] = eventName.split(this.sep); this.on(event, (data) => { this.correlationData[correlationField] = data[correlationField]; this.checkCorrelation(correlationEventName); }); }); } // Check if all required events have been received checkCorrelation(correlationEventName) { const packet = {}; let allReceived = true; for (const key in this.correlationData) { if (this.correlationData[key] === undefined) { allReceived = false; break; } packet[key] = this.correlationData[key]; } if (allReceived) { this.emit(correlationEventName, packet); // Reset correlation data after processing this.correlationData = {}; } } } // Example usage: const correlator = new Correlator(); // Correlate events correlator.correlate('checkout:completed', 'checkout:transactionId', 'paid:transactionId'); // Subscribe to the correlation event correlator.on('checkout:completed', (data) => { console.log('Correlation event emitted:', data); }); // Publish events correlator.emit('checkout', { transactionId: '12345' }); correlator.emit('paid', { transactionId: '12345' }); // Output: Correlation event emitted: { transactionId: '12345' } ``` ### Explanation: - **Correlator Class**: The `Correlator` class now extends `EventEmitter`, allowing it to directly use the `on` and `emit` methods for event handling. - **correlate Method**: This method subscribes to the specified events and stores the relevant data in `correlationData`. - **checkCorrelation Method**: This method checks if all required correlation fields have been received and emits the correlation event with the collected data if they have. ### Example Usage: - The example demonstrates how to correlate a `checkout:completed` event with `checkout:transactionId` and `paid:transactionId`. When both events are emitted, the correlator emits the correlation event with the relevant data. This implementation is clean and leverages the capabilities of `EventEmitter` while providing the correlation functionality in a straightforward manner.