Skip to content

Instantly share code, notes, and snippets.

View evanrs's full-sized avatar
🏋️‍♂️
Focusing

Evan Schneider evanrs

🏋️‍♂️
Focusing
View GitHub Profile
@evanrs
evanrs / README.md
Last active March 9, 2017 20:16
Debounce a state change, sustain for the duration by repeated calls.

Debounce a state change with a sustain.

This is debounce with true|false passed to the callback.

In this example the value of scrolling will be true during the scroll event and for 250ms after.

import sustain from 'sustain';
let scrolling = false;

sustainScrolling = sustain(250, (state) => scrolling = state)
@evanrs
evanrs / machine.js
Last active May 7, 2020 19:51
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@evanrs
evanrs / machine.js
Created May 7, 2020 19:58
Generated by XState Viz: https://xstate.js.org/viz
const todoMachine = Machine({
id: "todo",
initial: "reading",
context: {
id: undefined,
title: "",
prevTitle: "",
},
on: {
TOGGLE_COMPLETE: {
@evanrs
evanrs / once.ts
Last active January 29, 2022 04:43
variadic once in typescript
type ArityNone<T> = () => T;
type ArityOne<T, A> = (a?: A) => T;
type ArityTwo<T, A, B> = (a: A, b?: B) => T;
type ArityThree<T, A, B, C> = (a: A, b: B, c?: C) => T;
type ArityFour<T, A, B, C, D> = (a: A, b: B, c: C, d?: D) => T;
export function once<T>(resolver: ArityNone<T>): ArityNone<T>;
export function once<T, A>(resolver: ArityOne<T, A>): ArityOne<T, A>;
export function once<T, A, B>(resolver: ArityTwo<T, A, B>): ArityTwo<T, A, B>;
export function once<T, A, B, C>(resolver: ArityThree<T, A, B, C>): ArityThree<T, A, B, C>;
@evanrs
evanrs / toHTTPie.ts
Created July 9, 2020 14:52
Convert to HTTPie request
import { map } from "lodash";
export type HTTPieOptions = {
method: string;
headers?: Record<string, string>;
body?: string;
data?: unknown[] | Record<string, unknown>;
};
export function toHTTPie(url: string, { method, ...options }: HTTPieOptions) {

When it comes to memo'ization any option will be valid if it meets the following three criteria.

1. Will this work be closer to the root, or the leaf?

As we approach the leaves of the component tree the impact of memo'ization will be diminished as the number of impacted children decreases.

As the number of potential children increases we offer a corresponding level of care in making sure our values only update when there's a pertinent change to that value.

2. Where will my work be used?