UNPKG

schoolx-ota-manager

Version:

React Native library for managing OTA updates with GitLab integration

212 lines 7.83 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { jsx as _jsx } from "react/jsx-runtime"; import React, { useEffect, useRef } from "react"; import { Platform } from "react-native"; import { OtaManager } from "../OtaManager"; // HOC function export function withOTAUpdate(WrappedComponent, config) { return function WithOTAUpdateComponent(props) { const otaManagerRef = useRef(); const isInitialized = useRef(false); const [updateInfo, setUpdateInfo] = React.useState(null); const [isChecking, setIsChecking] = React.useState(false); const [isInstalling, setIsInstalling] = React.useState(false); useEffect(() => { if (!isInitialized.current) { const otaManager = new OtaManager(Object.assign(Object.assign({}, config), { platform: Platform.OS, autoUpdate: false })); otaManagerRef.current = otaManager; isInitialized.current = true; // Initial check performInitialCheck(); } return () => { // Cleanup if needed }; }, []); const performInitialCheck = async () => { try { if (otaManagerRef.current) { const updateInfo = await otaManagerRef.current.checkUpdate(); console.log("Available update", updateInfo); if (updateInfo.hasUpdate) { otaManagerRef.current.installUpdate(); } } } catch (error) { console.error("Initial OTA check failed:", error); } }; const checkUpdate = async () => { if (!otaManagerRef.current) return null; setIsChecking(true); try { const info = await otaManagerRef.current.checkUpdate(); setUpdateInfo(info); return info; } catch (error) { console.error("Check update failed:", error); return null; } finally { setIsChecking(false); } }; const installUpdate = async () => { if (!otaManagerRef.current) return false; setIsInstalling(true); try { const success = await otaManagerRef.current.installUpdate(); return success; } catch (error) { console.error("Install update failed:", error); return false; } finally { setIsInstalling(false); } }; const checkAndInstallUpdate = async () => { if (!otaManagerRef.current) return false; setIsChecking(true); setIsInstalling(true); try { const success = await otaManagerRef.current.checkAndInstallUpdate(); return success; } catch (error) { console.error("Check and install update failed:", error); return false; } finally { setIsChecking(false); setIsInstalling(false); } }; const checkOnAppStart = async () => { if (!otaManagerRef.current) return null; try { const info = await otaManagerRef.current.checkOnAppStart(); setUpdateInfo(info); return info; } catch (error) { console.error("Check on app start failed:", error); return null; } }; const getInstalledBuildNumber = async () => { if (!otaManagerRef.current) return 0; try { return await otaManagerRef.current.getInstalledBuildNumber(); } catch (error) { console.error("Get installed build number failed:", error); return 0; } }; const hocProps = { otaCheckUpdate: checkUpdate, otaInstallUpdate: installUpdate, otaCheckAndInstallUpdate: checkAndInstallUpdate, otaCheckOnAppStart: checkOnAppStart, otaGetInstalledBuildNumber: getInstalledBuildNumber, otaUpdateInfo: updateInfo, otaIsChecking: isChecking, otaIsInstalling: isInstalling, }; return _jsx(WrappedComponent, Object.assign({}, props, hocProps)); }; } export const OTAUpdate = (_a) => { var { onProgress, onUpdateAvailable, onUpdateCompleted, children } = _a, config = __rest(_a, ["onProgress", "onUpdateAvailable", "onUpdateCompleted", "children"]); const otaManagerRef = useRef(); const isInitialized = useRef(false); useEffect(() => { if (!isInitialized.current) { const otaManager = new OtaManager(Object.assign(Object.assign({}, config), { platform: Platform.OS, autoUpdate: false })); // Set progress callback if (onProgress) { otaManager.onProgress(onProgress); } otaManagerRef.current = otaManager; isInitialized.current = true; // Initial check performInitialCheck(); } return () => { }; }, []); const performInitialCheck = async () => { try { if (otaManagerRef.current) { const updateInfo = await otaManagerRef.current.checkUpdate(); console.log("Available update", updateInfo); if (updateInfo.hasUpdate) { otaManagerRef.current.installUpdate(); } } } catch (error) { console.error("Initial OTA check failed:", error); } }; React.useImperativeHandle(React.useRef(), () => ({ checkUpdate: async () => { if (otaManagerRef.current) { return await otaManagerRef.current.checkUpdate(); } return null; }, installUpdate: async () => { if (otaManagerRef.current) { const success = await otaManagerRef.current.installUpdate(); if (onUpdateCompleted) { onUpdateCompleted(success); } return success; } return false; }, checkAndInstallUpdate: async () => { if (otaManagerRef.current) { const success = await otaManagerRef.current.checkAndInstallUpdate(); if (onUpdateCompleted) { onUpdateCompleted(success); } return success; } return false; }, checkOnAppStart: async () => { if (otaManagerRef.current) { return await otaManagerRef.current.checkUpdate(); } return null; }, getInstalledBuildNumber: async () => { if (otaManagerRef.current) { return await otaManagerRef.current.getInstalledBuildNumber(); } return 0; }, })); return children; }; //# sourceMappingURL=OTAUpdate.js.map