rknell/dart_queue

identifier to each queue so that the same algorithm is rerun while in process

kevin4dhd opened this issue · 2 comments

Hello, I am implementing this library with the video thumbnail, but I have a problem, which is that sometimes the same element is called 2 times, causing an unnecessary queue to be generated, so I would like for example:

add the first queue
final resultOne = await queue.add(code: "001",() async {
await Future.delayed(Duration(seconds: 10));
return "Finish one";
});

Then comes the second queue
final resultTwo = await queue.add(code: "002",() async {
await Future.delayed(Duration(seconds: 10));
return "Finish Two";
});

Then comes a third queue with the same code as the first, so I would like that when the first one ends, the third queue is also executed.
final resultThree= await queue.add(code: "001",() async {
//returns the same result because 001 is already running
await Future.delayed(Duration(seconds: 10)); //this is no longer running
return "Finish one";
});

Hey @kevin4dhd

While a native solution might be added at some point, I would just extend the queue class with something like this:

class UniqueQueue<T> extends Queue {
  var _uniqueTracker = <dynamic>{};

  addUnique(dynamic identifier, Future<T> Function() closure) {
    if (_uniqueTracker.contains(identifier) == false) {
      add(closure);
      _uniqueTracker.add(identifier);
    }
  }
}

then replace your queue object with UniqueQueue and call addUnique.

Going to close with this for now, but if there is something else feel free to keep responding.