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;
}
listenerCount(emitter, event) Returns the number of listeners for a given event.
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.
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.
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.
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:
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With