Skip to content

Instantly share code, notes, and snippets.

@xfsnowind
xfsnowind / debounce.js
Created October 23, 2015 22:31
debounce method on blog "Introducing Javascript method debounce" on 2014/05/12
function debounce(func, wait, immediate) {
var timeout;
return function () {
var obj = this, argus = arguments;
function delayed () {
if (!immediate) {
func.apply(obj, argus);
}
timeout = null;
}
@xfsnowind
xfsnowind / prepare.js
Created October 23, 2015 22:36
preparation codes on blog "React -- Communication between components" on 2014/06/24
// For Parent.js
var Parent = React.createClass({
render: function () {
return (
<div><input onchange="" type="text" value="Parent"></input><child></child></div>
);
}
});
// For Child.js
@xfsnowind
xfsnowind / parent->child-triggered-parent.js
Last active October 23, 2015 22:40
communication from parent to child and triggered by parenton blog "React -- Communication between components" on 2014/06/24
// For Parent.js
var Parent = React.createClass({
getInitialState: function () {
return {
changedValue: ""
};
},
// the callback function for changing the text in <input type="text"></input>
changeHandler: function (event) {
@xfsnowind
xfsnowind / child->parent-trigged-parent.js
Last active October 23, 2015 22:41
communication from child to parent and triggered by parent on blog "React -- Communication between components" on 2014/06/24
// For Parent.js
var Parent = React.createClass({
getInitialState: function () {
return { childRef: "childRef" };
},
changeHandler: function () {
// use ref to call the instance of Child
this.refs[this.state.childRef].getChildValue();
},
@xfsnowind
xfsnowind / parent->child-triggered-child.js
Created October 23, 2015 22:41
communication from parent to child and triggered by child on blog "React -- Communication between components" on 2014/06/24
// For Parent.js
var Parent = React.createClass({
getInitialState: function () {
return { parentValue: "" };
},
// the callback function is passed to Child as props
passValueFunc: function (para) {
if (this.state[para] != undefined) {
return this.state[para];
@xfsnowind
xfsnowind / child->parent-trigged-child.js
Created October 23, 2015 22:42
communication from child to parent and triggered by child on blog "React -- Communication between components" on 2014/06/24
// For Parent.js
var Parent = React.createClass({
getInitialState: function () {
return { parentValue: "" };
},
// the callback function is passed to Child as props
passValueFunc: function (para) {
// handle the passed value from Child
},
@xfsnowind
xfsnowind / memoize.clj
Created October 23, 2015 22:46
official standard memoize function
(defn memoize
[f]
(let [mem (atom {})]
(fn [& args]
(if-let [e (find @mem args)]
(val e)
(let [ret (apply f args)]
(swap! mem assoc args ret)
ret)))))
@xfsnowind
xfsnowind / debounce-use.clj
Last active October 24, 2015 00:03
usage debounce-clj on blog "Debounce and Throttle in Clojure" on 2015/10/24
(def sync-source (chan))
(def sync-sink (debounce sync-source 10000))
(defn handle-event! [event]
(when-let
[handler (case (:type event)
:one-event function-wanna-call
unknown-event)]
(try
(apply handler (:args event))
@xfsnowind
xfsnowind / reserve.clj
Created December 9, 2015 08:42 — forked from eslick/reserve.clj
Simple reservation for multiple worker peers using Datomic
(ns experiment.infra.election
(:use [datomic.api :only [q] :as d])
(:require [experiment.system :as sys]
[experiment.infra.data :as data]
[experiment.infra.protocols :as p]))
;;
;; Reserve - Simple peer coordination mechanism
;;
;; Reservations support multiple peers trying to distribution operations
@xfsnowind
xfsnowind / download-skam-norsk-subtitle.js
Created December 29, 2016 00:19
last ned disse norske tekstene til den tve serien "Skam" sesong 1-3 og lagre som srt filer
#!/usr/bin/env node
var request = require('request'),
fs = require("fs");
var skamSesongSubtitles = [["1", 'MSUB1912', '16AW', 11],
["2", 'MYNT1500', '16AA', 12],
["3", 'MYNT1520', '16AA', 10]];
// get the subtitle content with given sesong number, episode, url, saving folder and callback function