Skip to content

Instantly share code, notes, and snippets.

Avatar
🚪

Tim Dorr timdorr

🚪
View GitHub Profile
@timdorr
timdorr / oldandbusted.js
Last active Feb 10, 2016
Reduce some boilerplate around React state
View oldandbusted.js
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 Feb 16, 2016 — forked from wesbos/lol.js
View 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 Feb 17, 2016
A common pitfall I'm seeing with server-side rendering in React Router
View badserver.js
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 Nov 13, 2015
Prefetching Data Decorator
View fetch_data.js
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 Jun 21, 2016
Tesla Autopark State Machine
View state_machine.java
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 Oct 24, 2016
Requesting a subscription to fire at a later time
View example.js
class Thing extends Component {
componentWillMount() {
this.subscriptionRequest = registerSubscriber(this.handleUpdate)
}
componentDidMount() {
startListening(this.subscriptionRequest)
}
}
@timdorr
timdorr / README.md
Created May 31, 2016
A basic Sidekiq rate limiter
View README.md

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 Dec 2, 2016
Clone (import/export) an Elasticsearch index
View clone_index.rb
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'])
View ConfirmButton.js
import React, { Component } from 'react'
class ConfirmButton extends Component {
state = {
sure: false
}
handleReallySure = event => {
event.preventDefault()
View react-redux.js
(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.
*