es7-aspect
v1.2.0
Published
A simple aspect-oriented programming library in ECMAScript 7
Downloads
14
Readme
ES7-Aspect
This library provides simple aspect oriented programming features in javascript using ES7 decorators.
It is a derivative of Board project.
Please note that this library is intended to do simple runtime checks and should be used as the last resort. It is not async-compatible, i.e. the activities in an Aspect class cannot contain any callbacks or promises, whereas the original function can be async.
Sample Usage
In gru-aspect.js
:
import Aspect from 'es7-aspect';
export default class GruAspect extends Aspect {
//Once again, no async calls in any of these functions
@Aspect.before('stealsTheMoon')
eatsABanana(bananas) {
if (bananas) {
console.log(`There're ${bananas.length} banana(s). Eat one.`);
bananas.pop();
}
}
@Aspect.intercept('stealsTheMoon')
checkBananas(resolve, reject, bananas) {
if (!bananas || bananas.length === 0) {
console.log('No banana. Nah.');
reject();
}
else {
console.log(`${bananas.length} banana(s)!`);
resolve();
}
}
@Aspect.after('stealsTheMoon')
givesBananas(bananas) {
console.log(`${bananas.length} banana(s) left.`);
}
}
In gru.js
:
import Aspect from 'es7-aspect';
import GruAspect from './gru-aspect';
@Aspect.use(GruAspect)
export default class Gru {
stealsTheMoon(bananas) {
console.log('Just stole the moon!');
}
}
In a console:
new Gru().stealsTheMoon();
// Prints 'No banana. Nah.'
new Gru().stealsTheMoon(['banana', 'banana']);
// Prints
// There're 2 banana(s). Eat one.
// Just stole the moon!
// 1 banana(s) left.
Advices
There are three advices: Before, After and Intercept.
Before
Perform certain activities before the original function executes. After all defined activities finishes, the original will run. The pre-activities have access to the actual arguments that will be eventually applied.
Intercept
The intercept advice runs before the original function, similar to the before advice, except that there can be one and only one intercept advice defined for a method, and it has the ability to prevent the original function from execution.
- To continue running after completing the interception, call
resolve()
. - Otherwise, to stop execution, call
reject([val])
.[val]
is an optional argument that will be used as the return value of original function.
After
Perform certain activities after the original function executes. All activities run after the original function completes execution. The post-activities have access to the actual arguments that will be eventually applied.