@ryo-98/react-api-bridge
Version:
A registry for component imperative api
63 lines (62 loc) • 2.55 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var useInitEffect_exports = {};
__export(useInitEffect_exports, {
useInitEffect: () => useInitEffect
});
module.exports = __toCommonJS(useInitEffect_exports);
var import_useHookId = require("../utils/useHookId.js");
var import_react = require("react");
var import_mountHookInitEffect = require("./mountHookInitEffect.js");
var import_mappedSet = require("../utils/mappedSet.js");
function useInitEffect(onInit, name, apiNList, contextValue, cacheInitCbMap, bridgeOptions, initializedOnInitMap) {
const hookId = (0, import_useHookId.useHookId)();
const callbackInfoRef = (0, import_react.useRef)(void 0);
(0, import_react.useEffect)(() => {
if (!onInit) return;
let cacheCbs = cacheInitCbMap.get(apiNList);
if (!cacheCbs) {
cacheCbs = /* @__PURE__ */ new Map();
cacheInitCbMap.set(apiNList, cacheCbs);
}
const callbackInfo = {
onInit,
touchedRefs: /* @__PURE__ */ new Set()
};
callbackInfoRef.current = callbackInfo;
cacheCbs.set(hookId, callbackInfo);
const clearInitEffect = (0, import_mountHookInitEffect.mountHookInitEffect)(name, onInit, apiNList, hookId, bridgeOptions, initializedOnInitMap, callbackInfo.touchedRefs);
return () => {
cacheCbs.delete(hookId);
if (cacheCbs.size === 0) {
cacheInitCbMap.delete(apiNList);
}
clearInitEffect?.();
callbackInfo.touchedRefs.forEach((apiRef) => {
(0, import_mappedSet.removeFromMappedSet)(initializedOnInitMap, apiRef, hookId);
});
callbackInfo.touchedRefs.clear();
callbackInfoRef.current = void 0;
};
}, []);
(0, import_react.useEffect)(() => {
if (!onInit || !callbackInfoRef.current) return;
callbackInfoRef.current.onInit = onInit;
}, [onInit]);
}