Skip to content

Instantly share code, notes, and snippets.

import {
Object3D, PerspectiveCamera, Scene,
WebGLRenderer
} from "three";
import { CleanupFn } from "./CleanupFn";
import { DisposePool, isDisposable } from "./DisposePool";
import { merge } from "./merge";
import { ISheet } from "@theatre/core";
import { TheatreMergeable, theatreMerge } from "./theatreMerge";
import { EventsPool } from "./EventsPool";
@colelawrence
colelawrence / dev-stringify.ts
Last active January 11, 2024 21:21
Tight JSON and JavaScript stringify
function jsReplacer() {
const seen = new WeakSet();
return (key: string, value: any) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) return "[Circular]";
seen.add(value);
}
if (value instanceof RegExp) return { regexp: value.toString() };
if (value instanceof Function) return { function: value.toString() };
if (value instanceof Error) return { error: value.toString() };
@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 / format-slint-deno.ts
Last active April 26, 2023 12:06
Format slint files using Deno
const ignore: string[] = [
// "ui/generated.slint"
];
const dirs = { ui: "ui" };
const whitespaceRE = /^(\s*)(.*?)(\/*|\/\/)?(.*)$/;
const indentBy = " ";
file: for await (const file of Deno.readDir(dirs.ui)) {
if (
@colelawrence
colelawrence / prosemirror-buildTypedNodeSpec.ts
Last active January 7, 2023 06:08
ProseMirror typed node spec (incomplete source code from Story.ai codebase)
import {
AttributeSpec,
DOMOutputSpec,
Fragment,
Mark,
Node as PMNode,
NodeSpec,
NodeType,
ParseRule,
} from "prosemirror-model";

"Untrusted code"

Worst possible approach:

  • Run plugins all in the same vm context on server side Bad approach:
  • Run plugins in the same vm context on browser side

Ish:

  • Run plugins in browser side web workers
/**
* Generate a fragile object that will throw error at any operation.
*/
export function createErrorObj<T = any>(error: string): T {
return new Proxy(
{},
{
get(target, prop, receiver: unknown) {
throw new Error(
@colelawrence
colelawrence / windows.ahk
Last active October 14, 2022 16:17
Making crucial keybindings similar on Windows to expected on personal macOS
#SingleInstance Force
; Debugging
; https://www.autohotkey.com/scite4ahk/pages/debugger.htm
; Map Capslock to control (macOS is Capslock as Command, so most keybindings feel similar)
CapsLock::RControl
; Left Ctrl Tab -> Left Alt Tab
<^Tab::>!Tab
@colelawrence
colelawrence / WeakCache.ts
Created September 6, 2022 12:48
When you needed something a little higher dimension than just a WeakMap.
/** Memory-volatile multi-key cache with a root weak key */
export class WeakCache {
private wm = new WeakMap<object, Map<string | number, unknown>>();
/** It's very important to not call this with the same keys and expect a different result. */
getOrPut<R>(weakKey: object, keys: CacheKeys, fn: () => R): R {
let found = this.wm.get(weakKey);
const innerKey = typeof keys === "object" ? JSON.stringify(keys) : keys;
if (!found) {
const computed = fn();
found = new Map([[innerKey, computed]]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type $IntentionalAny = any;
/**
* This special type can help generate pattern matchers for you!
* Just use it as so:
* // enum-ts
* type Result<Ok, Err> = Enum<{
* Ok: Ok,
* Err: Err,