Skip to content

Instantly share code, notes, and snippets.

View SebastianHGonzalez's full-sized avatar

Sebastian Gonzalez SebastianHGonzalez

View GitHub Profile
@SebastianHGonzalez
SebastianHGonzalez / injectPointCut.js
Created January 21, 2019 13:14
pointCut's inject correction
inject(definition) {
definition.decoreateTargets(joinPoint => this.decorate(joinPoint));
}
@SebastianHGonzalez
SebastianHGonzalez / aroundPointCut.js
Created January 21, 2019 12:39
point cut's around method
around(context, joinPoint, ...args) {
return this.aspects.reduce(
function aroundReducer(next, aspect) {
return function joinPointify(...newArgs) {
const newContext = this;
aspect.around(newContext, next, ...newArgs)
}
},
joinPoint
).apply(context, args);
@SebastianHGonzalez
SebastianHGonzalez / beforeAndAfterPointCut.js
Last active January 21, 2019 12:35
point cut's before and after methods
before(context, joinPointName, ...args) {
this.aspects.reduce(
(_, aspect) => aspect.before(context, joinPointName, ...args)
);
}
after(context, joinPointName, result, ...args) {
return this.aspects.reduce(
(prevResult, aspect) => aspect.after(context, joinPointName, prevResult, ...args),
result
@SebastianHGonzalez
SebastianHGonzalez / addAspect.js
Created January 21, 2019 12:30
addAspect example
addAspect(aspect) {
this.aspects = [...this.aspects, aspect];
}
class LoggerAspect {
constructor(logger) {
this.logger = logger;
}
before(context, joinPointName, ...args) {
this.logger.log("logging");
}
}
function timeAspect(joinPoint, logger) {
const aspect = {
logger,
timer: {
start: function () { this.startTime = Date.now() },
stop: function () { this.stopTime = Date.now() },
time: function () { return this.stopTime - this.startTime }
},
before: function () { this.timer.start() },
@SebastianHGonzalez
SebastianHGonzalez / firstApproachPointCut.js
Last active January 2, 2019 18:37
first approach to implemet point cuts
class PointCut {
constructor(definition) {
this.inject(definition);
}
inject(definition) {
this.injectAspectToObjectMethods(definition.class.prototype);
}
injectAspectToObjectMethods(prototype) {
@SebastianHGonzalez
SebastianHGonzalez / desiredJoinPointInterface.js
Last active January 2, 2019 17:53
desired joinPoint interface
const personGreetingPC = new PointCut({class: Person, methodName: "greet"});
jhon.greet();
// <- "Hello jhon"
personGreetingPC.addAspect(loggerAspect);
jhon.greet();
// logging
// <- "Hello jhon"
@SebastianHGonzalez
SebastianHGonzalez / thisManipulation.js
Last active January 1, 2019 17:40
javascript this manipulation example
function greet() {
return "Hello " + this.name ;
}
const john = {
name: "John"
}
const jane = {
name: "Jane"
@SebastianHGonzalez
SebastianHGonzalez / GenericTimeAspect.js
Last active January 1, 2019 16:45
time aspect using generic aspect implementation
function timeAspect(joinPoint, logger) {
const timer = {
start: function () { this.startTime = Date.now() },
stop: function () { this.stopTime = Date.now() },
time: function () { return this.stopTime - this.startTime }
};
function before() { timer.start() }
function after() { timer.stop(), logger.log(timer.time) }
return aspect(joinPoint, { before, after })