Skip to content

Instantly share code, notes, and snippets.

View timdorr's full-sized avatar
🚪

Tim Dorr timdorr

🚪
View GitHub Profile
@timdorr
timdorr / routes.js
Created January 27, 2016 16:26
Some common routes (taken from the react-router examples)
import { Route } from 'react-router'
import App from 'components/App'
import Page1 from 'components/Page1'
import Page2 from 'components/Page2'
import Tab1 from 'components/Tab1'
import Tab2 from 'components/Tab2'
export default const routes = (
<Route path="/" component={App}>
@timdorr
timdorr / app.js
Created January 27, 2016 16:30
A possible reroute component
import { Component } from 'reroute';
export default class App extends Component {
render() {
return (
<div>{this.props.params.id}</div>
)
}
}
@timdorr
timdorr / autoload.routes.js
Created January 29, 2016 04:48
Autoloaded JS
export default const routes = (
<App path="/">
<Page1 path="page1">
<Tab1 path="tab1" />
<Tab2 path="tab2" />
</Page1>
<Page2 path="page2">
<Tab1 path="tab1" />
<Tab2 path="tab2" />
</Page2>
@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 / 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 / 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 / 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'])