Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ccab191
Showing
6 changed files
with
130 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.md | ||
.git* | ||
examples/ |
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# About | ||
Simple AOP realization for async applications. | ||
|
||
# Installation | ||
|
||
npm install aop | ||
|
||
# Example | ||
|
||
```js | ||
var aop = require("aop"); | ||
|
||
var cache = {}; | ||
|
||
var someAsyncFunction = function fnName(num, callback) { | ||
console.log("calculating"); | ||
var result = num * 2; | ||
callback(null, result); | ||
cache[num] = result; | ||
}; | ||
|
||
var cacheAspect = function(method, params) { | ||
console.log(this.name); | ||
var num = params[0]; | ||
if (cache[num]) { | ||
var callback = params[params.length - 1]; | ||
console.log("using cached value"); | ||
callback(null, cache[num]); | ||
} else { | ||
method.apply(this, params); | ||
} | ||
}; | ||
|
||
var changeAspect = function(method, params) { | ||
console.log(this.name); | ||
params[0]++; | ||
method.apply(this, params); | ||
}; | ||
|
||
var formatAspect = function(method, params) { | ||
console.log(this.name); | ||
var callback = params[params.length - 1]; | ||
var newCallback = function(error, result) { | ||
callback(error, result + "s"); | ||
}; | ||
params[params.length - 1] = newCallback; | ||
method.apply(this, params); | ||
}; | ||
|
||
someAsyncFunction = aop.wrap(someAsyncFunction, someAsyncFunction, cacheAspect); | ||
someAsyncFunction = aop.wrap(someAsyncFunction, someAsyncFunction, formatAspect); | ||
someAsyncFunction = aop.wrap(someAsyncFunction, someAsyncFunction, changeAspect); | ||
|
||
someAsyncFunction(2, function(error, result) { | ||
console.log(result); | ||
}); | ||
|
||
someAsyncFunction(2, function(error, result) { | ||
console.log(result); | ||
}); | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
var aop = require("./../lib/aop"); | ||
|
||
var cache = {}; | ||
|
||
var someAsyncFunction = function fnName(num, callback) { | ||
console.log("calculating"); | ||
var result = num * 2; | ||
callback(null, result); | ||
cache[num] = result; | ||
}; | ||
|
||
var cacheAspect = function(method, params) { | ||
console.log(method.name); | ||
var num = params[0]; | ||
if (cache[num]) { | ||
var callback = params[params.length - 1]; | ||
console.log("using cached value"); | ||
callback(null, cache[num]); | ||
} else { | ||
method.apply(this, params); | ||
} | ||
}; | ||
|
||
var changeAspect = function(method, params) { | ||
console.log(this.name); | ||
params[0]++; | ||
method.apply(this, params); | ||
}; | ||
|
||
var formatAspect = function(method, params) { | ||
console.log(method.name); | ||
var callback = params[params.length - 1]; | ||
var newCallback = function(error, result) { | ||
callback(error, result + "s"); | ||
}; | ||
params[params.length - 1] = newCallback; | ||
method.apply(this, params); | ||
}; | ||
|
||
someAsyncFunction = aop.wrap(someAsyncFunction, someAsyncFunction, cacheAspect); | ||
someAsyncFunction = aop.wrap(someAsyncFunction, someAsyncFunction, formatAspect); | ||
someAsyncFunction = aop.wrap(someAsyncFunction, someAsyncFunction, changeAspect); | ||
|
||
someAsyncFunction(2, function(error, result) { | ||
console.log(result); | ||
}); | ||
|
||
someAsyncFunction(2, function(error, result) { | ||
console.log(result); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require("./lib/pool"); |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports.wrap = function(that, target, aspect) { | ||
var params = Array.prototype.slice.call(arguments, 3); | ||
var wrapper = function() { params.unshift(target, arguments); aspect.apply(that, params); } | ||
return wrapper; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name" : "aop", | ||
"version" : "0.1", | ||
"description" : "Simple AOP realization for async applications", | ||
"keywords": ["aop"], | ||
"author" : "Vadim M. Baryshev <vadimbaryshev@gmail.com>", | ||
"repository" : { "type" : "git", "url" : "https://github.com/baryshev/aop.git" } | ||
"main" : "index", | ||
"engines" : { "node": ">= 0.2.0" } | ||
} |