UNPKG

@ryo-98/react-api-bridge

Version:
59 lines (58 loc) 2.58 kB
"use strict"; 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 useTools_exports = {}; __export(useTools_exports, { useTools: () => useTools }); module.exports = __toCommonJS(useTools_exports); var import_core = require("../../core/index.js"); var import_react = require("react"); function useTools(apiBridge, hookOptions) { const contextValue = (0, import_core.useFinalContextValue)(hookOptions, apiBridge.BridgeContext); const getAPI = (0, import_react.useCallback)((_name, options) => { return (0, import_core.getApiDesc)(_name, options?.contextValue?.bridge || contextValue.bridge, apiBridge.bridgeOptions).apiNList; }, [contextValue.bridge]); const getBoundaryPayload = (0, import_react.useCallback)((options) => { return (options?.contextValue || contextValue).payload; }, [contextValue.payload]); const getUpperAPI = (0, import_react.useCallback)((_name, options) => { return (0, import_core.getUpperApiDesc)(options?.contextValue || contextValue, _name, options, apiBridge.bridgeOptions)?.apiNList; }, []); const getUpperBoundaryPayload = (0, import_react.useCallback)((options) => { const parent = (0, import_core.getUpperContextValue)(options?.contextValue || contextValue, options?.shouldForwardYield); if (!parent) return; return parent.payload; }, []); const getAPIAsync = (_name, options) => { const { initial } = { initial: true, ...options }; const { apiNList } = (0, import_core.getApiDesc)(_name, (options?.contextValue || contextValue).bridge, apiBridge.bridgeOptions); const { resolver } = (0, import_core.getResolverOrCreateWhenMissing)(apiNList, contextValue, initial, apiBridge.pendingResolverMap); return resolver.promise; }; return { getAPI, getBoundaryPayload, getUpperAPI, getUpperBoundaryPayload, getAPIAsync }; }