Skip to content

JadsonLucena/Emitter.mjs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Emitter.mjs

CodeQL Test Pass Coverage Status JavaScript Style Guide Conventional Commits

Using the observer pattern, define listeners in your class

Which is?

The Observer is a behavioral design pattern that allows you to define a signature mechanism to notify multiple objects about any events that happen to the object they are watching.

Interfaces

/**
 * @constructor
 * @throws {TypeError} Invalid maxListeners
 */
Emitter(maxListeners?: number = 10)
// Getters
eventNames(): string[]
maxListeners(): number
// Setters
/**
 * This is a useful default that helps finding memory leaks. The value must be contained in [0,+∞) and can be set to Infinity (or 0) to indicate an unlimited number of listeners.
 * @throws {TypeError} Invalid maxListeners
 */
maxListeners(maxListeners?: number = 10): void // https://nodejs.org/api/events.html#emittersetmaxlistenersn
/**
 * @method
 * @throws {TypeError} Invalid eventName
 */
emit(eventName: string, ...args: any[]): Promise<boolean>

/**
 * @method
 * @throws {TypeError} Invalid eventName
 * @throws {TypeError} Invalid listener
 * @throws {Error} Memory leak detected
 */
addListener(eventName: string, callback: Function): Emitter

/**
 * @method
 * @throws {TypeError} Invalid eventName
 * @throws {TypeError} Invalid listener
 */
listenerCount(eventName: string, callback?: Function): number

/**
 * @method
 * @throws {TypeError} Invalid eventName
 */
listeners(eventName: string): Function[]

/**
 * @method
 * @throws {TypeError} Invalid eventName
 * @throws {TypeError} Invalid listener
 */
off(eventName: string, callback: Function): Emitter // Alias for removeListener

/**
 * @method
 * @throws {TypeError} Invalid eventName
 * @throws {TypeError} Invalid listener
 * @throws {Error} Memory leak detected
 */
on(eventName: string, callback: Function): Emitter // Alias for addListener

/**
 * @method
 * @throws {TypeError} Invalid eventName
 * @throws {TypeError} Invalid listener
 * @throws {Error} Memory leak detected
 */
once(eventName: string, callback: Function): Emitter

/**
 * @method
 * @throws {TypeError} Invalid eventName
 * @throws {TypeError} Invalid listener
 * @throws {Error} Memory leak detected
 */
prependListener(eventName: string, callback: Function): Emitter

/**
 * @method
 * @throws {TypeError} Invalid eventName
 * @throws {TypeError} Invalid listener
 * @throws {Error} Memory leak detected
 */
prependOnceListener(eventName: string, callback: Function): Emitter

/**
 * @method
 * @throws {TypeError} Invalid eventName
 */
removeAllListeners(eventName?: string): Emitter

/**
 * @method
 * @throws {TypeError} Invalid eventName
 * @throws {TypeError} Invalid listener
 */
removeListener(eventName: string, callback: Function): Emitter

/**
 * @method
 * @throws {TypeError} Invalid eventName
 */
rawListeners(eventName: string): Function[]

Specifications

We strive to maintain complete code coverage in tests. With that, we provide all the necessary use cases for a good understanding of how this module works. See: test/Emitter.spec.js

About

Using the observer pattern, define listeners in your class.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published