sindresorhus / p-queue

Promise queue with concurrency control
MIT License
3.45k stars 185 forks source link

queue resources #148

Closed ftreesmilo closed 3 years ago

ftreesmilo commented 3 years ago

This api is great... one thing I would like in the api is a notion of a resource for a queue. Bluebird has some great apis for using resources and destroying references to them when the work is done.

If you would be willing to take a patch, I'd like to add some options to p-queue to register a resource function so that when a queue runs, it gets a resource, and when it is done it releases it... running each runnable in the queue with a reference to the obtained resource.

of course this would mean that the ref must be usable, once obtained, by concurrency async operations.

It would be useful for me... but perhaps it's too narrow a use-case for the project? If not, I'll send a PR

sindresorhus commented 3 years ago

I have a feeling this is a bit niche, but maybe you could elaborate a little bit more about what would have to be added? p-queue is already more complex than I'd like, so I'm hesitant to add more options/logic to it.

ftreesmilo commented 3 years ago

That's totally fair. It very well may be. I was thinking a queue could have a resource allocator function that would be called when a queue get's busy and is doing work, and a destructor for when the queue goes idle. that way all the work fns that the queue runs can be passed the resource they know will be destroyed when it's no longer needed.

sindresorhus commented 3 years ago

It seems like you could build something like that on top of p-queue with the right hooks. We already expose a bunch of events.

ftreesmilo commented 3 years ago

ok, np