Skip to content

Commit

Permalink
Got the Basic Version up will not start adding tests, features and do…
Browse files Browse the repository at this point in the history
…cumentation.
  • Loading branch information
Johanndutoit committed Jun 27, 2012
1 parent cdd206f commit 0d8e4ee
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
75 changes: 75 additions & 0 deletions lib/main.coffee
@@ -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
)
27 changes: 27 additions & 0 deletions sample/common.coffee
@@ -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.