ngQueue icon indicating copy to clipboard operation
ngQueue copied to clipboard

Easy way to know when a given queue is started or finished

Open jrdn91 opened this issue 8 years ago • 1 comments

I have a loop that pushes $http requests into a queue and the queue then runs waiting for each to finish before moving to the next which is fantastic. Upon starting this queue I open a toastr on the page so the user knows something is happening in the background and then have some code in the success of each $http call that looks like this

if(i+1 === importJSON.length){
    toastr.clear($scope.importToast);
}

This just checks to see if it's on the last ajax call for this given array being looped over and if it is, on success will clear the toast. It works but this feels clunky to me and I'm curious if there is a way not documented to listen to when a given queue has completed.

I tried watching the queue API instance but it didn't log anything at all

jrdn91 avatar May 09 '17 14:05 jrdn91

Hi @Jordan4jc , there is a feature of getting queue statistics which is undocumented in PR #2. I just released it as v0.2.0.

With the { statistics: true } option, you can access queueTotal, queuePending and queueIdle from the queue instance.

var queue = $queueFactory(1, { statistics: true });

// enqueue just one task
queue.enqueue(function () {
    console.log('finished the task');

    $timeout(function () {
        // after a while, task finished
        console.log(queue.queuePending, queue.queueIdle);  // 0, true
    });

    // task hasn't finished here
    console.log(queue.queuePending, queue.queueIdle);  // 1, false
});

pc035860 avatar May 15 '17 17:05 pc035860