Skip to content

Instantly share code, notes, and snippets.

@colelawrence
colelawrence / mise.toml
Created August 1, 2025 00:28
Jaeger v2 OTEL Collector in mise.toml
[tools."http:jaeger"]
# single authoritative version for every OS/arch
version = "2.8.0"
[tools."http:jaeger".platforms]
macos-x64 = { url = "https://download.jaegertracing.io/v1.71.0/jaeger-2.8.0-darwin-amd64.tar.gz" }
macos-arm64 = { url = "https://download.jaegertracing.io/v1.71.0/jaeger-2.8.0-darwin-arm64.tar.gz" }
linux-x64 = { url = "https://download.jaegertracing.io/v1.71.0/jaeger-2.8.0-linux-amd64.tar.gz" }
[tasks.start-jaeger]
@colelawrence
colelawrence / effect-rpc-http-example.ts
Last active July 31, 2025 12:36
Effect RPC (RpcServer.layerHttpRouter), HttpApi, HttpLayerRouter example with toWebHandler (with Bun.serve)
// ============================================================================
// PACKAGE VERSIONS (2025-07-30)
// ============================================================================
//
// Dependencies:
// - effect: 3.17.3
// - @effect/platform: 0.90.0
// - @effect/rpc: 0.68.0
//
// ============================================================================
@colelawrence
colelawrence / use-rpc-hooks.ts
Created June 2, 2025 10:50
Effect RPC + TanStack Query with good types. Customize for your use case! Let me know if you fix failure extraction!
import { FetchHttpClient, HttpClient, HttpClientRequest } from "@effect/platform";
import { RpcResolver, type RpcRouter } from "@effect/rpc";
import { HttpRpcResolver } from "@effect/rpc-http";
import type { AppRouter, JWTAccessToken, OrgID } from "@phosphor/server";
import { type UseMutationOptions, type UseQueryOptions, useMutation, useQuery } from "@tanstack/react-query";
import { Effect, flow, identity, pipe } from "effect";
import { Option } from "effect";
import type * as EffectRequest from "effect/Request";
import type { FiberFailure } from "effect/Runtime";
import React from "react";
/**
* FNV-1a 32-bit hash – fast, non-cryptographic, browser-safe.
* Sufficient for generating repeatable fragment keys.
*/
export const fnv1aHash32 = (str: string): string => {
let h = 0x811c9dc5; // FNV offset basis
for (let i = 0; i < str.length; i++) {
h ^= str.charCodeAt(i);
h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24);
}
export const OrgResource = RBAC.defineResource({
name: dev`Organization`,
idSchema: OrgID,
permissions: {
CreateWorkspace: (its) => its.Member,
},
roles: {
Admin: true,
Member: true,
Guest: true,
@colelawrence
colelawrence / loop-protection-counter.ts
Last active March 5, 2025 14:13
Loop protection counter in TypeScript
import { DevError, debounce } from "@project/prelude";
export class LoopError extends DevError {
constructor(
message: string,
public readonly debounceMs: number,
public readonly maxCount: number,
) {
super(`${message} (exceeded ${maxCount} calls in ${(debounceMs * 0.001).toFixed(1)}s)`);
}
export class MapOrSetDefault<K, T> extends Map<K, T> {
#setDefault(key: K): T {
const val = this.getDefault(key);
this.set(key, val);
return val;
}
constructor(public getDefault: (key: K) => T) {
super();
}
@colelawrence
colelawrence / downloadFileBinary.ts
Created February 10, 2025 22:11
Download a Uint8Array to file
export function downloadFileBinary(filename: string, data: Uint8Array, mimeType = "application/octet-stream") {
const blob = new Blob([data], { type: mimeType });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
a.click();
URL.revokeObjectURL(url);
}
@colelawrence
colelawrence / useClickByHoverToAvoidChangingFocus.tsx
Last active February 4, 2025 20:39
React helper for when you want to enable "clicking" a button without changing the document active element/focus
import { useEffect } from "react";
import { listen } from "./zlisten";
/** For when you want to do something without breaking the focus of the active element */
export const useClickByHoverToAvoidChangingFocus = (element: HTMLElement | null | undefined, callback: null | undefined | (() => void)) => {
useEffect(() => {
if (!element || !callback) return;
let openTimeout: (Animation | any)[] = [];
const DURATION = 1000;
@colelawrence
colelawrence / pipeNonNull.ts
Created January 18, 2025 20:26
Like a pipe function, but early return if any function returns a null-ish value
type Fn<T, U> = (t: T) => U | null | undefined;
/** like pipe, but returns early if any of the functions return a nullish value */
export function pipeNonNull<A, B>(a: A, b: Fn<A, B>): B | null | undefined;
export function pipeNonNull<A, B, C>(a: A, b: Fn<A, B>, c: Fn<B, C>): C | null | undefined;
export function pipeNonNull<A, B, C, D>(a: A, b: Fn<A, B>, c: Fn<B, C>, d: Fn<C, D>): D | null | undefined;
export function pipeNonNull<A, B, C, D, E>(a: A, b: Fn<A, B>, c: Fn<B, C>, d: Fn<C, D>, e: Fn<D, E>): E | null | undefined;
export function pipeNonNull<A, B, C, D, E, F>(
a: A,
b: Fn<A, B>,
c: Fn<B, C>,