Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring and adding success callback to the sync process
Using a Queue to allow for linear processing of user data that can be chained together. Now that there is a known completion point a callback can be given to execute when the process is complete. The dependency on `http` has been removed as well and instead the `UserSync` module now operates on a stream. This allows the JSON data to come from a number of sources including stdin.
- Loading branch information
1 parent
2b37e18
commit bbfcbef
Showing
4 changed files
with
193 additions
and
114 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,56 @@ | ||
var Queue = function( items ){ | ||
var Queue = function( items, operator ){ | ||
var queue = items.slice(); | ||
this.operator = operator; | ||
|
||
this.run = function( callback ){ | ||
this.run = function( operator, callback ){ | ||
|
||
if ( !callback ) { | ||
callback = operator; | ||
operator = this.operator; | ||
} | ||
|
||
if (typeof(operator) != 'function') { | ||
throw( "operator must be a function" ); | ||
} | ||
|
||
if (typeof(callback) != 'function') { | ||
throw( "callback must be a function" ); | ||
} | ||
|
||
if ( queue.length == 0 ) { | ||
callback( null, items ); | ||
return; | ||
}; | ||
|
||
// shift an item off | ||
var item = queue.shift(); | ||
var item = queue.shift(), next, self = this; | ||
|
||
try { | ||
this.operate( item ) | ||
} catch(e) { | ||
// fire the callback with the error and stop operating | ||
if ( queue.length == 0 ) { | ||
// queue is done, so we're going | ||
next = function( e ){ | ||
callback( e, items ); | ||
}; | ||
} else { | ||
next = function(){ | ||
self.run( operator, callback ); | ||
} | ||
} | ||
// operate on the item | ||
// do it again | ||
|
||
try{ | ||
operator( item, next ); | ||
} catch( e ){ | ||
callback( e ); | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
module.exports = { | ||
createQueue: function( items, operation, callback ){ | ||
var queue = new Queue( items ); | ||
queue.operation = operation; | ||
queue.run(operation, callback) | ||
}, | ||
Queue: Queue | ||
} |
Oops, something went wrong.