Skip to content

Instantly share code, notes, and snippets.

💭
Copying and pasting from StackOverflow

Cameron Nokes ccnokes

💭
Copying and pasting from StackOverflow
Block or report user

Report or block ccnokes

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@ccnokes
ccnokes / vanilla-online-offline.js
Last active Feb 20, 2017
Online/offline emitter in vanilla JS
View vanilla-online-offline.js
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));
};
@ccnokes
ccnokes / sum-objs.js
Created Feb 22, 2017
Function that sums objects of the same shape
View sum-objs.js
// 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
@ccnokes
ccnokes / pipe.js
Last active May 21, 2017
Pipe function, taken from twitter
View pipe.js
// 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
@ccnokes
ccnokes / detect-reflow.js
Created May 27, 2017
script for detecting DOM method calls that are know to cause a reflow
View detect-reflow.js
// 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',
@ccnokes
ccnokes / waitAtLeast.js
Created May 31, 2017
Make an async function take at least X amount of time
View waitAtLeast.js
/**
* 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;
@ccnokes
ccnokes / yes.js
Last active Jun 30, 2017
unix yes command in node.js
View yes.js
// 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);
@ccnokes
ccnokes / retry-example.js
Created Aug 11, 2017
Rx.Observable.retry example
View retry-example.js
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');
}
});
@ccnokes
ccnokes / SimpleStore.ts
Created Aug 16, 2017
A simple store thing
View SimpleStore.ts
import { EventEmitter } from 'events';
const enum Actions {
get,
set,
delete
}
export class Store extends EventEmitter {
private data = new Map<string, any>();
@ccnokes
ccnokes / basic-$http-cache.js
Created Aug 21, 2017
AngularJS $http cache
View basic-$http-cache.js
// 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
});
@ccnokes
ccnokes / custom-cache-ng1.js
Last active Aug 21, 2017
Use a custom cache class in angularJS $http
View custom-cache-ng1.js
// 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) {
You can’t perform that action at this time.