bottleneck
bottleneck copied to clipboard
Feature request: Emit "deleted" event when a limiter is deleted from a Group
Hi!
Would it be possible for you to add emitting deleted
event on a Group when a limiter is deleted after being idle?
There's already on("created")
(https://github.com/SGrondin/bottleneck#oncreated) so would be nice to be able to listen for the end of limiter's life and possibly do some cleanup.
Or maybe there's another way to know when a limiter is removed?
I like the idea!
This is another feature that would be very easy to implement if I didn't have to make it work with Clustering 😄
I'm about to improve how Groups work when Clustering is enabled, so I'll take the time to add this feature while I'm touching the code.
In the mean time I might be able to offer you a workaround. Are you using Clustering?
Cool!
In the mean time I might be able to offer you a workaround. Are you using Clustering?
No, I'm not. A workaround would be awesome.
You'll need to be careful and remove the workaround once you've upgraded to a version that supports this feature.
// Run the following block of code at the start of your app,
// make sure it's only executed one time and it's before any code uses Bottleneck!
const Bottleneck = require('bottleneck')
const impl = Bottleneck.Group.prototype.deleteKey
Bottleneck.Group.prototype.deleteKey = function (...args) {
this.Events.trigger('deleted', args[0])
return impl.apply(this, args)
}
Then you can use it in your normal app code like this:
const Bottleneck = require('bottleneck')
const group = new Bottleneck.Group({ minTime: 1000 })
group.on('created', (limiter, key) => console.log('created', key))
group.on('deleted', (key) => console.log('deleted', key))
Please let me know if it worked!
Hi, thanks for the workaround. It turned out that after some changes in the flow I don't need that currently. Sorry 😞
But I'll try it anyway soon. And I still think that deleted
event can be useful for other cases.
No problem, it might still be useful to some people. I'll leave this issue open until the feature is completed.