@@ -22,6 +22,8 @@ import {
2222import {
2323 CheckResult ,
2424 ProgressData ,
25+ UpdateCheckState ,
26+ UPDATE_CHECK_STATUS ,
2527 UpdateTestPayload ,
2628 VersionInfo ,
2729} from './type' ;
@@ -45,8 +47,17 @@ export const UpdateProvider = ({
4547 const updateInfoRef = useRef ( updateInfo ) ;
4648 const [ progress , setProgress ] = useState < ProgressData > ( ) ;
4749 const [ lastError , setLastError ] = useState < Error > ( ) ;
50+ const [ checkState , setCheckStateState ] = useState < UpdateCheckState > ( {
51+ status : UPDATE_CHECK_STATUS . IDLE ,
52+ } ) ;
53+ const checkStateRef = useRef ( checkState ) ;
4854 const lastChecking = useRef ( 0 ) ;
4955
56+ const setCheckState = useCallback ( ( nextState : UpdateCheckState ) => {
57+ checkStateRef . current = nextState ;
58+ setCheckStateState ( nextState ) ;
59+ } , [ ] ) ;
60+
5061 const throwErrorIfEnabled = useCallback (
5162 ( e : Error ) => {
5263 if ( options . throwError ) {
@@ -58,7 +69,13 @@ export const UpdateProvider = ({
5869
5970 const dismissError = useCallback ( ( ) => {
6071 setLastError ( undefined ) ;
61- } , [ ] ) ;
72+ if ( checkStateRef . current . error ) {
73+ setCheckState ( {
74+ ...checkStateRef . current ,
75+ error : undefined ,
76+ } ) ;
77+ }
78+ } , [ setCheckState ] ) ;
6279
6380 const alertUpdate = useCallback (
6481 ( ...args : Parameters < typeof Alert . alert > ) => {
@@ -164,20 +181,56 @@ export const UpdateProvider = ({
164181 const checkUpdate = useCallback (
165182 async ( { extra } : { extra ?: Partial < { toHash : string } > } = { } ) => {
166183 const now = Date . now ( ) ;
184+ // 频繁重复触发时不再发起新检查,但需要把本次调用标记为跳过。
167185 if ( lastChecking . current && now - lastChecking . current < 1000 ) {
186+ if ( checkStateRef . current . status !== UPDATE_CHECK_STATUS . CHECKING ) {
187+ setCheckState ( {
188+ status : UPDATE_CHECK_STATUS . SKIPPED ,
189+ } ) ;
190+ }
168191 return ;
169192 }
170193 lastChecking . current = now ;
194+ setCheckState ( {
195+ status : UPDATE_CHECK_STATUS . CHECKING ,
196+ } ) ;
197+ let result : CheckResult | void ;
171198 let rootInfo : CheckResult | undefined ;
172199 try {
173- rootInfo = { ...( await client . checkUpdate ( extra ) ) } ;
200+ result = await client . checkUpdate ( extra ) ;
201+ rootInfo = { ...result } ;
174202 } catch ( e : any ) {
175203 setLastError ( e ) ;
204+ setCheckState ( {
205+ status : UPDATE_CHECK_STATUS . ERROR ,
206+ error : e ,
207+ } ) ;
176208 alertError ( client . t ( 'error_update_check_failed' ) , e . message ) ;
177209 throwErrorIfEnabled ( e ) ;
178210 return ;
179211 }
212+ let nextCheckStatus : UpdateCheckState [ 'status' ] ;
213+ let nextCheckError : Error | undefined ;
214+ // client.checkUpdate 默认会兜底返回旧结果或空对象,这里只额外标记状态,不截断原有流程。
215+ if ( client . lastCheckError ) {
216+ nextCheckStatus = UPDATE_CHECK_STATUS . ERROR ;
217+ nextCheckError = client . lastCheckError ;
218+ setLastError ( client . lastCheckError ) ;
219+ alertError (
220+ client . t ( 'error_update_check_failed' ) ,
221+ client . lastCheckError . message ,
222+ ) ;
223+ } else if ( ! result ) {
224+ // undefined 表示本次调用被内部策略跳过,例如 beforeCheckUpdate 返回 false。
225+ nextCheckStatus = UPDATE_CHECK_STATUS . SKIPPED ;
226+ } else {
227+ nextCheckStatus = UPDATE_CHECK_STATUS . COMPLETED ;
228+ }
180229 if ( ! rootInfo ) {
230+ setCheckState ( {
231+ status : nextCheckStatus ,
232+ error : nextCheckError ,
233+ } ) ;
181234 return ;
182235 }
183236 const versions = [ rootInfo . expVersion , rootInfo ] . filter (
@@ -202,6 +255,18 @@ export const UpdateProvider = ({
202255 }
203256 updateInfoRef . current = info ;
204257 setUpdateInfo ( info ) ;
258+ if ( nextCheckStatus === UPDATE_CHECK_STATUS . COMPLETED ) {
259+ setCheckState ( {
260+ status : nextCheckStatus ,
261+ result : info ,
262+ error : nextCheckError ,
263+ } ) ;
264+ } else {
265+ setCheckState ( {
266+ status : nextCheckStatus ,
267+ error : nextCheckError ,
268+ } ) ;
269+ }
205270 if ( info . expired ) {
206271 if (
207272 options . onPackageExpired &&
@@ -268,11 +333,16 @@ export const UpdateProvider = ({
268333 }
269334 return info ;
270335 }
336+ setCheckState ( {
337+ status : nextCheckStatus ,
338+ error : nextCheckError ,
339+ } ) ;
271340 } ,
272341 [
273342 client ,
274343 alertError ,
275344 throwErrorIfEnabled ,
345+ setCheckState ,
276346 options ,
277347 alertUpdate ,
278348 downloadAndInstallApk ,
@@ -414,6 +484,7 @@ export const UpdateProvider = ({
414484 currentVersionInfo,
415485 parseTestQrCode,
416486 restartApp,
487+ checkState,
417488 } } >
418489 { children }
419490 </ UpdateContext . Provider >
0 commit comments