Skip to content

Instantly share code, notes, and snippets.

View Sheraff's full-sized avatar
🐙

Flo Sheraff

🐙
View GitHub Profile
@Sheraff
Sheraff / repro.js
Created October 10, 2026 20:13
vite chokidar throttle repro
// node repro.js <gap in ms>
import { readFileSync, writeFileSync } from 'node:fs'
import { setTimeout as sleep } from 'node:timers/promises'
import { createServer } from 'vite'
const gap = Number(process.argv[2])
writeFileSync('root/a.js', 'export default 0\n')
const server = await createServer({ root: 'root', configFile: false, server: { middlewareMode: true, hmr: false } })
await new Promise((resolve) => server.watcher.once('ready', resolve))
server.watcher.on('change', (file) => console.log('change:', readFileSync(file, 'utf8').trim()))
@Sheraff
Sheraff / README.md
Last active September 30, 2026 17:20
Proposed Rolldown PluginContext.load sender-lifetime fix (not compiled or tested; base v1.0.2)

Proposed fix for this.load() after HMR

Reproduction: with Vite's bundled dev mode, this.load() returns source initially but returns null for a new dependency after HMR.

This patch has not been compiled or tested. It has been formatted, checked for whitespace errors, and checked that it applies to the commit below.

Why it fails

import { useSyncExternalStore, useCallback } from "react";
/**
* Use for getting the value of given `key` in `localStorage`, or setting it.
*
* @param key - `localStorage` key we want to get its value, or change it
* @example
*
* ```tsx
* const [hello, setHello] = useLocalStorageKey("hello");
interface ReadonlySignal<T> {
invalidate(): void
readonly value: T
}
interface WritableSignal<T> {
value: T
}
let running: ReadonlySignal<any> | null = null
function getSupportedLocales(): string[] {
const candidates: string[] = []
const letters = Array.from({ length: 26 }, (_, i) => String.fromCharCode(65 + i))
for (const a of letters) {
for (const b of letters) {
candidates.push(a + b)
for (const c of letters) {
candidates.push(a + b + c)
}
@Sheraff
Sheraff / createLRUCache.ts
Created July 22, 2025 21:30
Fast LRU cache
export type LRUCache<K, V> = {
get: (key: K) => V | undefined
set: (key: K, value: V) => void
}
export function createLRUCache<K, V>(max: number): LRUCache<K, V> {
type Node = { prev?: Node; next?: Node; key: K, value: V }
const cache = new Map<K, Node>()
let oldest: Node | undefined
let newest: Node | undefined
const touch = (entry: Node) => {
@Sheraff
Sheraff / LRUMap.ts
Created July 22, 2025 20:34
LRU cache as a JS Map
class LRUMap<TKey, TValue> extends Map<TKey, TValue> {
constructor(max: number) {
super()
const get = this.get.bind(this)
const set = this.set.bind(this)
this.get = (key: TKey) => {
const item = get(key)
if (item !== undefined) {
// refresh key
this.delete(key)
@Sheraff
Sheraff / App.tsx
Last active November 16, 2023 23:35
beenawhile2
//@ts-check
const Component = ({ names }: { names: [string] }) => (
<p>{names[0]}</p>
)
export default function App() {
return <div>Hello world</div>
}
import { Suspense } from "react";
type CacheItem<T> = {
keys: any[]
promise: Promise<T>
error?: unknown
response?: T
}
const cache: CacheItem<any>[] = []
@Sheraff
Sheraff / script.js
Created June 21, 2022 06:19
NEAT string genome
const ACTIVATIONS = [
/*'identity': */x => x,
/*'abs': */ x => Math.abs(x),
/*'clamped': */ x => Math.min(1, Math.max(-1, x)),
/*'cube': */ x => Math.pow(x, 3),
/*'exp': */ x => Math.exp(x),
/*'gauss': */ x => x,
/*'hat': */ x => Math.max(0, x < 0 ? 1 + x : 1 - x),
/*'inv': */ x => 1 / x,
/*'log': */ x => Math.log(x),