Skip to content

Instantly share code, notes, and snippets.

View k1r0s's full-sized avatar

Ciro k1r0s

View GitHub Profile
@k1r0s
k1r0s / Invoice.js
Created March 21, 2017 14:57
This is an example of what we normaly do in OOP
class Invoice {
...
doCheckout(shippingDetails){
if(this.validate(shippingDetails)){
this.shippingDetails = shippingDetails
var serializedInstance = JSON.stringify(this)
$OurAjaxService.post({
url: this.url,
data: serializedInstance
}).then((response) => {
@k1r0s
k1r0s / Invoice.ts
Last active July 30, 2018 15:29
What we can achieve using AOP with ES7 Decorators
class Invoice {
...
@beforeMethod(Validators.customValidate)
@afterMethod(Serialize)
@afterMethod(AsyncAdvices.postResult, 'url..')
@afterMethod(triggerUpdates)
doCheckout(shippingDetails){
this.shippingDetails = shippingDetails
}
...
@k1r0s
k1r0s / Programmer.js
Last active February 10, 2018 13:30
OOP tricks in ES5 using kaop
const { createClass, extend, override } = require('kaop');
const Person = createClass({
name: undefined,
constructor: function(name){
this.name = name;
},
sayHello: function() {
return "Hey, I'm " + this.name;
}
@k1r0s
k1r0s / override.js
Last active February 10, 2018 13:25
override advice implementation using kaop
const reflect = require("./reflect");
const apply = reflect.advice(meta =>
meta.target.super.prototype[meta.key].apply(meta.scope, meta.args));
const implement = reflect.advice(meta =>
meta.args.unshift(meta.target.super.prototype[meta.key].bind(meta.scope));
module.exports = {
apply,

These properties can be accessed by:

@beforeInstance(function(meta) {
  meta.args // Arguments to be received by decorated method
  meta.key // Name of the decorated method as string
  meta.scope // Instance or the context of the call stack
  meta.method // Original method
  meta.target // Class definition
 meta.result // The returned value by the method
@k1r0s
k1r0s / abstract-resource.js
Last active February 10, 2018 13:53
nodeProvider provides a single node http server instance to every AbstractResource instance to allow each request to be handled by one resource subclass
const { createClass, inject } = require("kaop");
const { nodeProvider } = require("./node-server");
const url = require("url");
module.exports = AbstractResource = createClass({
constructor: [inject.args(nodeProvider), function(resourceIdentifier, node) {
this.uri = resourceIdentifier;
node.server.on("request", (req, res) =>
req.url.includes(resourceIdentifier) && this.processRequest(req, res));
}],
@k1r0s
k1r0s / kaop-showcase.js
Last active March 22, 2017 07:42
kaop showcase about advice placements
var Dummy = Class({
someMethod: [ //decoratedMethod
"subscribe","$inject", //befores
function($$dep1){
//method body
},
"trigger: 'action'", //afters (advice with an argument)
],
anotherMethod: function(){
/* method without advices */
var Advices = require("kaop").Advices;
Advices.add(
function log(){
//meta.args contains the arguments {array}
console.log(meta.methodName + " called");
console.log("with arguments: " + meta.args);
console.log("returned: " + meta.result);
}
)
Controller = Class({
constructor: function(app){
app.get('/', this.home);
app.get('/login', this.login);
app.use(this.notFound);
},
login: [function(req, res){
//what ever
}, "log"],
home: [function(req, res){
@k1r0s
k1r0s / Root.main.tsx
Created June 3, 2017 07:19
Handle exceptions in React components
import * as React from "react";
import Sidebar from "../Sidebar/Sidebar.main";
import Content from "../Content/Content.main";
import { Advices } from "../../advices/Advices"
import { onException, afterMethod } from "kaop-ts"
export default class Root extends React.Component<null, null> {
@onException(Advices.blameCovfefe)