Skip to content

Instantly share code, notes, and snippets.

View aleclarson's full-sized avatar

Alec Larson aleclarson

View GitHub Profile
function roundToStep(n: number, step: number) {
n = Math.round(n / step) * step
// Determine the number of decimals to round.
let p = 0
if (n % 1) {
const s = String(n)
p = s.length - s.indexOf('.') - 1
}
import { createHook } from './createHook'
class App {
count = 0
increaseCount() {
this.count++
}
decreaseCount() {
this.count--
}
@aleclarson
aleclarson / useSubscriber.jsx
Created August 16, 2019 22:55
useSubscriber: Baked-in hook that lets subscribers notify React when data goes stale
import {useSubscriber} from 'react'
const MyComponent = (props) => {
// Get the current value of an observable
const value = props.value.get()
// Subscribe to the observable in the render phase
useSubscriber(forceUpdate => {
// This code runs immediately, instead of acting like "useEffect"
const unsub = props.value.subscribe(() => {
@aleclarson
aleclarson / CHANGELOG.md
Last active April 10, 2020 17:50
react-spring v9

Changes in v9

  • Animated values are now instances of the SpringValue class, which gives users access to an imperative API for updating a single animated value.
  • The reset and immediate props can now be a key or array of keys, instead of only a function or boolean.
  • The next function passed to async to prop can now be passed any useSpring props, instead of only to values.
  • The useSpring hook now supports a deps array, for updating an animation only when certain values changed.
  • The useTransition hook was rewritten from scratch. (see #809)
  • The useTransition hook now has a ref prop.
  • The new cancel prop stops the current animation and prevents future animations. It can be a boolean, a function, a key, or an array of keys.
  • Delayed updates can be cancelled by the cancel prop or stop method.

<HotKey> for react-native-macos

There are two types of hotkeys: global and local.

  • Global hotkeys work even when the application is inactive.
  • Local hotkeys only work when the application is active.

The implementation for global hotkeys would be adapted from davedelong/DDHotKey.

The implementation for local hotkeys would use the RCTKeyCommands class that already exists.

export interface Lens<T> {
(): T
(newValue: T): void
}
export function bind<T>(get: () => T, set: (newValue: T) => void): Lens<T>
export function bind<T, U>(target: Map<T, U>, key: T): Lens<U>
export function bind<T extends object, P extends keyof T>(
target: T,
key: P
#include <os/log.h>
RCTLogFunction RCTDefaultLogFunction = ^(
RCTLogLevel level,
__unused RCTLogSource source,
NSString *fileName,
NSNumber *lineNumber,
NSString *message
)
{
import * as React from 'react'
import { UserView } from './components/user'
import gql from 'vana/gql'
// Create a data store with vana.useGraphQL
const data = gql.create({
...options,
queries: {
users: gql`...`,
}
@aleclarson
aleclarson / runTopological.ts
Created June 10, 2019 02:21
Run an async action in topological order (dependencies run first)
export const runTopological = <T>(
packages: PackageJson[],
action: (pkg: PackageJson) => Promise<T>
): Promise<T[]> => {
const promises: Promise<T>[] = []
const run = (pkg: PackageJson, i: number) =>
promises[i] ||
(promises[i] = Promise.all(
Object.keys(pkg.dependencies || {}).map(name => {
const i = packages.findIndex(pkg => pkg.name === name)
// Convert "translate(10px) rotate(0)" into ["translate", "rotate"]
// This helps interpolate strings of different arity.
const keyRegex = /(?:^| )([\w-]+)\(([^ ,]+(?:(?:,[ ]*|[ ]+)[^ ,)]+)*)\)/g
/**
* Create a specialized interpolator for strings like:
*
* "translate(...) rotate(...)"
*/