Skip to content

Instantly share code, notes, and snippets.

James K Nelson jamesknelson

Block or report user

Report or block jamesknelson

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
jamesknelson / model.js
Created Nov 24, 2019
A model class for storing data fetched with React Suspense
View model.js
const PurgeDelay = 1000
class Model {
fetcher: (id: string) => Promise<any>
cache = {}
callbacks = {}
holds = {}
purgeTimeouts = {}
jamesknelson / createSubscribe.js
Created Oct 22, 2019
Turn an async function into something you can subscribe to (untested)
View createSubscribe.js
const requests = {}
export const createSubscribe = (asyncFn) => {
return subscribeToData = (params, onUpdate) => {
const key = JSON.stringify(params)
const request = requests[key]
if (!request) {
request = requests[key] = {
jamesknelson /
Last active Oct 3, 2019
Convert a video into a gif

A 4-line shell script to convert your movies into gifs. Defaults to 10fps, 700px wide.

WIDTH=420 FPS=12 togif

You'll need to have ffmpeg installed -- on mac, you can do this with brew:

brew install ffmpeg
jamesknelson / BlackTriangle.js
Last active Apr 8, 2019
A black triangle. It spins if ES6 works, otherwise it doesn't.
View BlackTriangle.js
export default class BlackTriangle {
constructor(selector) {
this.angle = 0;
this.innerEl = document.querySelector(selector).querySelector('.BlackTriangle-inner');
rotate(amount) {
this.angle = (this.angle + amount) % 360;
View concatStringsAndStreams.js
const CombinedStream = require('combined-stream2');
function concatStringsAndStreams(strings, ...args) {
let combinedStream = CombinedStream.create()
combinedStream.append(Buffer.from(strings[0], 'utf8'))
for (let i = 0; i < args.length; i++) {
let arg = args[i]
let string = strings[i+1]
if (arg && arg.pipe) {

mapObservableToChunkIterator(observable, mapValueToChunks, mapErrorToChunks?): Generator<Promise | Chunk[]>

A function that accepts an Observable with a subscribe() method, and optionally with a getCurrentValue() or getCurrentResult() method.

It returns a Generator that yields Promise (when waiting for a new value) or Chunk objects (i.e. objects that extend { type: string }).

By default, mapErrorToChunks takes the error emitted by an observable, and yields [{ type: 'error', error: error }].

When the observable is complete, the Generator's next() function will return { done: true }.

jamesknelson / component-routing.js
Last active Mar 19, 2019
Two APIs for routing with React that support POST methods and SSR.
View component-routing.js
This component and hook based routing/fetching API won't work, as `useAsync()`
is an impossible component.
In order to use async functions to respond to route changes, the functions will
need to be registered with a parent cache/provider with a unique key. As such, a
more natural component-based architecture would involve a `<Route path>` component
View UpdatePasswordForm.jsx
import { map, redirect, route } from 'navi'
function UpdatePasswordForm({ submitErrors }) {
return (
validate={value =>
value.password !== value.passwordConfirmation &&
View gist:432f00af5522ea07cbb39990a8105e0c
A React app can be split into a number of different types of functions and components, where *each type can depend only on the types above it in the hierarchy*.
This makes gives you a way to split up components and functions over your filesystem. It also helps you to keep components from growing too large, and encourages practices that make testing easier.
types (TypeScript typings)
utils (plain javascript functions)
contexts (React context, and provider components)
jamesknelson /
Last active Aug 22, 2018
React CRUV - 4 directories to solve project structure for good
create-react-app myapp
cd myapp/src
mkdir containers routes utils views
touch config.js contexts.js
mv App.* routes
sed -i '' -e 's/.\/App/.\/routes\/App/g' index.js
You can’t perform that action at this time.