Permalink
Browse files

Got the Basic Version up will not start adding tests, features and do…

…cumentation.
  • Loading branch information...
1 parent cdd206f commit 0d8e4ee7b3b3c4726b6e46e69018fa319a6fa527 @Johanndutoit committed Jun 27, 2012
Showing with 102 additions and 0 deletions.
  1. +75 −0 lib/main.coffee
  2. +27 −0 sample/common.coffee
View
@@ -0,0 +1,75 @@
+
+Narrow = require 'narrow'
+
+###
+
+###
+module.exports = exports = (funcs, done_callback) ->
+ ###
+ Called by Narrow on each Request Info Object.
+
+ From here we the request and call Narrows' callback (fn)
+ ###
+ narrow_request_item_callback = (task_info, callback) ->
+
+ if !task_info.params
+ task_info.params = {}
+
+ ###
+ On Load
+ ###
+ if task_info.load
+ task_info.load(task_info.params)
+
+ ###
+ Perfom the task
+ ###
+ task_info.task task_info.params, (err, result) ->
+ if !err
+ callback null, {
+ result: true,
+ value: result,
+ name: task_info.name
+ }
+ else
+ callback null, {
+ result: false,
+ error: err,
+ name: task_info.name
+ }
+
+ ###
+ On Complete
+ ###
+ if task_info.complete
+ task_info.load(task_info.params)
+
+ ###
+ Create the Narrow Instance to handle the Async Operation.
+ ###
+ narrow = new Narrow 5, narrow_request_item_callback
+
+ ###
+ This is where we will store our results.
+ ###
+ results = Object()
+
+ ###
+ Receives the Results and Creates a map for the cient
+ ###
+ narrow.on('complete', (result) ->
+ Object.defineProperty results, result.name, {
+ value: result,
+ writable: false,
+ configurable: false
+ }
+ )
+
+ ###
+ Pushes all the Requests to the Pipeline and waits for the results.
+
+ When the results are received we return it to the callback.
+ ###
+ narrow.pushAll(funcs, ->
+ done_callback results
+ )
View
@@ -0,0 +1,27 @@
+async_block = require '../'
+
+async_block([
+ {
+ name: 'd1',
+ task: (params, callback) ->
+ callback null, 'testing 2'
+ },
+ {
+ name: 'd2',
+ task: (params, callback) ->
+ callback 'Something went wrong!'
+ }
+ ], (results) ->
+
+ if results.d1.result is true
+ console.log "D1 Success: " + results.d1.value
+ else
+ console.log "D1 Fail: " + results.d1.value
+
+
+
+ if results.d2.result is true
+ console.log "D1 Success: " + results.d2.value
+ else
+ console.log "D1 Fail: " + results.d2.value
+ )

0 comments on commit 0d8e4ee

Please sign in to comment.