Skip to content

Instantly share code, notes, and snippets.

😴
Help me get productive, thanks.

intrnl

😴
Help me get productive, thanks.
  • Indonesia
Block or report user

Report or block intrnl

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
@intrnl
intrnl / queue.js
Last active Dec 15, 2019
Functional Promise queue
View queue.js
class QueueError extends Error {}
/**
* Creates a Promise queue
* @param {object} [options]
* @param {number} [options.concurrency=4] How many tasks to run at once
* @param {number} [options.concurrencyTimeout=0] How long to wait before starting another task
* @param {number} [options.size=0] Maximum pending tasks to queue,
* @param {number} [options.retries=3] Maximum retries if task fails
* @param {boolean} [options.retryImmediate=true] Whether to immediately retry the failed task
@intrnl
intrnl / lock.js
Last active Dec 15, 2019
Functional locking mechanism
View lock.js
class LockError extends Error {}
function createLock () {
/** @type {boolean} */
let locked = false;
/** @type {PromiseObject[]} */
let pending = [];
function acquire () {
View deepClone.js
/**
* Deep clones an object or array
* @param {object|array} obj Object or array to clone
* @returns {object|array} Cloned object or array
*/
function deepClone (obj) {
if (typeof obj !== 'object' || obj === null) return {};
const res = Array.isArray(obj) ? [] : {};
const queue = [{ base: res, current: obj }];
View resolveDeps.js
function resolveDeps (pkgs, name) {
const resolved = [];
const missingRequired = [];
const missingOptional = [];
const queue = [name];
while (queue.length) {
const curr = queue.shift();
const pkg = pkgs[curr];
View findInTree.js
function isValidTree (obj) {
return obj !== null && (Array.isArray(obj) || typeof obj === 'object');
}
/**
* Traverses through a tree based on a filter
* @param {object|array} tree
* @param {string|findInTreeCB} filter
* @param {object} [opts]
* @param {string[]|null} [opts.walkable=null] Specific keys to walk on
View usefetch.js
import { useState, useEffect } from 'preact/hooks'
/**
* @param {RequestInfo} url
* @param {RequestInit} opts
* @returns {useFetchObject}
*/
function useFetch (url, opts) {
const [response, setResponse] = useState(undefined)
const [error, setError] = useState(undefined)
View useawaitinterval.js
import { useEffect, useRef } from 'preact/hooks'
/**
* @param {function} callback
* @param {number} delay
*/
function useAwaitInterval (callback, delay) {
const cb = useRef()
useEffect(() => {
View classnames.js
function classnames (...classes) {
const arr = []
for (const name of classes) {
if (typeof name === 'string') {
arr.push(name)
}
else if (typeof name === 'object' && name !== null) {
for (let [key, value] of Object.entries(name)) {
if (value) arr.push(key)
View stripBOM.js
function stripBOM (content) {
return content.charCodeAt(0) === 0xFEFF ? content.slice(1) : content
}
View sleep.js
function sleep (ms) {
return new Promise((res) => setTimeout(res, ms))
}
You can’t perform that action at this time.