The to-aop module help you with applying Aspect Oriented Programming to JavaScript. It use under the hood ES Proxy for object same as other similar modules. It allow you hook class without creating new instance as well. It use javascript prototype for that.
For example I am using it for adding hooks to production code where all debug code is missing. I don't have got access to instance but I have only class constructor. But usage is unlimited.
More articles about AOP:
- https://github.com/mjancarik/shallow-with-context/blob/master/src/shallowWithContext.js
- https://hackernoon.com/aspect-oriented-programming-in-javascript-es5-typescript-d751dda576d0
- https://kyu.io/sneak-peek-to-javascript-aop/
Examples:
- https://github.com/mjancarik/shallow-with-context/blob/master/src/shallowWithContext.js
- https://github.com/seznam/ima/blob/master/packages/devtools/src/services/defaultSettings.json
npm i to-aop --save
You have some common class. For example:
// a.js
export default class A {
constructor(variable) {
this.variable = variable;
}
method() {
return this.variable;
}
notHookedMethod() {
return 'not hook';
}
}
You can use it for creating hook to your favorite framework or your own application without modification source code.
import { aop, hookName, createHook } from 'to-aop';
import A from './a';
const classHookBefore = createHook(
hookName.beforeMethod,
/^(method)$/,
({ target, object, property, context, args }) => {
//call your own hook
console.log(
`Instance of ${target.name} call "${property}"
with arguments ${args && args.length ? args : '[]'}.`
);
}
);
const classHookAfter = createHook(
hookName.afterMethod,
/^(method)$/,
({ target, object, property, context, args, payload }) => {
//call your own hook
console.log(
`Instance of ${target.name} call "${property}"
with arguments ${args && args.length ? args : '[]'}
and return value is "${payload}".`
);
}
);
const hooks = Object.assign({}, classHookBefore, classHookAfter);
aop(
A,
hooks
); // bind hook to class
const a = new A('my hook');
a.method(); // Instance of A call "method" with arguments [] and return value is "my hook".
a.notHookedClassMethod(); // not hook
import { aop, hookName, createHook } from 'to-aop';
import A from './a';
const instanceHook = createHook(
hookName.afterMethod,
/^(method)$/,
({ target, object, property, context, args, payload }) => {
console.log(
`Instance of ${object.constructor.name} call "${property}"
with arguments ${args && args.length ? args : '[]'}
and return value is "${payload}".`
);
}
);
const a = new A('my hook');
const hookedInstance = aop(a, instanceHook); // bind hook to instance
hookedInstance.method(); // "Instance of A call "method" with arguments [] and return value is "my hook".
hookedInstance.notHookedClassMethod(); // not hook
- (string) hookName - set of hooks
- (string, regexp, Array, function) regular - condition for filtering
- (function) callbackHook - your defined action
- (class, object) - target for hooks
- (Object<string, Array>) - pattern of hooks
We implemented base set of hooks which you can use for AOP.
- beforeMethod
- afterMethod
- aroundMethod
- beforeGetter
- afterGetter
- aroundGetter
- beforeSetter
- afterSetter
- aroundSetter
import { hookName } from 'to-aop';
// hookName.(beforeMethod|afterMethod|...)