Skip to content

Commit

Permalink
Added maxCount, onError, and clear mehotds
Browse files Browse the repository at this point in the history
  • Loading branch information
rgr-myrg committed Aug 8, 2014
1 parent fc720a2 commit 42bc663
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
15 changes: 10 additions & 5 deletions examples/queue.html
Expand Up @@ -5,29 +5,34 @@
<title>Queue Example</title>
<style>
</style>
<script type="text/javascript" src="../build/1.0.0/devshop.js"></script>
<script type="text/javascript" src="../build/1.0.0/activity.js"></script>
<script type="text/javascript">
var queue = new DevShop.Queue({
var queue = new Activity.Queue({
id: "queue",

timeToWait: 1000, //one second

maxCount: 10,

callback: function( msg ) {
console.log( msg );
document.getElementById( "log" ).innerHTML += msg;
},

onError: function( e ) {
console.log( e );
}
});

// Load up the queue for testing
for( var x = 0; x < 11; x++ ) {
for( var x = 0; x < 13; x++ ) {
queue.add( x + " second(s) just passed. <br/>");
}

queue.start();

queue.add( "I'm the last in line." );

// Add to the queue while it's running.
queue.add( "I'm the last in line." );

</script>
</head>
Expand Down
34 changes: 31 additions & 3 deletions src/Queue.js
Expand Up @@ -4,33 +4,51 @@
intervalId = null,
running = false,
callback = function(){},
onError = function(){},
timeToWait = 300,
maxCount = -1,
queue = [];

if ( typeof options !== "object" || !options.id ) {
throw( "Queue options Object is Null" );
throw( new Error( "Queue options Object is Null" ) );
}

objectId = options.id;

if ( !isNaN( options.timeToWait ) ) {
if ( !isNaN( options.timeToWait ) && options.timeToWait > 0 ) {
timeToWait = options.timeToWait;
}

if ( !isNaN( options.maxCount ) && options.maxCount > 0 ) {
maxCount = options.maxCount;
}

if ( typeof options.callback === "function" ) {
callback = options.callback;
}

if ( typeof options.onError === "function" ) {
onError = options.onError;
}

return {
add: function() {
if ( maxCount > 0 && queue.length >= maxCount ) {
onError( new Error( "Max count exceeded: " + queue.length ) );

return false;
}

queue.push( arguments );
return true;
},

start: function() {
if ( !running ) {
try {
intervalId = setInterval( objectId + ".run()", timeToWait );
} catch( e ) {
onError( e );
}
}
},
Expand All @@ -43,6 +61,7 @@
callback.apply( this, queue.shift() );
} catch( e ) {
this.stop();
onError( e );
}
} else {
this.stop();
Expand All @@ -56,7 +75,16 @@

isRunning : function() {
return running;
},

count: function() {
return queue.length;
},

clear: function() {
this.stop();
queue = [];
}
};
};
})( DevShop );
})( Activity );

0 comments on commit 42bc663

Please sign in to comment.