1

I am trying to understand this snippet of Node.js code.

Roee.js

const emitters = require('events');
module.exports=class Roee extends emitters{
    constructor(eee){
    super();
    const emit = this.emit.bind(this);
    this.emit=undefined;
    eee(emit);
    }
};

ticker.js

     const emiCl = require('./roee');
     const tikee = new emiCl((emiff) => {
           let tickcnt=0;
           setInterval(() => {
                  emiff('ticker',tickcnt++)
            }, 1200);
      }
     );
    module.exports = tikee;

tester.js

const mytickel = require('./tiker');
mytickel.on('ticker', (tickcount) => console.log(tickcount,'tempdfg'));

My questions:

Roee.js: The constructor takes an arg which is used as a function.

ticker.js: defines the function which is passed in roee.js. This function takes a parameter, which is again a function, right?

tester.js: how is this able to access the counter and pass argument to the function emiff('ticker',tickcnt++)?

I am at beginner stage.

1 Answer 1

1

Roee.js:: The constructor takes an arg which is used as a function.

Yes. This constructor is sort of using the callback pattern, calling your function with the emit function on the instance of that class. This is an unusual thing to do.

ticker.js:: defines the function which is passed in roee.js. This function takes a parameter, which is again a function, right?

That's correct. In the callback from constructor (which is a bit weird to have), the parameter passed to this callback is a function bound to emit events from the instance of the Roee class.

tester.js: how is this able to access the counter and pass argument to the function "emiff('ticker',tickcnt++)" ?

tickcnt is in the closure set up by the callback function from the constructor. Since tickcnt is referenced from within that setInterval, it won't be garbage collected.

Now, how does that outer code get the value? The code in ticket.js calls the emit method on the Roee class (which is extending event emitter). Anything with a reference to that instance of Roee can add listeners to events, including the ticker event. When that event is fired, the data for the current tick is passed through it.

Really, this code pattern is pretty terrible in general. There are few circumstances where something outside of a class should be triggering events on that class. It's possible I'm not understanding something from the translation, but I point this code pattern issue out so that you know your trouble understanding is not just you, and not your fault.

Sign up to request clarification or add additional context in comments.

5 Comments

mytickel has a reference of Roee class. Is it mandatory that only this reference can act if an event 'ticker' is fired. Or can there be another class inheriting from event, which can also listen to 'ticker' event and take action of its own ?
@user2775371 Sure, things can inherit and that event be fired. In fact, that's what you've done. Event triggering is inherited from EventEmitter, in your example.
i mean a new emitter, like the one below: const EventEmitter = require('events'); class MyEmmitter extends EventEmitter {} const myEmitter = new MyEmmitter(); myEmitter.on('ticker', (tickcount) => console.log(tickcount,'trumpet'));
@user2775371 Sure, that can be done. The on becomes just a method you can call on that instance.
class MyEmmitter extends EventEmitter {} const myEmitter = new MyEmmitter(); myEmitter.on('ticker', (tickcount) => console.log(tickcount,'trumpet')); myEmitter.on('ticker'.... is not printing "trumpet" .. why ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.