Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First commit

  • Loading branch information...
commit ccab1910778b83d9763509575a02afea3274000f 0 parents
@baryshev authored
3  .npmignore
@@ -0,0 +1,3 @@
+*.md
+.git*
+examples/
61 README.md
@@ -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);
+});
+```
50 examples/example.js
@@ -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);
+});
1  index.js
@@ -0,0 +1 @@
+module.exports = require("./lib/pool");
5 lib/aop.js
@@ -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;
+};
10 package.json
@@ -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" }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.