Instantly share code, notes, and snippets.

View gist:4b31ec396c4cbfebede55558f6238891
```
R__________
/ \
a___ b1__
/ \ / \
a2 b3 a3 b2__
/ \ / \ / \ / \ \
e1 e2 e3 e4 e5 e6 e7 e8 e9
```
View 01-mobx-canvas.tsx
export interface MobxCanvasProps {
width?: number;
height?: number;
style?: any;
render(ctx: CanvasRenderingContext2D): void;
}
@observer
export class MobxCanvas extends React.Component<CanvasProps> {
private canvasRef = React.createRef();
View 01-command.sh
script -qfc 'yarn repro' /dev/null > raw.log
cat -vet raw.log
View experiment.spec.ts
import { validator, validate } from './experiment';
class U {
@validator() name!: string;
}
class C {
@validator() id!: string;
@validator() u!: U;
}
View debounce-example.js
const {observable, computed, action, extendObservable} = mobx;
const {observer} = mobxReact;
const {Component} = React;
let debounce = delay => (object, key, descriptor) => {
let getter = descriptor.get
let state = new WeakMap();
function initializeState(self) {
View cancel-via-race.js
function delay(n) { return new Promise(r => setTimeout(r, n)) }
function debounced(ms, action) {
let cancelPrevious;
let cancellation = () => new Promise((resolve, reject) => cancelPrevious = reject)
return function (...args) {
if (cancelPrevious != null) cancelPrevious()
return Promise.race([cancellation(), delay(ms)])
.then(() => action.apply(this, args))
View composable-type-guards.ts
type Checker<T> = (x: any) => x is T;
function tArray<T>(f: Checker<T>) {
return function (a:any): a is Array<T> {
if (!Array.isArray(a)) return false;
for (var k = 0; k < a.length; ++k)
if (!f(a[k])) return false;
return true;
}
}
View dbus-run.sh
#!/bin/sh
export "$(grep -az '^DBUS_SESSION_BUS_ADDRESS=' /proc/`ps -e | grep 'i3$' | awk '{print $1}'`/environ)"
/bin/bash -c "$*"
exit 0
View modules.md

Why .mjs

Alternative 1

As far as I can tell from the latest update, the ability to import ES6 import from CommonJS modules is off the table. The main argument for this was to enable existing modules to upgrade to ES6 without causing any breakage to their CommonJS dependants.

This is a big deal. This means that CommonJS libraries that want to upgrade to ES6 modules will HAVE to bump semver-major version to

View translate-error.js
function translateError(msg) {
var newErr = new Error(msg); // placed here to get correct stack
return e => {
newErr.originalError = e;
throw newErr;
}
}
async function asyncTask() {
const user = await UserModel.findById(1).catch(translateError('No user found'))