Conversation
rekmarks
left a comment
There was a problem hiding this comment.
Just a couple of nits, otherwise LGTM.
|
I've included #2 here as well, why not |
Co-Authored-By: Erik Marks <25517051+rekmarks@users.noreply.github.com>
Co-Authored-By: Mark Stacey <markjstacey@gmail.com>
Co-Authored-By: Mark Stacey <markjstacey@gmail.com>
|
Alrighty, I think this is good now. 100% test coverage, I've double-checked the package contents, and the CI is running. |
$ tar tvf metamask-safe-event-emitter-v1.0.1.tgz drwxr-xr-x 0 0 0 0 12 Feb 17:21 package -rw-r--r-- 0 0 0 481 21 Jan 18:04 package/README.md -rw-r--r-- 0 0 0 182 12 Feb 16:54 package/index.d.ts -rw-r--r-- 0 0 0 2115 12 Feb 16:54 package/index.js -rw-r--r-- 0 0 0 1867 12 Feb 16:54 package/index.js.map -rw-r--r-- 0 0 0 885 12 Feb 17:22 package/package.json
Co-Authored-By: Mark Stacey <markjstacey@gmail.com>
| throw er; // Unhandled 'error' event | ||
| } | ||
| // At least give some kind of context to the user | ||
| const err = new Error('Unhandled error.' + (er ? ' (' + er.message + ')' : '')); |
There was a problem hiding this comment.
Nit: This might be more readable using a string template?
| safeApply(handler, this, args); | ||
| } else { | ||
| const len = handler.length; | ||
| const listeners = arrayClone(handler); |
There was a problem hiding this comment.
Actually.... this is the only place where arrayClone is used, and it's entirely pointless? It makes a shallow clone, then discards it. The listeners array is never touched after this point - just listeners[i], which is the same as handler[i] because shallow. So arrayClone can be removed altogether.
There was a problem hiding this comment.
Huh - apparently this is in the Gozala/events module as well.
There was a problem hiding this comment.
It's also in node core. It was added here: nodejs/node#601
I must be missing something 🤔 I don't understand why that clone is happening at all, and from skimming through the discussion they don't mention it at all. Maybe some kinda obscure micro-optimization 🤷♂ .
There was a problem hiding this comment.
Yeah even @rekmarks' suggestions above, I assume they were all micro-optimizations in (what could be) hot code paths. I left in the len variable here for that reason. A part of me wants to clean everything up and just say "let the runtime deal with it," but... yeah. 🤷♂
That was 5 years ago, and runtimes have come a long way since. 🤔
There was a problem hiding this comment.
I was poking around on jsperf for that but couldn't find anything conclusive. The good news is, it's definitely fine, and there's no reason to block on it. I don't see any reason not to do [ ...handler ], but 🤷♂
There was a problem hiding this comment.
It turns out the clone is necessary 🤦♂ . Kinda obvious in retrospect - the clone happens here because the event handlers might subscribe new handlers, or unsubscribe them, thus mutating the handlers array.
| safeApply(handler, this, args); | ||
| } else { | ||
| const len = handler.length; | ||
| const listeners = arrayClone(handler); |
There was a problem hiding this comment.
I was poking around on jsperf for that but couldn't find anything conclusive. The good news is, it's definitely fine, and there's no reason to block on it. I don't see any reason not to do [ ...handler ], but 🤷♂
| throw er; // Unhandled 'error' event | ||
| } | ||
| // At least give some kind of context to the user | ||
| const err = new Error('Unhandled error.' + (er ? ' (' + er.message + ')' : '')); |
This PR, basically, migrates the project to use TypeScript. I've mixed in a few extra (read: too many) changes here because I'm lazy. 🤦♂
We've got:
npmtoyarn.nvmrcfile@metamasknpm scopeeventsdependency (is this needed?)I'm hoping this can serve as a reference for future migrations and that these configurations (i.e. ESLint & tsconfig) can be factored out as appropriate.