vega-frontend-monorepo/libs/react-helpers/src/hooks/use-mutation-observer.ts
Art 446af23d9c
fix: orderbook sticky behaviour and precision options (#1519)
* fix: orderbook sticky behaviour and precision options

* chore: add handling of y-axis resize

* fix: addressed PR comments

* fix: addressed PR comments

* added cy.log

* Update apps/trading-e2e/src/support/vega-wallet.ts
2022-09-29 14:23:14 +02:00

42 lines
1021 B
TypeScript

import { captureException } from '@sentry/react';
import debounce from 'lodash/debounce';
import { useEffect, useMemo } from 'react';
type MutationObserverConfiguration = {
debounceTime: number;
config: MutationObserverInit;
};
const DEFAULT_OPTIONS: MutationObserverConfiguration = {
debounceTime: 0,
config: {
attributes: true,
childList: false,
subtree: false,
},
};
export function useMutationObserver(
target: Node | null,
callback: MutationCallback,
options: MutationObserverConfiguration = DEFAULT_OPTIONS
) {
const observer = useMemo(() => {
return new MutationObserver(
options.debounceTime > 0
? debounce(callback, options.debounceTime)
: callback
);
}, [callback, options.debounceTime]);
useEffect(() => {
if (!observer || !target) return;
try {
observer.observe(target, options.config);
} catch (err) {
captureException(err);
}
return () => observer?.disconnect();
}, [observer, options.config, target]);
}