UNPKG

@fancode/react-native-codepush-joystick

Version:
81 lines 2.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useCodePushManager = void 0; const react_1 = require("react"); const codepush_manager_1 = require("../codepush-manager"); const managerMap = new WeakMap(); function useCodePushManager(config) { const [stateMap, setStateMap] = (0, react_1.useState)({}); const managerRef = (0, react_1.useRef)(null); const callbacksSetRef = (0, react_1.useRef)(false); const handleStateChange = (0, react_1.useCallback)((pr, newState, oldState) => { setStateMap((prev) => ({ ...prev, [pr.id]: newState, })); config?.callbacks?.onStateChange?.(pr, newState, oldState); }, [config?.callbacks?.onStateChange]); (0, react_1.useEffect)(() => { if (!config) { managerRef.current = null; callbacksSetRef.current = false; return; } if (!managerMap.has(config)) { managerMap.set(config, new codepush_manager_1.CodePushManager({ ...config, callbacks: { ...config.callbacks, onStateChange: handleStateChange, }, })); } managerRef.current = managerMap.get(config); managerRef.current.setCallbacks({ ...config.callbacks, onStateChange: handleStateChange, }); callbacksSetRef.current = true; }, [config, handleStateChange]); const fetchPullRequests = (0, react_1.useCallback)(async (options) => { if (!managerRef.current) { console.warn("Manager not ready yet"); return []; } // Ensure callbacks are set before fetching if (!callbacksSetRef.current && config) { managerRef.current.setCallbacks({ ...config.callbacks, onStateChange: handleStateChange, }); callbacksSetRef.current = true; } return await managerRef.current.fetchPullRequests(options); }, [config, handleStateChange]); const resetState = (0, react_1.useCallback)((prId) => { if (prId !== undefined) { setStateMap((prev) => { const updated = { ...prev }; delete updated[prId]; return updated; }); if (managerRef.current) { managerRef.current.resetState(prId); } } else { setStateMap({}); if (managerRef.current) { managerRef.current.resetState(); } } }, []); return { manager: managerRef.current, stateMap, fetchPullRequests, resetState, }; } exports.useCodePushManager = useCodePushManager; //# sourceMappingURL=use-code-push-manager.js.map