bottleneck icon indicating copy to clipboard operation
bottleneck copied to clipboard

Feature request: Emit "deleted" event when a limiter is deleted from a Group

Open tomaszlukasik opened this issue 5 years ago • 5 comments

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?

tomaszlukasik avatar Jan 29 '19 10:01 tomaszlukasik

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?

SGrondin avatar Jan 30 '19 00:01 SGrondin

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.

tomaszlukasik avatar Jan 30 '19 09:01 tomaszlukasik

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!

SGrondin avatar Jan 30 '19 13:01 SGrondin

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.

tomaszlukasik avatar Jan 31 '19 14:01 tomaszlukasik

No problem, it might still be useful to some people. I'll leave this issue open until the feature is completed.

SGrondin avatar Jan 31 '19 15:01 SGrondin