Simple AOP realization for async applications (NodeJS)
JavaScript
Latest commit 03ccb95 Nov 20, 2012 @baryshev Remove Travis CI
Permalink
Failed to load latest commit information.
examples Update example.js Feb 19, 2012
lib Fix params variable leak Feb 4, 2012
.npmignore First commit Sep 16, 2011
LICENSE
README.md Remove Travis CI Nov 21, 2012
index.js Convert double quotes to single quotes Oct 8, 2011
package.json Fix params variable leak Feb 4, 2012

README.md

About

Simple AOP realization for async applications.

Installation

npm install aop

Example

var aop = require("aop");

var cache = {};

var someAsyncFunction = function(num, callback) {
	console.log("calculating");
	var result = num * 2;
	callback(null, result);
	cache[num] = result;
};

var cacheAspect = function(method, params) {
	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) {
	params[0]++;
	method.apply(this, params);
};

var formatAspect = function(method, params) {
	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);
});