Skip to content

Instantly share code, notes, and snippets.

@Sawtaytoes
Sawtaytoes / TestComponent.js
Last active September 20, 2018 03:25
Example using `connect` from react-redux.
import React from 'react'
import { connect } from 'react-redux'
const TestComponent = ({
text,
}) => (
<div>
{text}
</div>
)
@Sawtaytoes
Sawtaytoes / ReduxConnect.js
Last active September 20, 2018 04:55
Attempt at replacing react-redux's `connect` HoC with a `render` prop.
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
export const ReduxConnect = ({
mapDispatchToProps,
mapStateToProps,
render,
}) => (
connect(
(mapStateToProps || null),
@Sawtaytoes
Sawtaytoes / simple-async-react-router-v4.jsx
Created September 20, 2018 02:48
Simple Async React Router v4 Example
import React, { PureComponent } from 'react'
import { Match, Miss, Redirect } from 'react-router'
export default class Routes extends PureComponent {
constructor() {
super()
this.views = {}
}
@Sawtaytoes
Sawtaytoes / complex-async-react-router-v4.jsx
Created September 20, 2018 02:49
Complex Async React Router v4 Example
import React, { PureComponent } from 'react'
import { Match, Miss, Redirect } from 'react-router'
const isAsyncCapable = typeof window !== 'undefined'
export default class Routes extends PureComponent {
constructor() {
super()
this.views = {}
@Sawtaytoes
Sawtaytoes / 3-press-and-hold.js
Created September 20, 2018 02:49
Adding a press-and-hold action
import {
BUTTON_DOWN,
BUTTON_UP,
holdPressAction,
singlePressAction,
} from './actions'
const timeLimit = 300
let startTime = Date.now()
@Sawtaytoes
Sawtaytoes / 4-combined-press-listeners.js
Created September 20, 2018 02:49
Rewriting Flic's button press listener to reduce the timeout between presses
import {
BUTTON_DOWN,
BUTTON_UP,
doublePressAction,
holdPressAction,
singlePressAction,
} from './actions'
const timeLimit = 300
@Sawtaytoes
Sawtaytoes / 5-observable-pattern-intro.js
Created September 20, 2018 02:49
Introductory listener using an observable
import {
BUTTON_DOWN,
singlePressAction,
} from './actions'
const createButtonObserver = bluetoothAddress => observer => (
new FlicConnectionChannel(bluetoothAddress)
.on(
'buttonUpOrDown',
buttonPressState => observer.next(buttonPressState)
@Sawtaytoes
Sawtaytoes / 6-overvable-pattern-implementation.js
Created September 20, 2018 02:49
Implementing the observable pattern and solving the real problem of listening for button presses
import {
BUTTON_UP,
doButtonPressAction,
} from './actions'
const createButtonObserver = bluetoothAddress => observer => (
new FlicConnectionChannel(bluetoothAddress)
.on(
'buttonUpOrDown',
buttonPressState => observer.next(buttonPressState)
@Sawtaytoes
Sawtaytoes / 7-generic-multi-press-listener.js
Created September 20, 2018 02:49
A non-observable solution using the same code concepts as the observable pattern.
import {
BUTTON_DOWN,
BUTTON_UP,
doButtonPressAction,
} from './actions'
const timeLimit = 300
let timeoutId = 0
@Sawtaytoes
Sawtaytoes / 2-double-press.js
Created September 20, 2018 02:49
Flic Button listener for single- and double-button presses.
import {
BUTTON_DOWN,
doublePressAction,
singlePressAction,
} from './actions'
const timeLimit = 300
let startTime = Date.now()
let timeoutId = 0