Skip to content

Instantly share code, notes, and snippets.

View Zaggen's full-sized avatar

Louis Roman Zaggen

View GitHub Profile
@Zaggen
Zaggen / counter-easy-fix.js
Created July 17, 2019 13:47
Counter with event state handlers with default fix
const Counter = () => {
const [count, setCount] = React.useState(0);
React.useEffect(() => {
const handleScroll = () => {
setCount(countState => countState + 1);
};
document.addEventListener(“scroll”, handleScroll);
// Clean up
return () => document.removeEventListener(“scroll”, handleScroll);
}, []);
@Zaggen
Zaggen / file-parser.coffee
Last active April 22, 2017 03:14
Middleware to ease the handling of file uploads with extra data on sails.js (skipper), it relies on the FileManager service
module.exports = injector.set ()->
# Takes all input streams that have the following format
# inputName[file] that contains a stream and inputName[data] that contains metaData
# and combines them together and adds it to the req.body.inputName for easy access
fileParser = (req, res, next)->
if req.is('multipart/form-data') and req._fileparser?
do async =>
streamNames = _.map(req._fileparser.upstreams, 'fieldName')
try
###
@Zaggen
Zaggen / store-class.ts
Last active April 8, 2017 04:10
Flux/Redux linke store implementation experiment
declare const _
export class Store {
_reducers: {path: string, reducer: Function, id: number}[] = []
__state__: any
_id: number
_subscribers = {}
constructor(initialState){
this.__state__ = initialState
}
@Zaggen
Zaggen / event-emitter.ts
Created April 4, 2017 02:54
Event Emitter implementation in typescript
declare const _
export class EventEmitter {
_listeners: {[key: string]: Function[]} = {}
_eventQueue: {[key: string]: any[]} = {}
on(eventName, listener: Function) {
this._listeners[eventName] = this._listeners[eventName] || []
this._listeners[eventName].push(listener)
this._callPendingEvents(eventName)
@Zaggen
Zaggen / load-balancer.js
Last active May 26, 2022 16:44
Node.js load balancer with
/*
* Author: Zaggen - 2017
* version: 0.1
* github: https://github.com/Zaggen
* Free to use and modify
* */
const httpProxy = require('http-proxy')
const http = require('http')
const proxy = httpProxy.createProxyServer({})
@Zaggen
Zaggen / traits-decorator.ts
Last active February 23, 2017 16:18
Traits decorator
declare const _: _.LoDashStatic
export function traits(...traitsList) {
return function decorator(constructor: any) {
const mergedTraits = _.extend.apply(_, traitsList)
const {prototype} = constructor
const {__proto__} = constructor.prototype
const prototypeClone = _.clone(prototype) // We use a clone to avoid getting properties on the proto chain
constructor.prototype = _.extend(prototype, mergedTraits, prototypeClone)
constructor.prototype.constructor = constructor
declare const _: _.LoDashStatic
import {template} from '../helpers/index'
interface BackBoneComponent {
el?: string,
template?: string | Function,
events?: any,
subComponents?: any[],
[key: string]: any
}
@Zaggen
Zaggen / component-classes.ts
Last active January 29, 2017 23:40
Component classes that extend base backbone class, to have similar support to angular2 directives
interface IObjectLiteral {
[key: string]: any
}
// @description This type of view takes care of extra initialization
// and rendering of the element. It allows you to define components
// in a similar fashion of angular 2; You define a subComponents
// by adding its class to the subComponents array and add the corresponding
// tagName on the template, and it will be replaced at runtime. You can pass
// attributes to those subComponents by defining them on the empty tag element
export class ComponentView extends Backbone.View {
@Zaggen
Zaggen / formData pupulator helper
Created November 2, 2016 22:39
Helper to append json data to a form Data
helper = {
populateFormData: (formData, populationData)->
@_iterativePush(formData, populationData);
return formData
_iterativePush: (formData, v, k = '')->
if _.isObject(v)
_.each v, (v, innerK)=>
@_iterativePush(formData, v, "#{k}[#{innerK}]")
else
@Zaggen
Zaggen / base-model.coffee
Last active May 13, 2016 04:58
WIP: Backbone base model, that adds schema system with associations
class App.Models.Base extends Backbone.Model
schema: {}
set: (attributes, options)->
if _.isString(attributes)
[key, val] = [attributes, options]
(attributes = {})[key] = val
options = null
super(@filter(attributes), options)