A simple aspect-oriented programming library in ECMAScript 7
npm install es7-aspectThis 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.__
In gru-aspect.js:
``javascript
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:`javascript
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:
`javascript
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.
`
.
* Otherwise, to stop execution, call reject([val]). [val]` is an optional argument that will be used as the return value of original function.