Skip to content

Instantly share code, notes, and snippets.

View k1r0s's full-sized avatar

Ciro Iván k1r0s

View GitHub Profile
@k1r0s
k1r0s / material-dialog-ccc.ts
Last active August 20, 2017 09:56
managing material's dialog is a common CCC in angular apps
// normal aproach
public doStuff () {
this.instanceDialogRef = this.mdDialogInstance.open(YourDialogComponent)
...
}
// advice aproach
@ShowDialog(YourDialogComponent)
public doStuff () {
...
@k1r0s
k1r0s / canvas-gam2d.js
Created July 4, 2017 23:22
wrap canvas 2d api in several OOP patterns
var GameScene = require("../common/GameScene");
var GameControls = require("../common/GameControls");
var Player = require("./Player");
var Wall = require("./Wall");
window.scene1 = new GameScene("#sampleScene");
var player = new Player({x: 35, y: 220});
var leftBox = new Wall({x: 0, y: 0}, {x: 0, y: 500});
var bottomBox = new Wall({x: 0, y: 500}, {x: 500, y: 500});
@k1r0s
k1r0s / persistance-advices.ts
Last active June 3, 2017 14:58
kaop-ts Async advice implementation example
import { AdvicePool, adviceMetadata, adviceParam, IMetadata } from 'kaop-ts'
import { Service } from './somewhere'
import { ICommonModel } from './somewhere'
export class PersistanceAdvices extends AdvicePool {
static read (@adviceMetadata meta: IMetadata, @adviceParam(0) model: ICommonModel) {
Service.get(model.url)
.then(data => meta.args.push(data))
.then(this.next)
}
@k1r0s
k1r0s / viewAsync.ts
Created June 3, 2017 14:56
kaop-ts Async operations within the same call-stack
import { beforeMethod } from 'kaop-ts'
import { PersistanceAdvices } from './persistance-advices'
import { FlowAdvices } from './flow-advices'
import { OrderModel } from './order-model'
class View {
@beforeMethod(PersistanceAdvices.read, OrderModel)
@beforeMethod(FlowAdvices.validate)
update (data?) { ... }
}
@k1r0s
k1r0s / metadata-show-case.ts
Created June 3, 2017 14:54
kaop-ts IMetadata property
export class Registry extends AdvicePool {
 static log (@adviceMetadata meta: IMetadata) {
meta.args // Arguments to be received by decorated method
meta.propertyKey // Name of the decorated method as string
meta.scope // Instance or the context of the call stack
meta.rawMethod // Original method (contains metadata)
meta.target // Class definition
meta.result // The returned value by the method
 }
}
@k1r0s
k1r0s / Covfefe.component.tsx
Created June 3, 2017 14:52
Return a React component within an advice
import { AdvicePool, adviceMetadata, IMetadata } from 'kaop-ts'
import { Covfefe } from './covfefe-components'
export class Advices extends AdvicePool {
static blameRussia (@adviceMetadata meta: IMetadata) {
if(meta.exception) {
meta.result = <Covfefe/>
}
}
}
@k1r0s
k1r0s / Advices.ts
Created June 3, 2017 14:51
capturing exceptions with kaop-ts
import { AdvicePool, adviceMetadata, IMetadata } from 'kaop-ts'
export class Advices extends AdvicePool {
static blameCovfefe (@adviceMetadata meta: IMetadata) {
meta.exception.message += " despite the constant negative press covfefe"
}
static throwOnError (@adviceMetadata meta: IMetadata) {
if(meta.exception) {
throw meta.exception
@k1r0s
k1r0s / Root.main.tsx
Created June 3, 2017 14:45
kaop-ts onException show case
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> {
@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)
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){