UNPKG

@ryo-98/react-api-bridge

Version:
38 lines (37 loc) 1.26 kB
import React, { forwardRef, useContext, useImperativeHandle, useMemo } from "react"; import { getApiDesc } from "../../core/index.js"; function createBoundary(apiBridge) { const Boundary = forwardRef((props, ref) => { const _ownBridge = useMemo(() => ({}), []); const bridge = props.contextValue?.bridge || _ownBridge; const paramParent = props.contextValue?.parent; const upperParent = useContext(apiBridge.BridgeContext); const parent = paramParent || upperParent; const _payload = props.contextValue?.payload ?? props.payload; useImperativeHandle(ref, () => { return { getAPI: (name) => { return getApiDesc(name, bridge, apiBridge.bridgeOptions).apiNList; }, parent, payload: _payload }; }, [_payload]); const contextValue = useMemo(() => { const final = { ...props.contextValue || { bridge, parent }, payload: _payload }; return final; }, [props.contextValue, _payload]); return /* @__PURE__ */ React.createElement(apiBridge.BridgeContext.Provider, { value: contextValue }, props.children); }); Boundary.displayName = "Boundary"; return Boundary; } export { createBoundary as default };