@@ -16,80 +16,66 @@ try {
1616
1717export const BACKGROUND_TASK_IDENTIFIER = 'background-fetch-task' ; // legacy id retained
1818
19- // Track definition across Fast Refresh cycles
20- let taskDefined = ( globalThis as any ) . __CODEBUILDER_BACKGROUND_TASK_DEFINED__ === true ;
21- function defineBackgroundTask ( ) {
22- if ( taskDefined ) return ;
19+ // IMPORTANT: TaskManager.defineTask must run in the global scope of the JS bundle.
20+ // Do NOT guard with global flags; Fast Refresh can keep globals while TaskManager resets.
21+ const handler = async ( ) => {
2322 try {
24- const handler = async ( ) => {
25- try {
26- const startedAt = new Date ( ) . toISOString ( ) ;
27- console . log ( '[BackgroundTask] Started' , startedAt ) ;
28- const response = await fetch ( 'https://api.codebuilder.org/jobs/fetch' ) ;
29- const contentType = response . headers . get ( 'content-type' ) || '' ;
30-
31- if ( ! response . ok ) {
32- const text = await response . text ( ) ;
33- console . error ( '[BackgroundTask] HTTP error' , response . status , text . slice ( 0 , 200 ) ) ;
34- return BackgroundTask . BackgroundTaskResult . Failed ;
35- }
36-
37- let data : unknown ;
38- if ( contentType . includes ( 'application/json' ) ) {
39- data = await response . json ( ) ;
40- } else {
41- const text = await response . text ( ) ;
42- console . warn ( '[BackgroundTask] Non-JSON response' , text . slice ( 0 , 200 ) ) ;
43- data = text ;
44- }
23+ const startedAt = new Date ( ) . toISOString ( ) ;
24+ console . log ( '[BackgroundTask] Started' , startedAt ) ;
25+ const response = await fetch ( 'https://api.codebuilder.org/jobs/fetch' ) ;
26+ const contentType = response . headers . get ( 'content-type' ) || '' ;
4527
46- console . log ( '[BackgroundTask] Success fetched data snapshot type:' , typeof data ) ;
47- return BackgroundTask . BackgroundTaskResult . Success ;
48- } catch ( error ) {
49- console . error ( '[BackgroundTask] Failed' , error ) ;
50- return BackgroundTask . BackgroundTaskResult . Failed ;
51- }
52- } ;
53- // Prefer TaskManager; some libs might also expose a define API
54- if ( typeof ( BackgroundTask as any ) . defineTask === 'function' ) {
55- try {
56- ( BackgroundTask as any ) . defineTask ( BACKGROUND_TASK_IDENTIFIER , handler ) ;
57- } catch ( btErr : any ) {
58- const m = btErr ?. message || '' ;
59- if ( ! / a l r e a d y d e f i n e d / i. test ( m ) ) {
60- console . warn ( '[BackgroundTask] BackgroundTask.defineTask failed, falling back to TaskManager.defineTask' , m ) ;
61- }
62- }
28+ if ( ! response . ok ) {
29+ const text = await response . text ( ) ;
30+ console . error ( '[BackgroundTask] HTTP error' , response . status , text . slice ( 0 , 200 ) ) ;
31+ return BackgroundTask . BackgroundTaskResult . Failed ;
6332 }
64- TaskManager . defineTask ( BACKGROUND_TASK_IDENTIFIER , handler ) ;
65- taskDefined = true ;
66- ( globalThis as any ) . __CODEBUILDER_BACKGROUND_TASK_DEFINED__ = true ;
67- console . log ( '[BackgroundTask] Task definition ensured' ) ;
68- } catch ( e ) {
69- // If already defined, ignore; otherwise log
70- const msg = ( e as any ) ?. message || '' ;
71- if ( ! / a l r e a d y d e f i n e d / i. test ( msg ) ) {
72- console . warn ( '[BackgroundTask] defineTask error' , msg ) ;
33+
34+ let data : unknown ;
35+ if ( contentType . includes ( 'application/json' ) ) {
36+ data = await response . json ( ) ;
7337 } else {
74- taskDefined = true ; // treat as defined
75- ( globalThis as any ) . __CODEBUILDER_BACKGROUND_TASK_DEFINED__ = true ;
38+ const text = await response . text ( ) ;
39+ console . warn ( '[BackgroundTask] Non-JSON response' , text . slice ( 0 , 200 ) ) ;
40+ data = text ;
7641 }
42+
43+ console . log ( '[BackgroundTask] Success fetched data snapshot type:' , typeof data ) ;
44+ return BackgroundTask . BackgroundTaskResult . Success ;
45+ } catch ( error ) {
46+ console . error ( '[BackgroundTask] Failed' , error ) ;
47+ return BackgroundTask . BackgroundTaskResult . Failed ;
7748 }
78- }
49+ } ;
7950
80- // Ensure definition at module load
81- defineBackgroundTask ( ) ;
51+ try {
52+ TaskManager . defineTask ( BACKGROUND_TASK_IDENTIFIER , handler ) ;
53+ console . log ( '[BackgroundTask] Task definition ensured' ) ;
54+ } catch ( e ) {
55+ const msg = ( e as any ) ?. message || '' ;
56+ if ( / a l r e a d y d e f i n e d / i. test ( msg ) ) {
57+ // Safe to ignore on reload
58+ console . log ( '[BackgroundTask] Task already defined' ) ;
59+ } else {
60+ console . warn ( '[BackgroundTask] defineTask error' , msg ) ;
61+ }
62+ }
8263
8364/**
8465 * Register background task (replaces deprecated expo-background-fetch).
8566 * minimumInterval is in MINUTES (platform minimum ~15 on Android; iOS may defer more).
8667 */
8768export async function registerBackgroundFetch ( minimumIntervalMinutes : number = 15 ) {
8869 try {
89- // Double-ensure task is defined before registering
90- defineBackgroundTask ( ) ;
91- // Small delay to let task registry settle (helps with Fast Refresh race conditions)
92- await new Promise ( res => setTimeout ( res , 25 ) ) ;
70+ // Small delay to let task registry settle (helps with Fast Refresh race conditions)
71+ await new Promise ( res => setTimeout ( res , 25 ) ) ;
72+
73+ const alreadyRegistered = await TaskManager . isTaskRegisteredAsync ( BACKGROUND_TASK_IDENTIFIER ) ;
74+ if ( alreadyRegistered ) {
75+ console . log ( '[BackgroundTask] Already registered' ) ;
76+ return true ;
77+ }
78+
9379 const status = await BackgroundTask . getStatusAsync ( ) ;
9480 if ( status !== BackgroundTask . BackgroundTaskStatus . Available ) {
9581 console . warn ( '[BackgroundTask] Not available, status=' , status ) ;
@@ -106,8 +92,7 @@ export async function registerBackgroundFetch(minimumIntervalMinutes: number = 1
10692 if ( / n o t d e f i n e d / i. test ( msg ) ) {
10793 console . log ( '[BackgroundTask] Retrying after ensure definition...' ) ;
10894 try {
109- defineBackgroundTask ( ) ;
110- await new Promise ( res => setTimeout ( res , 100 ) ) ;
95+ await new Promise ( res => setTimeout ( res , 100 ) ) ;
11196 await BackgroundTask . registerTaskAsync ( BACKGROUND_TASK_IDENTIFIER , {
11297 minimumInterval : Math . max ( 15 , minimumIntervalMinutes ) ,
11398 } ) ;
0 commit comments