Skip to content

Instantly share code, notes, and snippets.

View timdorr's full-sized avatar
🚪

Tim Dorr timdorr

🚪
View GitHub Profile
@timdorr
timdorr / oldandbusted.js
Last active February 10, 2016 11:11
Reduce some boilerplate around React state
class Foo extends Component {
constructor(props, context) {
super(props, context)
this.state = {
show: false
}
}
handleClick = () => {
this.setState({ show: true })
@timdorr
timdorr / lol.js
Last active February 16, 2016 22:16 — forked from wesbos/lol.js
// paste in your console
speechSynthesis.onvoiceschanged = function() {
var msg = new SpeechSynthesisUtterance();
msg.voice = this.getVoices().filter(v => v.name == 'Cellos')[0];
msg.text = document.body.textContent;
this.speak(msg);
};
@timdorr
timdorr / badserver.js
Last active February 17, 2016 02:32
A common pitfall I'm seeing with server-side rendering in React Router
app.use(function (req, res) {
const history = createMemoryHistory() // <- No arg creates a history with the default first location: '/'
// Do some stuff with history
match({ history, routes, location: req.url }, (error, redirectLocation, renderProps) => {
@timdorr
timdorr / fetch_data.js
Created November 13, 2015 16:36
Prefetching Data Decorator
import React, { Component, PropTypes } from 'react';
import hoistStatics from 'hoist-non-react-statics';
const { shape, func } = PropTypes;
let initialRender = true;
export function rendered() {
initialRender = false;
}
@timdorr
timdorr / state_machine.java
Created June 21, 2016 03:53
Tesla Autopark State Machine
from(APState.InitialScreenOpen).to(APState.InitialScreenOpen).on(APEvent.StartedScreenOpen);
from(APState.InitialScreenOpen).to(APState.Connecting).on(APEvent.StartedConnectingToWebSocket);
from(APState.Disconnected).to(APState.Connecting).on(APEvent.StartedConnectingToWebSocket);
from(APState.Connecting).to(APState.WaitingForInitialVehicleStatus).on(APEvent.BecameConnectedToWebSocket);
from(APState.WaitingForInitialVehicleStatus).to(APState.Disconnecting).on(APEvent.IsDisconnectingFromWebSocket);
from(APState.WaitingForNextVehicleStatus).to(APState.Disconnecting).on(APEvent.IsDisconnectingFromWebSocket);
from(APState.AutoparkReadyCriteriaNotMet).to(APState.Disconnecting).on(APEvent.IsDisconnectingFromWebSocket);
from(APState.AutoparkForwardOnlyReady).to(APState.Disconnecting).on(APEvent.IsDisconnectingFromWebSocket);
from(APState.AutoparkReverseOnlyReady).to(APState.Disconnecting).on(APEvent.IsDisconnectingFromWebSocket);
from(APState.AutoparkBidirectionalReady).to(APState.Disconnecting).on(APEvent.IsDisconn
@timdorr
timdorr / example.js
Created October 24, 2016 16:31
Requesting a subscription to fire at a later time
class Thing extends Component {
componentWillMount() {
this.subscriptionRequest = registerSubscriber(this.handleUpdate)
}
componentDidMount() {
startListening(this.subscriptionRequest)
}
}
@timdorr
timdorr / README.md
Created May 31, 2016 22:14
A basic Sidekiq rate limiter

Don't copypasta this!

Note that this is very purpose-built. The job args in particular depend on a unique key being in the first position.

There is also plenty of brittle code all over the place.

Instead, use this as a guide for how you might build your own. A lot of the parts are the same between implementations.

@timdorr
timdorr / clone_index.rb
Last active December 2, 2016 16:51
Clone (import/export) an Elasticsearch index
index = args[:index] # If you're in a Rake task
# You'll have to write your own code for this part.
# It just creates an index for you, like so:
# es.indices.create(index: index, body: mappings_and_such)
ci = CreateIndex.new(index)
ci.create
es = Elasticsearch::Client.new
es_prod = Elasticsearch::Client.new(url: ENV['PROD_ELASTICSEARCH_URL'])
import React, { Component } from 'react'
class ConfirmButton extends Component {
state = {
sure: false
}
handleReallySure = event => {
event.preventDefault()
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react'), require('redux')) :
typeof define === 'function' && define.amd ? define(['exports', 'react', 'redux'], factory) :
(factory((global.ReactRedux = global.ReactRedux || {}),global.React,global.Redux));
}(this, (function (exports,react,redux) { 'use strict';
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*