Skip to content

Instantly share code, notes, and snippets.

View ccnokes's full-sized avatar

Cameron Nokes ccnokes

View GitHub Profile
@ccnokes
ccnokes / iterate-iterator.js
Last active January 17, 2018 03:57
basis of a functional way to traverse iterators
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)
@ccnokes
ccnokes / promise-with-abort.js
Created February 13, 2018 23:43
Promise wrapper with abort function
function makeAbortablePromise(promise, abort = () => {}) {
const promiseWithAbort = {
abort,
promise,
// proxy methods
then: (...args) => {
promiseWithAbort.promise = promiseWithAbort.promise.then.apply(
promiseWithAbort.promise,
args
);
@ccnokes
ccnokes / SafeSetState.ts
Last active January 3, 2019 21:44
mixin class that extends your component and makes `setState` "safe". See https://codesandbox.io/s/6438ymvk8z
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
@ccnokes
ccnokes / modulo-use-cases.js
Last active March 2, 2018 17:55
Modulo use cases because math is hard
// find if number is even or odd
const isEven = val => val % 2 === 0;
isEven(11); //false
isEven(22); //true
// do time related things...
const formatMovieTime = val => {
const hours = Math.floor(val / 60); //get the hours, discard any remainder via `floor`
const mins = val % 60; //get the remainder of minutes left over as an integer
return `${hours}:${mins < 10 ? '0' + mins : mins}`;
@ccnokes
ccnokes / ts-npm-package-sample.json
Created March 8, 2018 03:48
Sample package.json for a TS lib that's published to NPM
{
"name": "my-ts-lib",
"version": "1.0.0",
"description": "My npm package written in TS",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc"
},
"author": "Cameron Nokes",
@ccnokes
ccnokes / tsconfig-for-npm-sample.json
Created March 8, 2018 03:50
Sample, minimal tsconfig.json for NPM package
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["es2017", "es7", "es6", "dom"],
"declaration": true,
"outDir": "dist",
"strict": true,
"esModuleInterop": true
},
@ccnokes
ccnokes / async-serial-queue.ts
Last active March 23, 2018 19:12
Queues async actions so they occur one after another
// class version
class Queue {
private p: Promise<any> = Promise.resolve();
push<T = any>(fn: () => Promise<any>): Promise<T> {
this.p = this.p
.then(() => fn())
.catch(err => {
console.error(err);
return fn(); //keep going to next queued
@ccnokes
ccnokes / Deferred.ts
Last active March 23, 2018 19:28
AngularJS $q style deferred from a native Promise, in TypeScript
class Deferred<T = any> {
resolve: (value?: T | PromiseLike<T>) => void;
reject: (reason?: any) => void;
promise = new Promise<T>((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
then = this.promise.then.bind(this.promise);
@ccnokes
ccnokes / fifo_test.sh
Created June 2, 2018 04:52
Bash fifos
mkfifo fifo
# produce data and send into fifo
# have to background it because it blocks until the fifo drains/completes
ls -l > fifo &
cat fifo # this drains it and completes the job
@ccnokes
ccnokes / batch-async-tasks.js
Last active January 15, 2019 09:51
Chunk async tasks into batches and process in parallel
// dummy async task
function asyncTask(n) {
return new Promise(resolve => setTimeout(() => resolve(n), 500));
}
// takes a flat array and returns a nested one, eg. chunkArray([1,2,3], 1) ==> [[1],[2],[3]]
function chunkArray(arr, chunkSize) {
return arr.reduce((aggr, item) => {
let lastArr = aggr[aggr.length - 1];
if (lastArr.length < chunkSize) {