This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inject(definition) { | |
definition.decoreateTargets(joinPoint => this.decorate(joinPoint)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addAspect(aspect) { | |
this.aspects = [...this.aspects, aspect]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LoggerAspect { | |
constructor(logger) { | |
this.logger = logger; | |
} | |
before(context, joinPointName, ...args) { | |
this.logger.log("logging"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PointCut { | |
constructor(definition) { | |
this.inject(definition); | |
} | |
inject(definition) { | |
this.injectAspectToObjectMethods(definition.class.prototype); | |
} | |
injectAspectToObjectMethods(prototype) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const personGreetingPC = new PointCut({class: Person, methodName: "greet"}); | |
jhon.greet(); | |
// <- "Hello jhon" | |
personGreetingPC.addAspect(loggerAspect); | |
jhon.greet(); | |
// logging | |
// <- "Hello jhon" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function greet() { | |
return "Hello " + this.name ; | |
} | |
const john = { | |
name: "John" | |
} | |
const jane = { | |
name: "Jane" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) |