Naked Panda Meditations

    @notenoughupdates/events-intercept
    TypeScript icon, indicating that this package has built-in type declarations

    3.0.1 • Public • Published

    events-intercept

    Build Status Coverage Status

    The node EventEmitter is very powerful. However, at times it could be valuable to intercept events before they reach their handlers, to modify the data, or emit other events. That's a job for event-intercept.

    Installation

    npm install events-intercept

    Standalone Usage

    The module contains a constructor, EventEmitter, which inherits from the standard node events.EventEmitter.

    import { EventEmitter } from "events-intercept";
    const emitter = new EventEmitter();

    In our application, we have an object that will emit a data event, and pass it a single argument.

    emitter.emit("data", "myData");

    It is very easy to listen for this event and handle it

    emitter.on("data", (arg) => {
      console.log(arg);
    }); //logs 'myData'

    However, we want to intercept that event and modify the data. We can do that by setting an interceptor with intercept(event, interceptor). It is passed all arguments that would be passed to the emitter, as well as a standard node callback. In this case, let's just add a prefix on to the data.

    emitter.intercept("data", (arg, done) => {
      return done(null, "intercepted " + arg);
    });

    This code will be executed before the handler, and the new argument will be passed on to the handler appropriately.

    emitter.emit("data", "some other data");
    //logs 'intercepted some other data'

    If multiple interceptors are added to a single event, they will be called in the order that they are added, like async.waterfall.

    Here's that sample code all together. Of course, intercept supports proper function chaining.

    import eventsIntercept from "events-intercept";
    const emitter = new eventsIntercept.EventEmitter();
    
    emitter
      .on("data", (arg) => console.log(arg))
      .intercept("data", (arg, done) => done(null, "intercepted " + arg))
      .emit("data", "myData");
    //logs 'intercepted myData'

    Please see test/intercept.js for more complete samples.

    Calling Separate Events

    There may be times when you want to intercept one event and call another. Luckily, all intercept handlers are called with the EventEmitter as the this context, so you can emit events yourself.

    emitter.intercept("data", (done) => {
      this.emit("otherData").emit("thirdData");
      return done(null);
    });
    //emits 'data', 'otherData', and 'thirdData'

    Remember, emitting an event that you are intercepting will cause a loop, so be careful.

    In fact, an interceptor do not need to call the callback at all, which means that the event that was intercepted will never be called at all.

    emitter.intercept("data", (done) => {
      this.emit("otherData").emit("thirdData");
    });
    //emits 'otherData' and 'thirdData' but not 'data'

    Utilities

    events-intercept supports all of the useful utilities that the standard EventEmitter supports:

    • interceptors(type) returns an array of all interceptors (functions) for the given type.
    • removeInterceptor(type, interceptor) removes an interceptor of a given type. You must pass in the interceptor function.
    • removeAllInterceptors(type) removes all interceptors for a given type.
    • removeAllInterceptors() removes all interceptors. Will remove the removeInterceptor event last, so they will all get triggered.
    • the EventEmitter will throw a warning if more than 10 interceptors are added to a single event, as this could represent a memory leak. setMaxInterceptors(n) allows you to change that. Set it to 0 for no limit.

    All of these are demonstrated in the tests.

    Patching

    Of course, many EventEmitters that you have the pleasure of using will not have the foresight to use event-intercept. Thankfully, Javascript is awesome, it's possible to monkey patch the interception capabilities onto an existing object. Just call

    import events from "events";
    import eventsIntercept from "events-intercept";
    
    const emitter = new events.EventEmitter();
    
    eventsIntercept.patch(emitter);
    
    emitter
      .on("data", (arg) => console.log(arg))
      .intercept("data", (arg, done) => done(null, "intercepted " + arg))
      .emit("data", "myData");
    //logs 'intercepted myData'

    Now, you should be able to call intercept on the standard EventEmitter.

    This is also shown in test/intercept.js.

    Install

    npm i @notenoughupdates/events-intercept

    DownloadsWeekly Downloads

    4

    Version

    3.0.1

    License

    MIT

    Unpacked Size

    81.3 kB

    Total Files

    10

    Last publish

    Collaborators

    • ironm00n