@hitachivantara/uikit-react-core
Version:
Core React components for the NEXT Design System.
96 lines (95 loc) • 3.68 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const React = require("react");
const FinalizationRegistryBasedCleanupTracking = require("../utils/FinalizationRegistryBasedCleanupTracking.cjs");
const TimerBasedCleanupTracking = require("../utils/TimerBasedCleanupTracking.cjs");
function _interopNamespace(e) {
if (e && e.__esModule) return e;
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
if (e) {
for (const k in e) {
if (k !== "default") {
const d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: () => e[k]
});
}
}
}
n.default = e;
return Object.freeze(n);
}
const React__namespace = /* @__PURE__ */ _interopNamespace(React);
class ObjectToBeRetainedByReact {
}
function createUseInstanceEventHandler(registryContainer2) {
let cleanupTokensCounter = 0;
return function useInstanceEventHandler2(instance, eventName, handler) {
if (registryContainer2.registry === null) {
registryContainer2.registry = typeof FinalizationRegistry !== "undefined" ? new FinalizationRegistryBasedCleanupTracking.FinalizationRegistryBasedCleanupTracking() : new TimerBasedCleanupTracking.TimerBasedCleanupTracking();
}
const [objectRetainedByReact] = React__namespace.useState(
new ObjectToBeRetainedByReact()
);
const subscription = React__namespace.useRef(null);
const handlerRef = React__namespace.useRef(void 0);
handlerRef.current = handler;
const cleanupTokenRef = React__namespace.useRef(null);
if (!subscription.current && handlerRef.current) {
const enhancedHandler = (params, event) => {
if (!event.defaultMuiPrevented) {
handlerRef.current?.(params, event);
}
};
subscription.current = instance.$$subscribeEvent(
eventName,
enhancedHandler
);
cleanupTokensCounter += 1;
cleanupTokenRef.current = { cleanupToken: cleanupTokensCounter };
registryContainer2.registry.register(
objectRetainedByReact,
// The callback below will be called once this reference stops being retained
() => {
subscription.current?.();
subscription.current = null;
cleanupTokenRef.current = null;
},
cleanupTokenRef.current
);
} else if (!handlerRef.current && subscription.current) {
subscription.current();
subscription.current = null;
if (cleanupTokenRef.current) {
registryContainer2.registry.unregister(cleanupTokenRef.current);
cleanupTokenRef.current = null;
}
}
React__namespace.useEffect(() => {
if (!subscription.current && handlerRef.current) {
const enhancedHandler = (params, event) => {
if (!event.defaultMuiPrevented) {
handlerRef.current?.(params, event);
}
};
subscription.current = instance.$$subscribeEvent(
eventName,
enhancedHandler
);
}
if (cleanupTokenRef.current && registryContainer2.registry) {
registryContainer2.registry.unregister(cleanupTokenRef.current);
cleanupTokenRef.current = null;
}
return () => {
subscription.current?.();
subscription.current = null;
};
}, [instance, eventName]);
};
}
const registryContainer = { registry: null };
const useInstanceEventHandler = createUseInstanceEventHandler(registryContainer);
exports.createUseInstanceEventHandler = createUseInstanceEventHandler;
exports.useInstanceEventHandler = useInstanceEventHandler;