Skip to content

refactor: add event types #3706

@konstantinabl

Description

@konstantinabl

This topic arised from the following discussion #3646 (comment)

Problem
Currently, the events we emit, are not typed, so type safety is not ensured and this may bugs and harder maintainability, since when you change an event you cannot just change the type, but need to hunt down all such events in the repo and edit them

Solution
Introduce a typed event API so that emit/on calls are checked by TypeScript. Two approaches are possible:

  1. Use a third party package called typed-emitter, which allows to wrap an eventemitter and provide a type of the event
interface TransactionServiceEvents {
     [constants.EVENTS.ETH_EXECUTION]: {
       method: typeof constants.ETH_SEND_RAW_TRANSACTION
       requestDetails: RequestDetails
     }
     // …other events
   }

class TransactionService {
       private eventEmitter: TypedEmitter<TransactionServiceEvents>

       constructor() {
         this.eventEmitter = new EventEmitter() as TypedEmitter<TransactionServiceEvents>
       }
}

Pros

  • Already used by the community (npm community) and tested
  • No extra code for us to maintain

Cons

  • Extra dependency for our repo
  • If the package is deprecated it would affect us
  1. Use our own EventEmitter wrapper
class TypedEmitter<E extends Record<string, any>> extends EventEmitter {
       emit<K extends keyof E>(event: K, payload: E[K]): boolean {
         return super.emit(event as string, payload)
       }
       on<K extends keyof E>(event: K, listener: (payload: E[K]) => void) {
         return super.on(event as string, listener)
       }
     }

Pros

  • No third-party dependencies—pure in-house code
  • Customize our behaviour

Cons

  • Maintain and write it ourselves (possibly more resources towards something existing)
  • Need to keep it in sync with eventEmitter API
  • Test it ourselves

Metadata

Metadata

Assignees

Labels

Team PromotedIssues to be triaged and discussed by committers, maintainers, to be worked on the following sprintenhancementNew feature or request

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions