@@ -16,9 +16,14 @@ interface WorkspaceItemContext {
16
16
updatedTemplate ?: TypesGen . Template
17
17
}
18
18
19
- type WorkspaceItemEvent = {
20
- type : "UPDATE_VERSION"
21
- }
19
+ type WorkspaceItemEvent =
20
+ | {
21
+ type : "UPDATE_VERSION"
22
+ }
23
+ | {
24
+ type : "UPDATE_DATA"
25
+ data : TypesGen . Workspace
26
+ }
22
27
23
28
export const workspaceItemMachine = createMachine (
24
29
{
@@ -40,6 +45,7 @@ export const workspaceItemMachine = createMachine(
40
45
} ,
41
46
tsTypes : { } as import ( "./workspacesXService.typegen" ) . Typegen0 ,
42
47
type : "parallel" ,
48
+
43
49
states : {
44
50
updateVersion : {
45
51
initial : "idle" ,
@@ -53,6 +59,9 @@ export const workspaceItemMachine = createMachine(
53
59
// don't need to display an extra spinner.
54
60
actions : [ "assignQueuedStatus" , "displayUpdatingVersionMessage" ] ,
55
61
} ,
62
+ UPDATE_DATA : {
63
+ actions : "assignUpdatedData" ,
64
+ } ,
56
65
} ,
57
66
} ,
58
67
gettingUpdatedTemplate : {
@@ -64,7 +73,7 @@ export const workspaceItemMachine = createMachine(
64
73
target : "restartingWorkspace" ,
65
74
} ,
66
75
onError : {
67
- target : "error " ,
76
+ target : "idle " ,
68
77
actions : "displayUpdateVersionError" ,
69
78
} ,
70
79
} ,
@@ -78,7 +87,7 @@ export const workspaceItemMachine = createMachine(
78
87
target : "waitingToBeUpdated" ,
79
88
} ,
80
89
onError : {
81
- target : "error " ,
90
+ target : "idle " ,
82
91
actions : "displayUpdateVersionError" ,
83
92
} ,
84
93
} ,
@@ -99,18 +108,12 @@ export const workspaceItemMachine = createMachine(
99
108
actions : [ "assignUpdatedData" ] ,
100
109
} ,
101
110
{
102
- target : "success " ,
111
+ target : "idle " ,
103
112
actions : [ "assignUpdatedData" , "displayUpdatedSuccessMessage" ] ,
104
113
} ,
105
114
] ,
106
115
} ,
107
116
} ,
108
- error : {
109
- type : "final" ,
110
- } ,
111
- success : {
112
- type : "final" ,
113
- } ,
114
117
} ,
115
118
} ,
116
119
} ,
@@ -187,7 +190,7 @@ interface WorkspacesContext {
187
190
getWorkspacesError ?: Error | unknown
188
191
}
189
192
190
- type WorkspacesEvent = { type : "SET_FILTER " ; query : string } | { type : "UPDATE_VERSION" ; workspaceId : string }
193
+ type WorkspacesEvent = { type : "GET_WORKSPACES " ; query : string } | { type : "UPDATE_VERSION" ; workspaceId : string }
191
194
192
195
export const workspacesMachine = createMachine (
193
196
{
@@ -201,42 +204,54 @@ export const workspacesMachine = createMachine(
201
204
}
202
205
} ,
203
206
} ,
204
- id : "workspaceState" ,
205
- initial : "ready" ,
206
- states : {
207
- ready : {
208
- on : {
209
- SET_FILTER : "extractingFilter" ,
210
- UPDATE_VERSION : {
211
- actions : "triggerUpdateVersion" ,
212
- } ,
213
- } ,
207
+ id : "workspacesState" ,
208
+ on : {
209
+ GET_WORKSPACES : {
210
+ actions : "assignFilter" ,
211
+ target : "gettingWorkspaces" ,
214
212
} ,
215
- extractingFilter : {
216
- entry : "assignFilter" ,
217
- always : {
218
- target : "gettingWorkspaces" ,
219
- } ,
213
+ UPDATE_VERSION : {
214
+ actions : "triggerUpdateVersion" ,
215
+ } ,
216
+ } ,
217
+ initial : "idle" ,
218
+ states : {
219
+ idle : {
220
+ tags : [ "loading" ] ,
220
221
} ,
221
222
gettingWorkspaces : {
222
223
entry : "clearGetWorkspacesError" ,
223
224
invoke : {
224
225
src : "getWorkspaces" ,
225
226
id : "getWorkspaces" ,
226
- onDone : {
227
- target : "ready" ,
228
- actions : [ "assignWorkspaceRefs" , "clearGetWorkspacesError" ] ,
229
- } ,
227
+ onDone : [
228
+ {
229
+ target : "waitToRefreshWorkspaces" ,
230
+ actions : [ "assignWorkspaceRefs" ] ,
231
+ cond : "isEmpty" ,
232
+ } ,
233
+ {
234
+ target : "waitToRefreshWorkspaces" ,
235
+ actions : [ "updateWorkspaceRefs" ] ,
236
+ } ,
237
+ ] ,
230
238
onError : {
231
- target : "ready " ,
232
- actions : [ "assignGetWorkspacesError" , "clearWorkspaces" ] ,
239
+ target : "waitToRefreshWorkspaces " ,
240
+ actions : [ "assignGetWorkspacesError" ] ,
233
241
} ,
234
242
} ,
235
- tags : "loading" ,
243
+ } ,
244
+ waitToRefreshWorkspaces : {
245
+ after : {
246
+ 5000 : "gettingWorkspaces" ,
247
+ } ,
236
248
} ,
237
249
} ,
238
250
} ,
239
251
{
252
+ guards : {
253
+ isEmpty : ( context ) => ! context . workspaceRefs ,
254
+ } ,
240
255
actions : {
241
256
assignWorkspaceRefs : assign ( {
242
257
workspaceRefs : ( _ , event ) =>
@@ -251,7 +266,6 @@ export const workspacesMachine = createMachine(
251
266
getWorkspacesError : ( _ , event ) => event . data ,
252
267
} ) ,
253
268
clearGetWorkspacesError : ( context ) => assign ( { ...context , getWorkspacesError : undefined } ) ,
254
- clearWorkspaces : ( context ) => assign ( { ...context , workspaces : undefined } ) ,
255
269
triggerUpdateVersion : ( context , event ) => {
256
270
const workspaceRef = context . workspaceRefs ?. find ( ( ref ) => ref . id === event . workspaceId )
257
271
@@ -261,6 +275,44 @@ export const workspacesMachine = createMachine(
261
275
262
276
workspaceRef . send ( "UPDATE_VERSION" )
263
277
} ,
278
+ updateWorkspaceRefs : assign ( {
279
+ workspaceRefs : ( context , event ) => {
280
+ let workspaceRefs = context . workspaceRefs
281
+
282
+ if ( ! workspaceRefs ) {
283
+ throw new Error ( "No workspaces loaded." )
284
+ }
285
+
286
+ // Update the existent workspaces or create the new ones
287
+ for ( const data of event . data ) {
288
+ const ref = workspaceRefs . find ( ( ref ) => ref . id === data . id )
289
+
290
+ if ( ! ref ) {
291
+ workspaceRefs . push ( spawn ( workspaceItemMachine . withContext ( { data } ) , data . id ) )
292
+ } else {
293
+ ref . send ( { type : "UPDATE_DATA" , data } )
294
+ }
295
+ }
296
+
297
+ // Remove workspaces that were deleted
298
+ for ( const ref of workspaceRefs ) {
299
+ const refData = event . data . find ( ( workspaceData ) => workspaceData . id === ref . id )
300
+
301
+ // If there is no refData, it is because the workspace was deleted
302
+ if ( ! refData ) {
303
+ // Stop the actor before remove it from the array
304
+ if ( ref . stop ) {
305
+ ref . stop ( )
306
+ }
307
+
308
+ // Remove ref from the array
309
+ workspaceRefs = workspaceRefs . filter ( ( oldRef ) => oldRef . id === ref . id )
310
+ }
311
+ }
312
+
313
+ return workspaceRefs
314
+ } ,
315
+ } ) ,
264
316
} ,
265
317
services : {
266
318
getWorkspaces : ( context ) => API . getWorkspaces ( workspaceQueryToFilter ( context . filter ) ) ,
0 commit comments