UNPKG

@hitachivantara/uikit-react-lab

Version:

Contributed React components to UI Kit by the community.

41 lines (40 loc) 1.2 kB
import { createContext, useCallback, useContext, useMemo, useRef } from "react"; import { jsx } from "react/jsx-runtime"; //#region src/Flow/FlowContext/NodeMetaContext.tsx var HvFlowNodeMetaContext = createContext(void 0); var HvFlowNodeMetaProvider = ({ children }) => { const registryRef = useRef({}); const registerNode = useCallback((id, nodeInfo) => { registryRef.current[id] = nodeInfo; }, []); const unregisterNode = useCallback((id) => { delete registryRef.current[id]; }, []); const getRegistry = useCallback(() => { return registryRef.current; }, []); const value = useMemo(() => ({ registerNode, unregisterNode, getRegistry }), [ registerNode, unregisterNode, getRegistry ]); return /* @__PURE__ */ jsx(HvFlowNodeMetaContext.Provider, { value, children }); }; function useNodeMetaRegistry() { const context = useContext(HvFlowNodeMetaContext); if (context === void 0) throw new Error("useNodeRegistry must be used within a HvFlowNodeMetaProvider"); return { registerNode: context.registerNode, unregisterNode: context.unregisterNode, registry: context.getRegistry() }; } //#endregion export { HvFlowNodeMetaProvider, useNodeMetaRegistry };