Friday, 12 May 2017

How to share *only* the events of an object in node.js?

I'd like for an object B to subscribe to the events than an object A emits—however, I don't want to pass the object B itself to A, because of the potential for another developer (or myself in the future) to abuse presence of B in A, and thus tightly couple the two items. Is there a way I can pass only the event-emitter aspect of B to A, so that A can receive events from (i.e. read) B, but not manipulate (i.e. write to) B?

A somewhat simple solution I can think of would be to create a new EventEmitter C, then subscribe C to all events of B, with handlers that emit the same events, and then pass C to A.

Is there a simpler solution?



via fearpi

No comments:

Post a Comment