Skip to content

Instantly share code, notes, and snippets.

View pleasetrythisathome's full-sized avatar

Scarlet Dame pleasetrythisathome

View GitHub Profile
@pleasetrythisathome
pleasetrythisathome / gist:9647688
Created March 19, 2014 18:06
state animation for React.js using d3.js and some underscore.js sugar
animate: function(attr, targetValue, duration, ease) {
var cmp = this;
var interpolator;
if (_.isFunction(targetValue)) {
interpolator = targetValue;
} else {
interpolator = d3.interpolate(this.state[attr], targetValue);
}
@pleasetrythisathome
pleasetrythisathome / gist:9648154
Last active August 29, 2015 13:57
react.animate example
var component = React.createClass({
mixins: [React.Animate],
getInitialState: function() {
return {
width: 100
};
},
render: function() {
var heightBounds = [50, 100];
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.counties {
fill: none;
}
.states {
fill: none;
@pleasetrythisathome
pleasetrythisathome / multi.cljs
Last active August 29, 2015 13:58
stab at multimethods for app view
(def app-state (atom {:section :main
:sections {:admin {:id :admin
:title "Admin"}
:main {:id :main
:title "Main"}
:other {:id :other
:title "Other"}}}))
(defn button
[{:keys [id title] :as section} owner]
@pleasetrythisathome
pleasetrythisathome / validators.clj
Last active August 29, 2015 14:00
clj validators
(defn validator
[msg fn]
(letfn [(validate [& args]
(apply fn args))]
(with-meta validate {:msg msg})))
(defn checker
[& validators]
(fn [v]
(clojure.core/reduce (fn [errors validator]
@pleasetrythisathome
pleasetrythisathome / form.cljs
Last active May 7, 2016 05:45
om forms - form parent local state as cursor
(def inputs [{:korks :email}
{:korks [:name :first]}
{:korks [:name :last]}])
(defn form [app owner]
(reify
om/IInitState
(init-state [this]
(reduce (fn [state {:keys [korks]}]
(assoc-in state korks nil))
@pleasetrythisathome
pleasetrythisathome / go_aware.cljs
Last active August 29, 2015 14:04
Go block component lifecycle mixin
(defmixin go-block-aware
(init-state [this]
{:chans {:mounted (async/chan)}})
(will-unmount [this]
(async/close! (om/get-state owner [:chans :mounted])))
(go [owner read-chan callback]
(let [mounted (om/get-state owner [:chans :mounted])]
(go-loop []
(when-some [v (first (async/alts! [read-chan exit-chan]))]
@pleasetrythisathome
pleasetrythisathome / raf.cljs
Last active August 29, 2015 14:05
Request Animation Frame Channel
(defn animation-frame
"Return a channel which will close on the nth next animation frame."
([] (animation-frame 1))
([n] (animation-frame n (async/chan 1)))
([n out]
(js/window.requestAnimationFrame
(fn [timestamp]
(if (= n 1)
(do
(async/put! out timestamp)
@pleasetrythisathome
pleasetrythisathome / control_loop.clj
Last active August 29, 2015 14:06
play, pause, kill a go-loop
(defn control-loop
"calls f on each value read from read-chan
returns a control channel that can :play, :pause, and :kill the read loop"
([f read-chan] (control-loop f read-chan (chan)))
([f read-chan control]
(go-loop [action :play]
(let [[v c] (alts! (condp = action
:play [read-chan control]
:pause [control]))]
(when-not (nil? v)
@pleasetrythisathome
pleasetrythisathome / browser.cljs
Created October 7, 2014 19:00
clojurescript browser utils
(ns browser
(:require-macros [cljs.core.async.macros :refer [go go-loop]])
(:require [goog.events :as events]
[clojure.string :as str]
[cljs.core.async :refer [put! <! chan]])
(:import goog.History
goog.history.Html5History
goog.history.Html5History.TokenTransformer
goog.history.EventType))