Skip to content

Instantly share code, notes, and snippets.

View SebastianHGonzalez's full-sized avatar

Sebastian Gonzalez SebastianHGonzalez

View GitHub Profile
@SebastianHGonzalez
SebastianHGonzalez / InstanceMethodsAOP.js
Created January 1, 2019 16:13
Aspects applied to instance methods
function loggerAspect(joinPoint, logger) {
return function (...args) {
logger.log("logging");
return joinPoint.apply(this, args)
}
}
class Person {
constructor(name) {
this.name = name
@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 })
@SebastianHGonzalez
SebastianHGonzalez / FirstAspectGeneralizationAOP.js
Last active January 1, 2019 16:50
first attempt to generalize aspect implementation
function aspect(joinPoint, { before, around, after }) {
return function (...args) {
before && before(this, joinPoint.name, ...args);
const result = around ? around(this, joinPoint, ...args) : joinPoint.apply(this, args);
after && after(this, joinPoint.name, result, ...args);
return result;
}
}
function loggerAspect(joinPoint, logger) {
@SebastianHGonzalez
SebastianHGonzalez / secondTryAtAOP.js
Last active January 1, 2019 16:51
second try at AOP
function loggerAspect(joinPoint, logger) {
return function (...args) {
logger.log("logging");
return joinPoint.apply(this, args)
}
}
const psyduck = {
sound: "PSYDUCK!",
cuack: function() {return this.sound}
@SebastianHGonzalez
SebastianHGonzalez / firstTryAtAOP.js
Last active January 1, 2019 17:11
My first try at AOP
function loggerAspect(joinPoint, logger) {
return (...args) => {
logger.log("logging");
return joinPoint(...args);
}
}
function add(a, b) {
return a + b;
}
@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 / 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"
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) {
class LoggerAspect {
constructor(logger) {
this.logger = logger;
}
before(context, joinPointName, ...args) {
this.logger.log("logging");
}
}