Skip to content

Instantly share code, notes, and snippets.

@mwmcode
mwmcode / vanilla-observable.ts
Created January 16, 2024 15:36 — forked from SgtPooki/vanilla-observable.ts
Typescript version of a simple Observable. This was copied from https://stackoverflow.com/a/62002044/592760, translated to TypeScript, and improved.
/**
* Modified by Russell Dempsey on 2021 DEC 15
* @see https://stackoverflow.com/a/62002044/592760
*/
type Subscriber<T> = (value: T) => void;
class Observable<T> {
private subscribers = new Set<Subscriber<T>>();
constructor(private value: T) {}
// TODO: make `pages` optional and measure the div when unspecified, this will
// allow more normal document flow and make it easier to do both mobile and
// desktop.
import {
createContext,
useCallback,
useContext,
useEffect,
useMemo,
useRef,