Skip to content

Instantly share code, notes, and snippets.

@debugtheworldbot
debugtheworldbot / jotai.tsx
Last active September 15, 2023 06:10
minimal jotai implementation
import { useSyncExternalStore } from 'react'
type CB<T> = (v: T) => void
export type Atom<V> = {
get: () => V
set: (v: V | ((oldV: V) => V)) => V
subscribe: (cb: CB<V>) => () => void
}
export function atom<AtomValue>(value: AtomValue): Atom<AtomValue> {
const subscribers = new Set<(value: AtomValue) => void>()
JavaScript 1 hr 16 mins β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ– 97.3%
TypeScript 2 mins β–Œβ–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘ 2.6%
JSON 0 secs β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘ 0.0%