A small barebones reactive library built for a stripped back observable primitive. The api and method names are borrowed from rxjs.
npm install mini-rx # or yarn add mini-rx or pnpm add mini-rx
Observable
Unlike rxjs this observable provides no error pathway and never completes.
type Observable<T> = (subscriber: (x: T) => void): () => void
Usage
import { pipe, merge, map, of, switchMap, fromEvent } from 'mini-rx'
const box = document.getElementById('#box')!;
const $pointer_over = pipe(
merge(
pipe(fromEvent(box, 'pointerenter'), map(() => true)),
pipe(fromEvent(box, 'pointerleave'), map(() => false))
),
)
// Get the pointer position when pointer is over box
const $pointer_position = pipe(
$pointer_over,
switchMap(is_over => is_over ? fromEvent(box, 'pointermove') : of()),
map(e => ({ x: e.clientX, y: e.clientY }))
)
// Subscribe to events
$pointer_over((x: boolean) => ...)
$pointer_position((x: { x: nunber, y: number }) => ...)
License
MIT © Dan Beaven