@@ -32,60 +32,75 @@ export default function PrebuildLogs(props: PrebuildLogsProps) {
32
32
const [ logsEmitter ] = useState ( new EventEmitter ( ) ) ;
33
33
const [ prebuild , setPrebuild ] = useState < PrebuildWithStatus | undefined > ( ) ;
34
34
35
+ function handlePrebuildUpdate ( prebuild : PrebuildWithStatus ) {
36
+ if ( prebuild . info . buildWorkspaceId === props . workspaceId ) {
37
+ setPrebuild ( prebuild ) ;
38
+
39
+ // In case the Prebuild got "aborted" or "time(d)out" we want to user to proceed anyway
40
+ if ( props . onIgnorePrebuild && ( prebuild . status === "aborted" || prebuild . status === "timeout" ) ) {
41
+ props . onIgnorePrebuild ( ) ;
42
+ }
43
+ // TODO(gpl) We likely want to move the "happy path" logic (for status "available")
44
+ // here as well at some point. For that to work we need a "registerPrebuildUpdate(prebuildId)" API
45
+ }
46
+ }
47
+
35
48
useEffect ( ( ) => {
36
49
const disposables = new DisposableCollection ( ) ;
37
- setWorkspaceInstance ( undefined ) ;
38
50
( async ( ) => {
39
51
if ( ! props . workspaceId ) {
40
52
return ;
41
53
}
54
+ setWorkspaceInstance ( undefined ) ;
55
+ setPrebuild ( undefined ) ;
56
+
57
+ // Try get hold of a recent WorkspaceInfo
42
58
try {
43
59
const info = await getGitpodService ( ) . server . getWorkspace ( props . workspaceId ) ;
60
+ setWorkspaceInstance ( info ?. latestInstance ) ;
61
+ } catch ( err ) {
62
+ console . error ( err ) ;
63
+ setError ( err ) ;
64
+ }
65
+
66
+ // Try get hold of a recent Prebuild
67
+ try {
44
68
const pbws = await getGitpodService ( ) . server . findPrebuildByWorkspaceID ( props . workspaceId ) ;
45
- if ( info . latestInstance ) {
46
- setWorkspaceInstance ( info . latestInstance ) ;
47
- }
48
69
if ( pbws ) {
49
70
const foundPrebuild = await getGitpodService ( ) . server . getPrebuild ( pbws . id ) ;
50
- setPrebuild ( foundPrebuild ) ;
71
+ if ( foundPrebuild ) {
72
+ handlePrebuildUpdate ( foundPrebuild ) ;
73
+ }
51
74
}
52
- disposables . push (
53
- getGitpodService ( ) . registerClient ( {
54
- onInstanceUpdate : ( instance ) => {
55
- if ( props . workspaceId === instance . workspaceId ) {
56
- setWorkspaceInstance ( instance ) ;
57
- }
58
- } ,
59
- onWorkspaceImageBuildLogs : (
60
- info : WorkspaceImageBuild . StateInfo ,
61
- content ?: WorkspaceImageBuild . LogContent ,
62
- ) => {
63
- if ( ! content ) {
64
- return ;
65
- }
66
- logsEmitter . emit ( "logs" , content . text ) ;
67
- } ,
68
- onPrebuildUpdate ( update : PrebuildWithStatus ) {
69
- if ( update . info && update . info . buildWorkspaceId === props . workspaceId ) {
70
- setPrebuild ( update ) ;
71
-
72
- // In case the Prebuild got "aborted" or "time(d)out" we want to user to proceed anyway
73
- if (
74
- props . onIgnorePrebuild &&
75
- ( update . status === "aborted" || update . status === "timeout" )
76
- ) {
77
- props . onIgnorePrebuild ( ) ;
78
- }
79
- // TODO(gpl) We likely want to move the "happy path" logic (for status "available")
80
- // here as well at some point. For that to work we need a "registerPrebuildUpdate(prebuildId)" API
81
- }
82
- } ,
83
- } ) ,
84
- ) ;
85
75
} catch ( err ) {
86
76
console . error ( err ) ;
87
77
setError ( err ) ;
88
78
}
79
+
80
+ // Register for future updates
81
+ disposables . push (
82
+ getGitpodService ( ) . registerClient ( {
83
+ onInstanceUpdate : ( instance ) => {
84
+ if ( props . workspaceId === instance . workspaceId ) {
85
+ setWorkspaceInstance ( instance ) ;
86
+ }
87
+ } ,
88
+ onWorkspaceImageBuildLogs : (
89
+ info : WorkspaceImageBuild . StateInfo ,
90
+ content ?: WorkspaceImageBuild . LogContent ,
91
+ ) => {
92
+ if ( ! content ) {
93
+ return ;
94
+ }
95
+ logsEmitter . emit ( "logs" , content . text ) ;
96
+ } ,
97
+ onPrebuildUpdate ( update : PrebuildWithStatus ) {
98
+ if ( update . info ) {
99
+ handlePrebuildUpdate ( update ) ;
100
+ }
101
+ } ,
102
+ } ) ,
103
+ ) ;
89
104
} ) ( ) ;
90
105
return function cleanup ( ) {
91
106
disposables . dispose ( ) ;
0 commit comments