Skip to content

Commit

Permalink
added eachAsync. Updated PubSub to use eachAsync.
Browse files Browse the repository at this point in the history
  • Loading branch information
Web Advanced committed Jul 25, 2013
1 parent 0f9d286 commit db12a6c
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/takeCommand.command.js
Expand Up @@ -3,8 +3,9 @@ window.takeCommand.Command = ( function( takeCommand, $, window ) {

var Command = takeCommand.Module.base( null, takeCommand.Events ),
_defaultOptions = {
type: 'GET',
dataType: 'JSON',
type: 'GET',
dataType: 'JSON',
contentType: 'application/json; charset=utf-8',
mock: { wasSuccess: true }
},
_utils = takeCommand.utils,
Expand Down
4 changes: 2 additions & 2 deletions src/takeCommand.events.js
Expand Up @@ -32,8 +32,8 @@ window.takeCommand.Events = ( function( utils ) {

ctx = ctx || this;

utils.each( list, function( func, i ) {
utils.pushToQueue( ctx, func, args );
utils.eachAsync( list, function( func, i ) {
func.apply( ctx, args );
});

return true;
Expand Down
18 changes: 18 additions & 0 deletions src/takeCommand.utils.js
Expand Up @@ -52,6 +52,24 @@ window.takeCommand.utils = ( function( Array, Object, window ) {
}, delay );
};

utils.eachAsync = function( collection, func, i ) {
this.chkArg.isNotUndefined( collection );
this.chkArg.isNotUndefined( func );
this.chkArg.isFunction( func );
i = i || 0;
//do work
var result = func( collection[ i ], i );
if( result === false ) return;
//up by one
i++
//again
if( i < collection.length ) {
window.setTimeout(function() {
utils.eachAsync( collection, func, i );
}, 10);
}
};

utils.each = function( collection, func ) {
this.chkArg.isNotUndefined( collection );
this.chkArg.isNotUndefined( func );
Expand Down
2 changes: 1 addition & 1 deletion take-command.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 57 additions & 6 deletions tests/sample.html
Expand Up @@ -20,22 +20,72 @@
var count = 0;

$(function() {
$count = $('#count');
var taskCommands = takeCommand.CommandGroup.init('taskCommands');
var userCommands = takeCommand.CommandGroup.init('userCommands');

userCommands.register( 'sendForm', '/test.php', 'POST' ).error( function() {
userCommands.register( 'sendForm', 'runner.html' ).error( function() {
// for(var i = 0; i < 50000000; i++) {
// i.toString();
// }
$('a').text('error!');

});

userCommands.sendForm.always(function() {
console.log(count);
if( count >= 3 ) userCommands.sendForm.off( 'submit', 'form' );
count++;
});
var addCount = function( i ) {
$('<li />').text( i ).appendTo($count);
};

userCommands.sendForm
.always(function() {
addCount(1);
}).always(function() {
addCount(2);
}).always(function() {
addCount(3);
}).always(function() {
addCount(4);
}).always(function() {
addCount(5);
}).always(function() {
addCount(6);
}).always(function() {
addCount(7);
}).always(function() {
addCount(8);
}).always(function() {
addCount(9);
}).always(function() {
addCount(10);
}).always(function() {
addCount(11);
}).always(function() {
addCount(12);
}).always(function() {
addCount(13);
}).always(function() {
addCount(14);
}).always(function() {
addCount(15);
}).always(function() {
addCount(16);
}).always(function() {
addCount(17);
}).always(function() {
addCount(18);
}).always(function() {
addCount(19);
}).always(function() {
addCount(20);
}).always(function() {
addCount(21);
}).always(function() {
addCount(22);
}).always(function() {
addCount(23);
}).always(function() {
addCount(24);
});


userCommands.sendForm.on( 'submit', 'form' );
Expand Down Expand Up @@ -84,5 +134,6 @@
<input type="text" name="name" />
<input type="submit" value ="submit" />
</form>
<ul id="count"></ul>
</body>
</html>

0 comments on commit db12a6c

Please sign in to comment.