This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function wait(ms, opts = {}) { | |
return new Promise((resolve, reject) => { | |
let timerId = setTimeout(resolve, ms); | |
if (opts.signal) { | |
// implement aborting logic for our async operation | |
opts.signal.addEventListener('abort', event => { | |
clearTimeout(timerId); | |
reject(event); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { EventEmitter } from 'events'; | |
class AbortSignal { | |
private events = new EventEmitter(); | |
constructor( | |
private getIsAborted: () => boolean | |
) {} | |
get aborted(): boolean { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let map = new WeakMap(); // weakly holds all object refs (works in IE11+) | |
let n = 0; // global counter for ids | |
export function idObj(obj: any) { | |
if (map.has(obj)) { | |
return map.get(obj); | |
} else { | |
let key = String(++n); | |
map.set(obj, key); | |
return key; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A function for batching RAFs together | |
*/ | |
export default function RAFScheduler() { | |
let queue = []; | |
let rafId; | |
let scheduled = false; | |
const DURATION = 10; | |
return function scheduleRaf(cb: () => void) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
stash_count=$(git stash list | wc -l) | |
i=0 | |
while [[ $i -lt $stash_count ]]; do | |
git stash show -p stash@{$i} > "stash-$i.patch" | |
i=$(( $i + 1 )) | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Deferred<Type> { | |
promise: Promise<Type>; | |
// TODO type these | |
resolve: any; | |
reject: any; | |
then: any; | |
catch: any; | |
constructor() { | |
this.promise = new Promise((resolve, reject) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SparseList<T = any> { | |
private list: T[]; | |
emptySlotsCount: number; | |
constructor(size: number) { | |
this.list = new Array(size); | |
this.emptySlotsCount = size; | |
} | |
get size() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RICScheduler { | |
private queue: { task: () => void, id: number }[] = []; | |
private running = false; | |
private nextId = 0; | |
private ricId; | |
private id() { | |
return this.nextId++; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react'; | |
import hoistStatics from 'hoist-non-react-statics'; | |
declare var angular: any; | |
let injector; | |
// NOTE you probably can't run this at the top level of a module because angular takes time to load and bootstrap | |
export default function getNgService<T = any>(serviceName: string): T { | |
if (!injector) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react'; | |
const defaultMapper = (ctx) => ctx | |
export function initContext<CtxType>() { | |
const Context = React.createContext<CtxType | null>(null); | |
function mapContextToProps<Props>(Component: React.ComponentType<Props & CtxType>, mapper: (ctx: CtxType) => any = defaultMapper): React.ComponentType<Props> { | |
return (props: Props) => ( | |
<Context.Consumer> | |
{(ctx) => <Component {...props} {...mapper(ctx)} />} |