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 const ONE_SECOND = 1000; | |
export const ONE_MINUTE = ONE_SECOND * 60; | |
/** | |
* keeps previous work until duration | |
* @example | |
* const getFile = getCachedWork(async () => { | |
* const file = await readFile() | |
* return await transform(file) | |
* }, ONE_MINUTE) |
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 { useState } from 'react'; | |
type GlobalState<T> = { value: T }; | |
const setStateMap = new Map<GlobalState<any>, Set<Function>>(); | |
export function useGlobalState<T>(stateObj: GlobalState<T>) { | |
const setStateFuncs = setStateMap.get(stateObj) ?? new Set(); | |
setStateMap.set(stateObj, setStateFuncs); |
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
const listeners = new Set(); | |
const addSubscriber = (res) => { | |
res.writeHead(200, { | |
"Content-Type": "text/event-stream", | |
Connection: "keep-alive", | |
"Cache-Control": "no-cache", | |
}); | |
listeners.add(res); | |
}; |
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 { createServer } from "node:http"; | |
const sleep = (ms) => new Promise((res) => setTimeout(res, ms)); | |
const suspend = (placeholder, promise) => { | |
const id = "stream-" + Math.random().toString(32).slice(2); | |
const inserterTag = `<script | |
src="data:text/javascript," | |
onload="window["${id}"].replaceWith(this.previousElementSibling), this.remove()" | |
></script>`; |
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
function tmp() { | |
local rand_name="r$(head /dev/urandom | LC_ALL=C tr -dc a-z0-9 | head -c 6)" | |
local temp_folder="/tmp/trash-dirs/${1:+$1-}$rand_name" | |
mkdir -p "$temp_folder" && cd "$temp_folder" || return 1 | |
} | |
# usage: | |
tmp # make a random directory in /tmp/trash-dirs/ and cd into it | |
tmp my-project # prefix dir with "my-project-" |
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
// https://www.ecma-international.org/ecma-262/11.0/index.html#sec-toprimitive | |
const isCallable = (obj, prop) => typeof obj[prop] === 'function'; | |
const isPrimitive = (val) => | |
val === null || !['object', 'function'].includes(typeof val); | |
// return primitive or error | |
function toPrimitive(obj) { | |
if (isPrimitive(obj)) return obj; |
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
/** @param {string} text */ | |
const parenToIndented = (text) => { | |
let num = 0; | |
return text.replace(/[,)(]/g, ([m]) => { | |
if (m === ")") { | |
num--; | |
return ""; | |
} | |
if (m === "(") num++; |
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
// for use in quickly testing out webcam on browser | |
document.body.style.margin = '0'; | |
const constraints = { | |
audio: false, | |
video: { width: 1280, height: 720 } | |
}; | |
const videoEl = document.createElement('video'); |
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 { Env } from './index'; | |
type RouteFunc = (request: Request, env: Env, groups: Record<string, string>) => Promise<Response>; | |
type RouteGuard = { pattern: URLPattern, func: RouteFunc }; | |
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS'; | |
export class Router { | |
guards: Map<HttpMethod, RouteGuard[]> = new Map(); | |
enabledCors = 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
/* container data, change as needed */ | |
.staggered { | |
--max-depth: 20; | |
--max-delay: 500ms; | |
} | |
/* requires an --index variable on each */ | |
.staggered > * { | |
animation: slide-in .5s forwards; | |
display: block; |
NewerOlder