Created
July 3, 2013 01:07
-
-
Save bancek/5914672 to your computer and use it in GitHub Desktop.
AngularJS job queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('Queue') | |
.factory('Queue', ($q) -> | |
class Queue | |
constructor: -> | |
@queue = [] | |
@processing = no | |
enqueue: (func) => | |
defer = $q.defer() | |
@queue.push | |
func: func | |
defer: defer | |
@processQueue() | |
defer.promise | |
processQueue: -> | |
return if @processing | |
next = @queue.shift() | |
if next? | |
@processing = yes | |
next.func().then((res) => | |
@processing = no | |
next.defer.resolve(res) | |
@processQueue() | |
, (err) => | |
next.defer.reject(err) | |
@processQueue() | |
) | |
) | |
# Usage: | |
# queue = new Queue() | |
# | |
# doSomethingSerial: -> | |
# queue.enqueue -> | |
# defer = $q.defer() | |
# | |
# defer.resolve() | |
# | |
# defer.promise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment