/** * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal */ import { useEffect, useState } from 'react'; interface Behavior { value: T; subscribe(f: (v: T) => void): { unsubscribe(): void }; } export function useBehavior(s: Behavior): T; // eslint-disable-next-line export function useBehavior(s: Behavior | undefined): T | undefined; // eslint-disable-next-line export function useBehavior(s: Behavior | undefined): T | undefined { const [value, setValue] = useState(s?.value); useEffect(() => { if (!s) return; let fst = true; const sub = s.subscribe((v) => { if (fst) { fst = false; if (v !== value) setValue(v); } else setValue(v); }); return () => { sub.unsubscribe(); }; // eslint-disable-next-line }, [s]); return value; }