@@ -25,6 +25,9 @@ const { GridItem } = Grid;
2525
2626const { Text } = Typography ;
2727
28+ // pageExecute is to store subscribe function(s) globally
29+ const pageExecute : Record < string , ( data : any ) => void > = { } ;
30+
2831function App ( ) {
2932 const { subscribeMessage } = useAppContext ( ) ;
3033
@@ -63,83 +66,80 @@ function App() {
6366 } , 1000 ) ;
6467 } , [ mTimeClose ] ) ;
6568
66- useEffect ( ( ) => {
67- const getBatchUpdateRecord = async ( ) : Promise < TBatchUpdateRecordObject | null > => {
68- let resultText = "" ;
69- let r ;
70- let i = 0 ;
71- while ( true ) {
72- r = await scriptClient . getBatchUpdateRecordLite ( i ++ ) ;
73- if ( ! r ) break ;
74- const chunk = r . chunk ;
75- if ( typeof chunk !== "string" ) break ;
76- resultText += chunk ;
77- if ( r . ended ) break ;
78- }
79- return resultText ? JSON . parse ( resultText ) : null ;
80- } ;
81-
82- const updateRecord = ( ) => {
83- getBatchUpdateRecord ( ) . then ( ( batchUpdateRecordObjectLite ) => {
84- const list = batchUpdateRecordObjectLite ?. list || [ ] ;
85- const site = [ ] as TBatchUpdateRecord [ ] ;
86- const other = [ ] as TBatchUpdateRecord [ ] ;
87- const ignored = [ ] as TBatchUpdateRecord [ ] ;
88- for ( const entry of list ) {
89- let mEntry = entry ;
90- if ( ! entry . checkUpdate ) {
91- site . push ( entry ) ;
92- continue ;
93- }
94- const isIgnored = entry . script . ignoreVersion === entry . newMeta ?. version ?. [ 0 ] ;
95- mEntry = {
96- ...entry ,
97- } ;
69+ const getBatchUpdateRecord = async ( ) : Promise < TBatchUpdateRecordObject | null > => {
70+ let resultText = "" ;
71+ let r ;
72+ let i = 0 ;
73+ while ( true ) {
74+ r = await scriptClient . getBatchUpdateRecordLite ( i ++ ) ;
75+ if ( ! r ) break ;
76+ const chunk = r . chunk ;
77+ if ( typeof chunk !== "string" ) break ;
78+ resultText += chunk ;
79+ if ( r . ended ) break ;
80+ }
81+ return resultText ? JSON . parse ( resultText ) : null ;
82+ } ;
9883
99- if ( isIgnored ) {
100- ignored . push ( mEntry ) ;
84+ const updateRecord = ( ) => {
85+ getBatchUpdateRecord ( ) . then ( ( batchUpdateRecordObjectLite ) => {
86+ const list = batchUpdateRecordObjectLite ?. list || [ ] ;
87+ const site = [ ] as TBatchUpdateRecord [ ] ;
88+ const other = [ ] as TBatchUpdateRecord [ ] ;
89+ const ignored = [ ] as TBatchUpdateRecord [ ] ;
90+ for ( const entry of list ) {
91+ if ( ! entry . checkUpdate ) {
92+ site . push ( entry ) ;
93+ continue ;
94+ }
95+ const isIgnored = entry . script . ignoreVersion === entry . newMeta ?. version ?. [ 0 ] ;
96+ const mEntry = {
97+ ...entry ,
98+ } ;
99+
100+ if ( isIgnored ) {
101+ ignored . push ( mEntry ) ;
102+ } else {
103+ if ( ! paramSite || mEntry . sites ?. includes ( paramSite ) ) {
104+ site . push ( mEntry ) ;
101105 } else {
102- if ( ! paramSite || mEntry . sites ?. includes ( paramSite ) ) {
103- site . push ( mEntry ) ;
104- } else {
105- other . push ( mEntry ) ;
106- }
106+ other . push ( mEntry ) ;
107107 }
108108 }
109- setRecords ( { site, other, ignored } ) ;
110- } ) ;
111- } ;
112-
113- const pageApi = {
114- onScriptUpdateCheck ( data : any ) {
115- if (
116- mRecords === null &&
117- ( ( data . status ?? 0 ) & UpdateStatusCode . CHECKING_UPDATE ) === 0 &&
118- ( ( data . status ?? 0 ) & UpdateStatusCode . CHECKED_BEFORE ) === UpdateStatusCode . CHECKED_BEFORE
119- ) {
120- setStatusText (
121- t ( "updatepage.status_last_check" ) . replace ( "$0" , data . checktime ? dayFormat ( new Date ( data . checktime ) ) : "" )
122- ) ;
123- updateRecord ( ) ;
124- setCheckUpdateSpin ( false ) ;
125- } else if ( ( ( data . status ?? 0 ) & UpdateStatusCode . CHECKING_UPDATE ) === UpdateStatusCode . CHECKING_UPDATE ) {
126- setStatusText ( t ( "updatepage.status_checking_updates" ) ) ;
127- setRecords ( null ) ;
128- setCheckUpdateSpin ( true ) ;
129- } else if ( mRecords !== null && data . refreshRecord === true ) {
130- updateRecord ( ) ;
131- }
132- } ,
133- } ;
109+ }
110+ setRecords ( { site, other, ignored } ) ;
111+ } ) ;
112+ } ;
113+
114+ const onScriptUpdateCheck = ( data : any ) => {
115+ if (
116+ mRecords === null &&
117+ ( ( data . status ?? 0 ) & UpdateStatusCode . CHECKING_UPDATE ) === 0 &&
118+ ( ( data . status ?? 0 ) & UpdateStatusCode . CHECKED_BEFORE ) === UpdateStatusCode . CHECKED_BEFORE
119+ ) {
120+ setStatusText (
121+ t ( "updatepage.status_last_check" ) . replace ( "$0" , data . checktime ? dayFormat ( new Date ( data . checktime ) ) : "" )
122+ ) ;
123+ updateRecord ( ) ;
124+ setCheckUpdateSpin ( false ) ;
125+ } else if ( ( ( data . status ?? 0 ) & UpdateStatusCode . CHECKING_UPDATE ) === UpdateStatusCode . CHECKING_UPDATE ) {
126+ setStatusText ( t ( "updatepage.status_checking_updates" ) ) ;
127+ setRecords ( null ) ;
128+ setCheckUpdateSpin ( true ) ;
129+ } else if ( mRecords !== null && data . refreshRecord === true ) {
130+ updateRecord ( ) ;
131+ }
132+ } ;
134133
135- return subscribeMessage ( "onScriptUpdateCheck" , pageApi . onScriptUpdateCheck ) ;
136- } , [ mRecords , paramSite , subscribeMessage , t ] ) ;
134+ // 每次render会重新定义 pageExecute 的 onScriptUpdateCheck
135+ pageExecute . onScriptUpdateCheck = onScriptUpdateCheck ;
137136
137+ // 只在第一次render执行
138138 const doInitial = ( ) => {
139139 // faster than useEffect
140140 setInitial ( true ) ;
141+ subscribeMessage ( "onScriptUpdateCheck" , ( msg ) => pageExecute . onScriptUpdateCheck ! ( msg ) ) ;
141142 scriptClient . fetchCheckUpdateStatus ( ) ;
142- // updateRecord();
143143 scriptClient . sendUpdatePageOpened ( ) ;
144144 } ;
145145
0 commit comments