Hello and welcome back to Code Review, a series of coding interview challenges and career related content released weekly exclusively on Dev.to. Iβ...
For further actions, you may consider blocking this person and/or reporting abuse
That's a good question, and the solution is really clearπ
I'm not sure I'd call the
emit
function a closure, it's not returning a function, and it's not being run outside it's lexical scope (although the callbacks are closures themselves I guess)Not the emit function - the event callbacks themself close over the
data
object and gain access to that themselves!That's not so different than a typical implementation:
github.com/Olical/EventEmitter/blo...
Kudos
Regardless of whatever you two decide is a better answer, its always nice to see other ways of solving the same problem! Play nicely :)
Good point that will definitely be a bug! Nice catch!
while I like the content, Google has not been a good place for developers for a while and we should stop idolizing the "anything related to google must be good" label
Couldn't agree more. Idolizing any company is a recipe for disappointment. However, they are KNOWN for the caliber of interview questions they ask so its really nice to see some real questions they've asked in the past!
There's a couple of things that bother me about this solution, both of them in
emit
.First, there's no error handling, so a called function could break the forEach with a throw.
The second is a little more subtle, and I'm not sure if, or how it should be fixed. The object is mutable, and one of the callbacks could change it. Is it canonical to not worry about this? How would you go about fixing it? Deep copy? Freezing? Since it's a parameter, not sure the best way.
Making the eventData immutable is probably not the job of the
EventEmitter
The Node.js EventEmitter doesn't alter the data in its
emit
implementationThat's why I don't know the solution to this. In C++, passing along const references solves it from anything but something very intentional. If it's not immutable (or rather, some callback decides to mutate it) you're going to run into all sorts of potential bugs. Things can change based on the order of the callbacks and on the implementation of the emitter. That just really grates on me and feels wrong.
I wonder if that's part of the point of the question. When I did C++ interviews, one of my standard questions was "implement memcpy". I was more interested in how they handled edge cases than the actual copying.
I would say that the implementation is straight forward, but my brain focused on the events member and how it is being used. I have seen this pattern many times and I am surprised there are no common specialized classes for it.
So, first of all, without changing any types, I would optimize the on (and similarly emit) methods like this:
It's a minor improvement for small numbers, but it hurts me to see repeated key access to the same reference.
Second of all, why not write first a class that we could call Lookup to handle all those string indexed lists?
Now, if events is a Lookup class, the code is cleaner and safer:
In fact, most of the code in your EventEmitter class is actually Lookup code. One could add to the Lookup class a forEach method and you wouldn't even need an EventEmitter class:
Also, being all modern and not ending statements in semicolon feels obnoxious to me. It's probably me being me and old, but I had to say it.
Very good post. Thanks.
PubSub
The bases for p/s, yes
Sorry to poop the party, but I hope google asks better questions in interviews for their sake.
Anyone smell that..? ;) Sometimes you get lucky with an "easier" problem - not the worst thing in the world!
how about doing a filter for removing a listener i.e filtering in objects which need not be removed.
I see a lot of comments related to ts, but the article dowsnt seem to use ts, did it get edited?
nice
Hi, Elisabeth.
I want to share my solution to your series in the Korean community.
Are there any rules I must follow?
You could also have used a Set, assuming that functions are added only one.