This file contains hidden or 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
// @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 { |
This file contains hidden or 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
// 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 |
This file contains hidden or 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
// 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 |
This file contains hidden or 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 axios from 'axios' | |
// Extend me! | |
export default class ApiClient { | |
constructor(endpoint, token){ | |
this.endpoint = endpoint | |
this.token = token | |
} | |
async post(path, data){ |
This file contains hidden or 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
# (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: |
This file contains hidden or 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
// Returns promise that resolves in ms milliseconds | |
export async function sleep(ms) { | |
return new Promise(resolve => { | |
setTimeout(resolve, ms) | |
}) | |
} |
This file contains hidden or 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
// 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 |
This file contains hidden or 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
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; | |
} |
This file contains hidden or 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
class Pipe { | |
constructor(){ | |
this.onChangeHandler = null | |
this.cache = [] | |
} | |
onChange(fn){ | |
this.onChangeHandler = fn | |
this.flush() | |
} |
This file contains hidden or 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
// 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 () => { |
NewerOlder