pc035860 / ngQueue

ngQueue is an AngularJS module that helps you to handle routine sync/async queue task in AngularJS with ease.
MIT License
55 stars 13 forks source link

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

Open jrdn91 opened 7 years ago

jrdn91 commented 7 years ago

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

pc035860 commented 7 years ago

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
});