Skip to content

Instantly share code, notes, and snippets.

@SebastianHGonzalez
Last active January 2, 2019 18:37
Show Gist options
  • Save SebastianHGonzalez/26230a55c91f5c10c33e591ec27a8b6b to your computer and use it in GitHub Desktop.
Save SebastianHGonzalez/26230a55c91f5c10c33e591ec27a8b6b to your computer and use it in GitHub Desktop.
first approach to implemet point cuts
class PointCut {
constructor(definition) {
this.inject(definition);
}
inject(definition) {
this.injectAspectToObjectMethods(definition.class.prototype);
}
injectAspectToObjectMethods(prototype) {
prototype[name] = this.decorate(prototype[name]);
}
decorate(joinPoint) {
const pointCut = this;
return function (...args) {
const context = this;
pointCut.before(context, joinPoint.name, ...args);
const result = pointCut.around(context, joinPoint, ...args);
pointCut.after(context, joinPoint.name, result, ...args);
return result;
}
}
before(context, joinPointName, ...args) {
console.log("before");
}
after(context, joinPointName, result, ...args) {
console.log("after");
}
around(context, joinPoint, ...args) {
console.log("around: start");
const result = joinPoint.apply(context, args);
console.log("around: end");
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment