import React, { useRef } from 'react'; import { BehaviorSubject, Subject } from 'rxjs'; import { throttleTime } from 'rxjs/operators'; export const useSubject = (valueRef?: React.RefObject) => { const subjectRef = useRef(new Subject()); const push = (value?: any) => { subjectRef.current.next(value ?? valueRef?.current); }; const subscription = subjectRef.current; return { push, subscription }; }; export const useBehaviorSubject = ( valueRef: React.RefObject, throttle = 100, ) => { const subjectRef = useRef(new BehaviorSubject(valueRef.current)); const push = (value?: any) => { subjectRef.current.next(value ?? valueRef.current); }; const subscription = !throttle ? subjectRef.current : subjectRef.current.pipe( throttleTime(throttle, undefined, { leading: true, trailing: true, }), ); return { push, subscription }; };