Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to document an event emitter returned

How to document the events emitted by stream returned in MyFunc() with JSDoc?

/**
 * [MyFunc description]
 * @param  {Object} opts - [description]
 * @return {Stream} - [description]
 */
function MyFunc (opts) {
  // stream is an EventEmitter
  var stream = new MyEventEmitter();

  stream.emit('event1', ... );
  stream.emit('event2', ... );

  return stream;
}
like image 239
leesei Avatar asked Jan 07 '16 10:01

leesei


People also ask

What is returned by emitter on event listener?

listenerCount(emitter, event) Returns the number of listeners for a given event.

What happens if an error event is emitted through an EventEmitter and nothing listens to it?

Any listeners for the error event should have a callback with one argument to capture the Error object and gracefully handle it. If an EventEmitter emits an error event, but there are no listeners subscribed for error events, the Node. js program would throw the Error that was emitted.

How are functions called when an EventEmitter object emits an event?

When the EventEmitter object emits an event, all of the functions attached to that specific event are called synchronously. Any values returned by the called listeners are ignored and discarded. The following example shows a simple EventEmitter instance with a single listener.

What do event emitters do?

An event emitter is a pattern that listens to a named event, fires a callback, then emits that event with a value. Sometimes this is referred to as a “pub/sub” model, or listener. It's referring to the same thing.


1 Answers

You can document these behaviors by documenting your events (event1, event2, ...) as @event MyFunc#event1 and MyFunc, or whoever does the emitting, with @fires MyFunc#event1.

You can also document functions that listen to those events with @listens MyFunc#event:event1.

Here are the official JSDoc pages for the aforementioned tags:

  • https://jsdoc.app/tags-event.html
  • https://jsdoc.app/tags-fires.html
  • https://jsdoc.app/tags-listens.html

Do note some nuance around "event" mentioned in the tags event page, repeating here:

JSDoc automatically prepends the namespace event: to each event's name. In general, you must include this namespace when you link to the event in another doclet. (The @fires tag is a notable exception; it allows you to omit the namespace.)

like image 123
Jasmine Hegman Avatar answered Sep 17 '22 02:09

Jasmine Hegman