Skip to content

Instantly share code, notes, and snippets.

View miketromba's full-sized avatar
🎯
Focusing

Michael Tromba miketromba

🎯
Focusing
View GitHub Profile
@miketromba
miketromba / FifoOperationQueueCache.js
Created February 7, 2022 17:10
FifoOperationQueueCache.js
// @ts-nocheck
function createDeferredPromise(){
let res, rej
const promise = new Promise((resolve, reject) => { [res, rej] = [resolve, reject] })
promise.resolve = res
promise.reject = rej
return promise
}
class FifoOperationQueue {
@miketromba
miketromba / ItemCache.js
Last active January 10, 2022 16:51
ItemCache.js
// Very simple item cache (easily add .flush/dump and/or .delete functions if needed)
class ItemCache {
constructor({ get, init }){
this._items = {}
this.getItem = get
// use init to initialize the cache: init(set){ getItems, then set(key,
// item) for each }
this._init = init
this._initialized = false
@miketromba
miketromba / PaginationTraverser.js
Last active January 10, 2022 16:43
PaginationTraverser.js
// Get all records from { page, limit } style pagination
// NOTE: this class assumes that the first page value is 1 (NOT 0)
// Tweak the class if your system begins pagination with page 0.
class PaginationTraverser {
constructor({ limit, getPage }){
// Limit must not exceed server's clamp range or this breaks
// very badly (infinite while loop)
this.limit = limit
this.getPage = getPage
@miketromba
miketromba / ApiClient.js
Created September 29, 2021 21:36
ApiClient.js
import axios from 'axios'
// Extend me!
export default class ApiClient {
constructor(endpoint, token){
this.endpoint = endpoint
this.token = token
}
async post(path, data){
@miketromba
miketromba / install_node_and_docker.sh
Last active May 10, 2021 18:21
install node and docker
# (use docker droplet)
# Install node and npm:
sudo apt update
sudo apt install nodejs
sudo apt install npm
# Now, docker + node + npm should be ready.
# Yarn, optional:
@miketromba
miketromba / sleep.js
Created March 31, 2021 20:16
sleep.js
// Returns promise that resolves in ms milliseconds
export async function sleep(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms)
})
}
@miketromba
miketromba / RecurringTask.js
Created March 31, 2021 20:15
RecurringTask.js
// Utility for running recurring tasks
// Uses setInterval and setTimeout
// Has ability to schedule the first run instantly or wait for a fixed MS
export default class RecurringTask {
constructor({
task, // can be async
intervalMs = 1000, // interval in milliseconds
lastRun = 0, // 0 == run immediately
@miketromba
miketromba / chunkArray.js
Last active March 12, 2021 13:22
chunkArray.js
export function chunkArray(array, size) {
const chunked_arr = [];
let index = 0;
while (index < array.length) {
chunked_arr.push(array.slice(index, size + index));
index += size;
}
return chunked_arr;
}
@miketromba
miketromba / Pipe.js
Created January 26, 2021 21:27
Pipe data from one fn call to another with delivery guarentee
class Pipe {
constructor(){
this.onChangeHandler = null
this.cache = []
}
onChange(fn){
this.onChangeHandler = fn
this.flush()
}
@miketromba
miketromba / PollingPromise.js
Last active January 16, 2021 17:10
Get a promise which resolves once a given function returns true. The promise will set an interval and poll the function at a specified interval until it becomes true.
// Resolves once the check function returns true
// You optionally specify the interval at which to poll the check function. Default = 1000ms
// TODO: add timeout functionality
class PollingPromise extends Promise {
constructor(check, interval = 1000){
super(resolve => {
if(this.check()){ return resolve() }
const interval = setInterval(async () => {