@@ -10,25 +10,23 @@ import React, {
1010import { wrap , Remote } from "comlink" ;
1111import { MyWorker } from "../../pyodide-worker/worker" ;
1212
13- type PyodideWorkerStatus =
14- | "idle"
15- | "starting"
16- | "preparing"
17- | "ready"
18- | "error" ;
13+ type WorkerState =
14+ | { status : "idle" }
15+ | { status : "starting" }
16+ | { status : "preparing" }
17+ | { status : "ready" }
18+ | { status : "error" ; error : Error } ;
1919
2020export type BitbakeSpec = {
2121 version : string ;
2222 url ?: string ;
2323} ;
2424
2525type PyodideWorkerContextValue = {
26- status : PyodideWorkerStatus ;
26+ workerState : WorkerState ;
2727 getClient : ( ) => Remote < MyWorker > | null ;
28- error : Error | null ;
2928 bitbakeSpec : BitbakeSpec ;
3029 bitbakeZipUrl : string ;
31- prepared : boolean ;
3230} ;
3331
3432const DEFAULT_BITBAKE_VERSION = "2.8.0" ;
@@ -61,9 +59,9 @@ export const PyodideWorkerProvider: React.FC<{
6159 [ normalizedSpec ]
6260 ) ;
6361
64- const [ status , setStatus ] = useState < PyodideWorkerStatus > ( "idle" ) ;
65- const [ error , setError ] = useState < Error | null > ( null ) ;
66- const [ prepared , setPrepared ] = useState < boolean > ( false ) ;
62+ const [ workerState , setWorkerState ] = useState < WorkerState > ( {
63+ status : "idle" ,
64+ } ) ;
6765
6866 const workerRef = useRef < Worker | null > ( null ) ;
6967 const clientRef = useRef < Remote < MyWorker > | null > ( null ) ;
@@ -72,8 +70,7 @@ export const PyodideWorkerProvider: React.FC<{
7270 let cancelled = false ;
7371
7472 const setup = async ( ) => {
75- setStatus ( "starting" ) ;
76- setError ( null ) ;
73+ setWorkerState ( { status : "starting" } ) ;
7774 clientRef . current = null ;
7875
7976 const worker = new Worker (
@@ -91,24 +88,21 @@ export const PyodideWorkerProvider: React.FC<{
9188 }
9289
9390 clientRef . current = api ;
94- setStatus ( "preparing" ) ;
95- setPrepared ( false ) ;
91+ setWorkerState ( { status : "preparing" } ) ;
9692
9793 await api . prepareBitbake ( bitbakeZipUrl , normalizedSpec . version ) ;
9894
9995 if ( cancelled ) {
10096 worker . terminate ( ) ;
10197 return ;
10298 }
103- setPrepared ( true ) ;
104- setStatus ( "ready" ) ;
99+ setWorkerState ( { status : "ready" } ) ;
105100 } catch ( err ) {
106101 if ( cancelled ) {
107102 return ;
108103 }
109104 clientRef . current = null ;
110- setError ( err as Error ) ;
111- setStatus ( "error" ) ;
105+ setWorkerState ( { status : "error" , error : err as Error } ) ;
112106 }
113107 } ;
114108
@@ -119,22 +113,20 @@ export const PyodideWorkerProvider: React.FC<{
119113 workerRef . current ?. terminate ( ) ;
120114 workerRef . current = null ;
121115 clientRef . current = null ;
122- setPrepared ( false ) ;
116+ setWorkerState ( { status : "idle" } ) ;
123117 } ;
124118 } , [ bitbakeZipUrl , normalizedSpec . version ] ) ;
125119
126120 const getClient = useCallback ( ( ) => clientRef . current , [ ] ) ;
127121
128122 const value = useMemo < PyodideWorkerContextValue > (
129123 ( ) => ( {
130- status ,
124+ workerState ,
131125 getClient,
132- error,
133126 bitbakeSpec : normalizedSpec ,
134127 bitbakeZipUrl,
135- prepared,
136128 } ) ,
137- [ bitbakeZipUrl , error , getClient , normalizedSpec , prepared , status ]
129+ [ bitbakeZipUrl , getClient , normalizedSpec , workerState ]
138130 ) ;
139131
140132 return (
0 commit comments