This is what we get on execution:
Uncaught TypeError: Cannot read property ‘nil’ of undefined despite the constant negative press covfefe
Here is the implementation of `Advices`
Maybe this is not a good example but if you know twitter API it might be useful to distract several taxpayers..
But if you think previous example was funny somehow.. Instead of throwing an exception, it would be nice render an error component each time our app fails:
Welp. The idea about Aspect Oriented Programming (aka AOP) is to manage common problems in one place and still access to the needed context.
In this article we’re going to use kaop-ts
In the previous example we need to capture exceptions when occur somehow but we don’t want to mess our code pasting same blocks of code everywhere. We simply want to tell our application to apply encapsulated patterns in several life cycle hooks in OOP paradigm (aka: ‘Join Points’).
AOP is an extension of OOP. Helps us to encourage abstraction and modularity. Is an strong tool against repetition.
You probably remember situations where you have to paste some blocks of code only replacing one variable or reference in order to replicate some behavior. Think about logs, yeah.
But there are many other cases, most of them are fairly covered by frameworks. In NodeJS environments for example Express deals with lots of “common problems” that maybe almost nobody have to deal with, like receiving payload from a HTTP request.
AOP is everywhere, but most frameworks don’t provide you tools to extend this technique, I mean by provide access to Join Points. AngularJS, for example, brought a bunch of nice solutions to common problems when JQuery or EJS/Underscore templates was the only way to face DOM manipulation.
Modern frameworks that include DOM manipulation like Vue or Angular, and many others, encourages declarative programming, which includes implicit instructions to be executed to achieve its purpose (DOM manipulation) my removing the side effects. React is more explicit somehow but the idea of JSX with virtualDOM behind was simply awesome.
kaop-ts is nice to build big things from the bottom with a powerful tool of abstraction and architecture assets. But also allows enhance your code by providing access to Join Points (aka: ‘apply encapsulated patterns in several life cycle hooks in OOP paradigm by accessing dynamic contexts’).
Nowadays it includes access to: `AfterInstance, BeforeInstance, AfterMethod, BeforeMethod, OnException` where you can retrieve:
Even you may read and write these references and, of course, perform async request without messing the call-stack. Let me say, in other words. You can access service layer within Advices (aka: ‘functions that are executed within Join Points’) and perform async requests to retrieve data and inject to decorated methods.
Read carefully this piece of code:
this.next is a tool to explicitly tell kaop-ts that current advice needs time to be done, so following operations will wait until fulfilled
Note that `View class` has a method called update, if we instantiate View and call .update() on it we re going to trigger a composed call-stack, but the implementation is clean, declarative and is out of side effects.
Our `viewInstance` will receive request data as an argument in update method and it will be executed as if we provided explicitly as an argument.
So, thanks for reading. Hope you enjoy it. Checkout the repo and feel free to contribute :) happy codding!
Hail covfefe.