Files
v10/dist/default/i18n/use-locale-root-notifications.js

27 lines
1.1 KiB
JavaScript

import { useCallback, useEffect, useReducer, useRef } from "react";
//#region src/i18n/use-locale-root-notifications.ts
function useLocaleRootNotifications(resolvedLocale, onActiveLocaleChange) {
const onActiveLocaleChangeRef = useRef(onActiveLocaleChange);
onActiveLocaleChangeRef.current = onActiveLocaleChange;
const childLocaleRootCountRef = useRef(0);
const [localeRootEpoch, invalidateLocaleRoots] = useReducer((epoch) => epoch + 1, 0);
const addLocaleRoot = useCallback(() => {
childLocaleRootCountRef.current += 1;
return () => {
childLocaleRootCountRef.current = Math.max(0, childLocaleRootCountRef.current - 1);
if (childLocaleRootCountRef.current === 0) invalidateLocaleRoots();
};
}, []);
useEffect(() => {
const id = setTimeout(() => {
if (childLocaleRootCountRef.current > 0) return;
onActiveLocaleChangeRef.current?.(resolvedLocale);
}, 0);
return () => clearTimeout(id);
}, [resolvedLocale, localeRootEpoch]);
return addLocaleRoot;
}
//#endregion
export { useLocaleRootNotifications };
//# sourceMappingURL=use-locale-root-notifications.js.map