Skip to content

Instantly share code, notes, and snippets.

@colelawrence
colelawrence / yjs-shared-types.ts
Created September 18, 2023 05:44
YJs, TypeScript, "Shared Types" with zod-like parser inference
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
import * as Y from "yjs";
export interface ZodLikeParser<T> {
_type: T;
parse: (value: unknown) => T;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type AnyValueDesign = ZodLikeParser<any> | CollectionDef<any> | RecordDef<any>;
@colelawrence
colelawrence / collectAndFlush.ts
Created April 4, 2024 19:50
Add items with a throttled effect of applying the items.
/**
* Add items with a throttled effect of applying the items.
* We might use this to collect items and then apply them in batches like for
* search results and timeline items.
*/
export function collectAndFlush<T>(
then: (items: T[]) => void,
// 60 fps
throttleMs: number = 17,
): {
@colelawrence
colelawrence / createTrpcDurableObject.ts
Last active March 27, 2024 22:10
Create strongly typed Durable Objects with TRPC wrappers (requires trpc@10)
import { createTRPCProxyClient, httpLink } from "@trpc/client";
import { AnyRouter, initTRPC, MaybePromise, Router } from "@trpc/server";
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
import type { ParserWithInputOutput, ParserWithoutInput } from "@trpc/server/dist/core/parser";
import type { AnyRouterDef } from "@trpc/server/dist/core/router";
import type { ResponseMetaFn } from "@trpc/server/dist/http/internals/types";
import { getParseFn } from "./getParseFn";
export type TrpcDurableObjectRouter<Router extends AnyRouter> = {
router: Router,
@colelawrence
colelawrence / fetchProxyProofOfConcept.ts
Created March 25, 2024 14:47
Serializing Requests into a Response
/**
* As a proof of concept,
* JSON'infy the args, and parse them into a new Request Init,
* Make the fetch request with the new Request Init
* JSONify the response and use the jsonified response data
* to recreate the response object and return that
*
*/
const fetchProxyProofOfConcept: typeof fetch = async (...args) => {
import type { Component, DevJSX, JSXNode, JSXOutput } from "@builder.io/qwik";
import { useSignal, useVisibleTask$ } from "@builder.io/qwik";
const QWIK_DATA_ATTR = "data-qwik-inspector";
interface HackedProps {
"data-qwik-inspector"?: DevJSX;
"p:where"?: DevJSX;
}
function createLinkFromDevJSX(dev: DevJSX) {
@colelawrence
colelawrence / warnOnce.ts
Created February 27, 2024 02:46
Warn console messages only once.
const warned = new Set<string>();
export function warnOnce(message: string) {
if (!warned.has(message)) {
warned.add(message);
console.warn(message);
}
}
@colelawrence
colelawrence / Prettify.d.ts
Created February 24, 2024 01:25
Improve preview of union types in autocomplete
/** Improve preview of union types in autocomplete. */
export type Prettify<T> = { [K in keyof T]: T[K] } & {};
@colelawrence
colelawrence / obfuscate.js
Created February 21, 2024 20:32
Obfuscate secure tokens retaining the shape and scale (Raycast)
#!/usr/bin/env node
// Required parameters:
// @raycast.schemaVersion 1
// @raycast.title Obfuscate
// @raycast.mode compact
// Optional parameters:
// @raycast.icon 🤖
// @raycast.argument1 { "type": "text", "placeholder": "Original" }
@colelawrence
colelawrence / DisposePool.ts
Created December 19, 2023 20:53
Dispose Pool (Subscription) class
export interface IDisposable {
dispose(): void;
}
export class DisposePool implements IDisposable {
#fns: null | (() => void)[] = [];
#pool: null | IDisposable[] = [];
get disposed() {
return this.#pool === null;
}
const MINUTE = 1000 * 60;
function asyncCachedFn<Args extends any[], Result>(options: {
fn: (...args: Args) => Promise<Result>;
key: (...args: Args) => string;
}) {
const cache: Map<
string,
{ computedAt: number; evict: any; result: Promise<Result> }