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
const axios = require('axios'); | |
const http = require('http'); | |
const https = require('https'); | |
module.exports = axios.create({ | |
//60 sec timeout | |
timeout: 60000, | |
//keepAlive pools and reuses TCP connections, so it's faster | |
httpAgent: new http.Agent({ keepAlive: 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
// throughput is usually ~350-400 MiB/s | |
// run: node yes.js | pv > /dev/null | |
const buf = Buffer.alloc(4096, 'y\n', 'utf8'); | |
const str = buf.toString(); | |
const { Readable } = require('stream'); | |
class Y extends Readable { | |
_read() { | |
this.push(str); |
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
/** | |
* make an async function take at least X amount of time | |
* @param limit - in ms | |
* @param fn - returns promise | |
* @returns {Promise.<T>} | |
*/ | |
function waitAtLeast(limit, fn) { | |
let start = Date.now(); | |
let end = start + limit; |
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
// These methods and getter/setters force layout/reflow in Chrome/WebKit | |
// From https://gist.github.com/paulirish/5d52fb081b3570c81e3a | |
const getterSetters = [ | |
'offsetLeft', | |
'offsetTop', | |
'offsetWidth', | |
'offsetHeight', | |
'offsetParent', | |
'clientLeft', |
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
//wrap a fn that returns a function into a decorator | |
function makeFnWrapDecorator(fnWrapper: Function) { | |
return function decoratorWrapper(...args) { | |
return function decorator(target, propertyKey: string, descriptor: PropertyDescriptor) { | |
const fn = descriptor.value; | |
let wrappedFn = fnWrapper.apply(null, [fn, ...args]); | |
return { | |
configurable: true, | |
get() { | |
return wrappedFn; |
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 makeLazy(name, fn, obj = {}) { | |
const nullSym = Symbol('nil'); //could change this to be more es5 friendly | |
let val = nullSym; //I'm not sure if holding the value in this closure or right on the object is better | |
Object.defineProperty(obj, name, { | |
enumerable: true, | |
get() { | |
if(val === nullSym) { | |
val = fn(); |
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
// sync version | |
const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x); | |
// example | |
const newFunc = pipe(fn1, fn2, fn3); | |
const result = newFunc(arg); | |
// async version | |
// take a series of promise producing functions and return a single promise |
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
// objs[] *must* be the same shape | |
// returns new object with summation of all properties | |
function sumObjs(objs) { | |
const keys = Object.keys(objs[0]); | |
// initialize return object with 0s | |
const ret = keys.reduce((aggr, k) => { | |
aggr[k] = 0; | |
return aggr; | |
}, {}); | |
// sum each property |
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
const { Observable } = require('rxjs/Observable'); | |
require('rxjs/add/operator/filter'); | |
require('rxjs/add/operator/switchMap'); | |
require('rxjs/add/operator/take'); | |
require('rxjs/add/operator/toPromise'); | |
const axios = require('axios'); | |
const online$ = createOnline$(); | |
//only make the network request when we're online |
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 createOnlineEmitter() { | |
let cbs = []; //array of registered callbacks for the event | |
let unsub; //function for removing the main event listener | |
//this is the main event listener that gets registered with window.online/offline event | |
const mainListener = (isOnline) => { | |
//call all the subscribed callbacks | |
cbs.forEach(cb => cb(isOnline)); | |
}; |