Design an EventEmitter class similar to Node.js event system or DOM Event Target interface. The class should allow subscribing to events and emitting them with callback execution.
Your EventEmitter class should have the following methods:
subscribe(eventName, callback): Takes an event name (string) and callback function. Multiple listeners can subscribe to the same event and will be called in subscription order. Returns an object with an unsubscribe method to remove the callback.
emit(eventName, args): Takes an event name (string) and optional array of arguments to pass to callbacks. Returns an array of results from all callback executions in subscription order, or empty array if no listeners exist.
Input & Output
Constraints
- 1 ≤ operations.length ≤ 100
- Event names are non-empty strings
- Callbacks are valid JavaScript functions
- Arguments passed to emit are valid JSON values