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'; | |
// see https://github.com/Microsoft/TypeScript/pull/13743 for what the typing in here's all about | |
type Constructor<T> = new (...args: any[]) => T; | |
const isMounted_Symbol = Symbol('isMounted'); | |
/** | |
* This is for when you're calling setState in uncancellable async callbacks. | |
* NOTE: Doing this can mask memory leaks so be careful |
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 makeAbortablePromise(promise, abort = () => {}) { | |
const promiseWithAbort = { | |
abort, | |
promise, | |
// proxy methods | |
then: (...args) => { | |
promiseWithAbort.promise = promiseWithAbort.promise.then.apply( | |
promiseWithAbort.promise, | |
args | |
); |
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 iterate(iterator, cb) { | |
let next = iterator.next(); | |
while(!next.done) { | |
cb(next.value); | |
next = iterator.next(); | |
} | |
} | |
// iterate(new Set([1,2,3]).values(), console.log) |
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 _ from 'lodash'; | |
let n = 2; | |
let someString; | |
// NOTE `_.pickBy(params, _.negate(_.isNil))` removes undefined/null entries | |
// so you don't have to worry about `undefined` getting coerced to a string | |
const formatUrl = (urlStr, params) => urlStr + '?' + | |
new URLSearchParams(_.pickBy(params, _.negate(_.isNil))).toString(); |
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 objToParams(obj) { | |
const params = new URLSearchParams(); | |
for(let key in obj) { | |
if(Array.isArray(obj[key])) { | |
obj[key].forEach(item => params.append(key, item)); | |
} | |
else if(typeof obj[key] === 'object') { | |
continue; | |
} | |
else { |
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
// create an LRU (least recently used) cache with up to 10 entries | |
const lru = $cacheFactory('my-cache', { capacity: 10 }); | |
$http.get('http://pokeapi.co/api/v2/pokemon/1/', { | |
cache: lru | |
}); |
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
// this will cache the response indefinitely in a cache created via $cacheFactory | |
// that cache is shared globally among all $http requests | |
$http.get('http://pokeapi.co/api/v2/pokemon/1/', { | |
cache: true | |
}); |
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'; | |
const enum Actions { | |
get, | |
set, | |
delete | |
} | |
export class Store extends EventEmitter { | |
private data = new Map<string, any>(); |
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 randomErrorObs() { | |
return Rx.Observable.create(obs => { | |
let n = 0; | |
if(Math.random() < 0.5) { | |
obs.next(++n); | |
} else { | |
console.warn('producing error'); | |
obs.error('bummer'); | |
} | |
}); |
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
// This class implements the same interface that the cache created by $cacheFactory does | |
// See https://github.com/angular/angular.js/blob/master/src/ng/cacheFactory.js#L142 | |
// This cache evicts an entry once it's expired (which we define as 5 seconds). | |
class ExpirationCache { | |
constructor(timeout = 5000) { | |
this.store = new Map(); | |
this.timeout = timeout; | |
} | |
get(key) { |