Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #589 from hochang/master
join test connector name change engine comments
- Loading branch information
Showing
4 changed files
with
145 additions
and
4 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
47 changes: 47 additions & 0 deletions
47
modules/engine/test/mock-routes/connectors/naive_connector.js
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,47 @@ | |||
/* | |||
example of using pipeline. | |||
On top of library when (https://github.com/cujojs/when.git) | |||
description of pipeline: https://github.com/cujojs/when/blob/master/docs/api.md | |||
Things must have: | |||
customConnector.connectorName. this field tells the connector loader | |||
.exec that verb.js would like to call when there is an execution for any connectors. | |||
*/ | |||
var pipeline = require('/Users/hochang/workspace/ql.io-mp-apis/node_modules/when/pipeline') | |||
var customConnector = module.exports = function(table, statement, type, bag, path){ | |||
var foo = function(n){ | |||
return n+1 | |||
} | |||
var bar = function(n){ | |||
return n+2 | |||
} | |||
var wae = function(n){ | |||
return n*3 | |||
} | |||
var beforePipeline = [foo] | |||
var afterPipeline = [foo, bar,wae] | |||
this.exec = function(args) { | |||
var mypromise = pipeline(beforePipeline, 1); | |||
mypromise.then(function(arg){ | |||
results = send(arg) | |||
var myresult = pipeline(afterPipeline, results) | |||
myresult.then(function(resultbody){ | |||
var result = { | |||
headers: { | |||
'content-type': 'application/json' | |||
}, | |||
body: resultbody | |||
}; | |||
return args.callback(null, result); | |||
}) | |||
}) | |||
|
|||
} | |||
|
|||
|
|||
function send(arg){ | |||
return arg+1 | |||
} | |||
|
|||
} | |||
customConnector.connectorName = 'aaa' |
80 changes: 80 additions & 0 deletions
80
modules/engine/test/mock-routes/connectors/pipeline_connector.js
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,80 @@ | |||
/* | |||
example of using pipeline to make async calls.. | |||
On top of library when (https://github.com/cujojs/when.git) | |||
description of pipeline: https://github.com/cujojs/when/blob/master/docs/api.md | |||
put all chained functions into arrays. In my example, beforePipeline and afterPipeline | |||
Calling pipeline to executed tasks in sequence, but without overlap. Result from prior task will be passed as argument to the latter tasks. | |||
The final result can be accessed in the function passed in promise.then. | |||
*/ | |||
var pipeline = require('/Users/hochang/workspace/ql.io-mp-apis/node_modules/when/pipeline'), | |||
when = require('/Users/hochang/workspace/ql.io-mp-apis/node_modules/when'), | |||
http = require('http'); | |||
var customConnector = module.exports = function(){ | |||
var foo = function(n){ | |||
foo1(function(err, results){ | |||
|
|||
}) | |||
} | |||
/* | |||
This is an aysnc call. In order to put it into pipeline, | |||
a defer() is required to keep the call blocking during I/O | |||
*/ | |||
var foo = function(){ | |||
var deferred = when.defer() | |||
var options = { | |||
host: 'localhost', | |||
port: 3000 | |||
} | |||
|
|||
callback = function(response) { | |||
var str = ''; | |||
|
|||
//another chunk of data has been recieved, so append it to `str` | |||
response.on('data', function (chunk) { | |||
str += chunk; | |||
}); | |||
|
|||
//the whole response has been recieved, so we just print it out here | |||
response.on('end', function () { | |||
deferred.resolve(parseInt(str)) | |||
}); | |||
} | |||
|
|||
http.request(options, callback).end(); | |||
return deferred.promise | |||
} | |||
var bar = function(n){ | |||
return n+2 | |||
} | |||
var wae = function(n){ | |||
return n*3 | |||
} | |||
|
|||
|
|||
this.exec = function(args) { | |||
var cb = function(n){ | |||
var asd=args | |||
var result = { | |||
headers: { | |||
'content-type': 'application/json' | |||
}, | |||
body: n | |||
}; | |||
return args.callback(null, result); | |||
} | |||
var afterPipeline = [foo, bar,wae,cb] | |||
pipeline(afterPipeline, 1); | |||
|
|||
|
|||
function send(arg){ | |||
return arg+1 | |||
} | |||
|
|||
|
|||
} | |||
} | |||
customConnector.connectorName = 'bbb' |