Skip to content

Instantly share code, notes, and snippets.

View bcherny's full-sized avatar

Boris Cherny bcherny

View GitHub Profile
@bcherny
bcherny / $arguments.flow.js
Created June 10, 2019 00:03
Flow: $Arguments
type $Arguments<F> = $Call<<A: $ReadOnlyArray<mixed>>((...A) => mixed) => A, F>;
type $MethodArguments<O: {}, F: $Keys<O>> = $Arguments<
$NonMaybeType<$ElementType<O, F>>,
>;
/*
Nice things about this approach:
- No need to expose $refs to users. Just use regular IDs instead.
- No need for double-composition (just compose React components, not queries).
- Composition becomes less magical (since we're passing regular IDs around).
- Collapse Query and Fragment concepts into Query.
- Take advantage of existing template strings to pass in query variables.
let called = false;
// this 'scope' is safe to interact with the React component,
// rendering and clicking as you please
await render(
<App
callback={() => {
called = true;
}}
/>,
document.body
/*
Results of a few runs:
$ node ./index.js
{
"a": 1000
}
$ node ./index.js
{
let {readFile} = require('fs')
let tally = {ab: 0, ba: 0}
let globalDone = 0
let COUNT = 1000
for (let x = 0; x < COUNT; x++) {
let result = ''
let done = 0
@bcherny
bcherny / Option-fp.ts
Last active August 19, 2018 21:29
Option implementation, once https://github.com/Microsoft/TypeScript/issues/7294 is fixed
type Option<T> = Some<T> | None
type Some<T> = {
flatMap<U>(f: (value: T) => None): None
flatMap<U>(f: (value: T) => Some<U>): Some<U>
flatMap<U>(f: (value: T) => Option<U>): Option<U>
getOrElse(value: T): T
}
type None = {
@bcherny
bcherny / Option.ts
Last active July 9, 2019 22:52
Draft: Reference Option implementation for Programming TypeScript
function Option<T>(value?: null | undefined): None
function Option<T>(value: T): Some<T>
function Option<T>(value?: T): Option<T> {
if (value == null) {
return new None
}
return new Some(value)
}
interface Option<T> {
@bcherny
bcherny / compiled-js-file-sizes.md
Last active July 29, 2018 20:41
Compiled file sizes of common JavaScript dependencies

Using Webpack + Uglify + treeshaking + commonjs (not es2015).

Setup: https://github.com/bcherny/rxjs-filesize-repro

Package import Size Size (gzipped)
rxjs Observable 12kb 3kb
react Component 9kb 4kb
react-dom render 100kb 33kb
lodash map 20kb 6kb
@bcherny
bcherny / gcable-undux.md
Last active June 11, 2018 00:37
Thoughtstream: GC'able Undux stores

Thoughtstream: GC'able Undux stores

  • Stores would no longer be singletons
  • No top level createStore
  • Instead, use store factory that creates store instances
  • Created for you as part of connect from an initial state
let withStore = connect({
 a: 1,