-
-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature unnamed hook used as a reference #52
Conversation
Wow that’s quite a big pull request. I think it would be better to start out with an issue or maybe a pull request to the README to discuss what you want to do before putting in so much work :) I couldn’t yet wrap my head around it. You need the I feel like your change is so big, you could make your own library which drops the named hooks altogether? |
Never :) It depends on what you optimize for. I use this in Octokit, the JavaScript tooling for GitHub’s APIs. The library is used in both browsers and servers. Particularly for browsers, the bundle size is relevant, so I’d always prefer smaller speciallized libraries over libraries that have a lot of features I don’t use
It’s fantastic, thank you so much! I kinda wish we came up with this in the first place, I feel everything I need to do with the current API I could do with E.g. current in Octokit, I have this octokit.before('request', beforeRequest) I could as well do this in future octokit.request.before(beforeRequest) So okay, let’s do it, you convinced me :) I wonder if instead we could do something like this import Hook from 'before-after-hook/unnamed'
const saveHook = new Hook<user>() I think that way the current bundle would remain the same because it wouldn’t bundle in the new unnamed functionality. Now, we need to document the new API. And I’m not sure if |
Valid point regarding the bundle size, but I guess it will always remain a double-edged sword :) I'm glad I convinced you, whoe! Now all we got to do is come up with the best implementation and semantics 👍 Suggestion one Suggestion two
Suggestion three I lean towards suggestion 1 for simplicity and no breaking changes, option 2 as mixture of ease and completeness API wise, but definitely to three for perfection 😄 |
The build is failing✨ Good work on this PR so far! ✨ Unfortunately, the Travis CI build is failing as of b544c5f. Here's the output:
|
You are totally right! I think we can use that for naming as well, the current API is a collection, the new API is singular. I think eventually this API would be nice, but require a breaking change // singular
const userHook = new Hook<User>()
userHook.before(validate)
userHook(options, save)
// collection
const hooks = new Hook.Collection()
hooks.before('save', validate)
hooks('save', options, save) Would that make sense? 🤔 In order to get there, we could introduce a temporary API |
I like it! I think that fixes most of the inconsistencies we've discussed so I agree with all of your suggestions. |
All right, I've made the changes. It might look a little messy, but without the code for the deprecation warnings, it should look nice. |
All right, I think I covered every angle 🎉 I've thought about copying the collection API in the readme and remove all the As for the unit tests. I think the unit tests are covering it very minimally, but most of the code is covered because of the collection tests so I don't think it a huge problem. I think in your next major release you can rewrite the unit tests to better fit what it should test. Also, I'm not very familiar with this particular unit tests syntax, so, therefore, I'm a bit reluctant to go crazy there 😉 Hope you like what you read! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few minor comments.
The workaround for the memory leak is so straight forward, great job 👍
Co-Authored-By: harm-less <harmvdwerf@gmail.com>
Is there anything left to discuss or improve? |
Nope. I’ll test it with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 This PR is included in version 1.4.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
That was a quick test, haha. But thanks for merging, it was a fun process we went through ;) |
Hey,
Thanks for the plugin, I've added an additional feature I hope you like.
The reason I made this PR is because I wanted to be able to create a hook reference that wasn't based on a name. This allowed me to use typed hooks inTypeScript!
An example:

Console output:

As you can see, the
User
interface is being passed along to thebefore
and the actual hook method. So exporting thesaveHook
reference allowed me to use the hook in the different file but with strong typing.I also improved the typings file by adding Promises where necessary, otherwise the
before
hook in my example started to complain about theasync
keyword.I'm not super happy about having to make a seperate
unnamed
method for this, but I was forced to because thename
properly can be a string or a string array. And there was no way to alter theregister
method in such a way that its parameters could vary between 2 and 4 parameters and still function consistently in all scenarios as the options. And then again, thebefore
,after
,error
andwrap
methods still needed to know what hook reference to talk to without providing aname
, so I'm not sure there even is a way.Maybe you have some improvements you can think off, but I already spend too much time trying to figure out a Typescript related bug. Turned out I was using a using an old version of Typescript that caused it... So sorry for the additional "debug" commits 😅