Skip to content

Instantly share code, notes, and snippets.

View EmNudge's full-sized avatar
💭
${status}

EmNudge EmNudge

💭
${status}
View GitHub Profile
@EmNudge
EmNudge / getCachedWork.ts
Created August 4, 2024 01:37
get cacjed
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)
@EmNudge
EmNudge / state.ts
Created May 26, 2024 22:38
React Global State Demo
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);
@EmNudge
EmNudge / server.js
Last active February 22, 2025 14:04
Minimal Live Reload
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);
};
@EmNudge
EmNudge / ooos.mjs
Created April 20, 2024 20:41
Out Of Order Streaming in Node.js
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[&quot;${id}&quot;].replaceWith(this.previousElementSibling), this.remove()"
></script>`;
@EmNudge
EmNudge / temp-dir.zsh
Last active April 6, 2024 01:14
Make temp directory and cd into it
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-"
@EmNudge
EmNudge / toPrimitive.js
Created January 26, 2024 19:29
ToPrimitive
// 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;
@EmNudge
EmNudge / parenToIndented.js
Created August 30, 2023 22:25
Turn an incremental AST into an indented tree
/** @param {string} text */
const parenToIndented = (text) => {
let num = 0;
return text.replace(/[,)(]/g, ([m]) => {
if (m === ")") {
num--;
return "";
}
if (m === "(") num++;
// 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');
@EmNudge
EmNudge / mini-router.ts
Created July 2, 2022 21:47
Mini router with no middleware support
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;
@EmNudge
EmNudge / stagger.css
Created May 6, 2022 14:26
stagger items in CSS
/* 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;