@@ -61,18 +61,14 @@ type ProgressDataMap = Record<number, ProgressData | undefined>;
61
61
* and configure auto-switching behavior.
62
62
*/
63
63
export class StagingAreaApi {
64
- sessionId : string ;
65
- _app : StagingAreaAppApi ;
66
- _subscriptions = new Set < ( ) => void > ( ) ;
64
+ /** The current session ID. */
65
+ _sessionId : string | null = null ;
67
66
68
- constructor ( sessionId : string , app : StagingAreaAppApi ) {
69
- this . sessionId = sessionId ;
70
- this . _app = app ;
67
+ /** The app API */
68
+ _app : StagingAreaAppApi | null = null ;
71
69
72
- this . _subscriptions . add ( this . _app . onItemsChanged ( this . onItemsChangedEvent ) ) ;
73
- this . _subscriptions . add ( this . _app . onQueueItemStatusChanged ( this . onQueueItemStatusChangedEvent ) ) ;
74
- this . _subscriptions . add ( this . _app . onInvocationProgress ( this . onInvocationProgressEvent ) ) ;
75
- }
70
+ /** A set of subscriptions to be cleaned up when we are finished with a session */
71
+ _subscriptions = new Set < ( ) => void > ( ) ;
76
72
77
73
/** Item ID of the last started item. Used for auto-switch on start. */
78
74
$lastStartedItemId = atom < number | null > ( null ) ;
@@ -136,7 +132,7 @@ export class StagingAreaApi {
136
132
/** Selects a queue item by ID. */
137
133
select = ( itemId : number ) => {
138
134
this . $selectedItemId . set ( itemId ) ;
139
- this . _app . onSelect ?.( itemId ) ;
135
+ this . _app ? .onSelect ?.( itemId ) ;
140
136
} ;
141
137
142
138
/** Selects the next item in the queue, wrapping to the first item if at the end. */
@@ -152,7 +148,7 @@ export class StagingAreaApi {
152
148
return ;
153
149
}
154
150
this . $selectedItemId . set ( nextItem . item_id ) ;
155
- this . _app . onSelectNext ?.( ) ;
151
+ this . _app ? .onSelectNext ?.( ) ;
156
152
} ;
157
153
158
154
/** Selects the previous item in the queue, wrapping to the last item if at the beginning. */
@@ -168,7 +164,7 @@ export class StagingAreaApi {
168
164
return ;
169
165
}
170
166
this . $selectedItemId . set ( prevItem . item_id ) ;
171
- this . _app . onSelectPrev ?.( ) ;
167
+ this . _app ? .onSelectPrev ?.( ) ;
172
168
} ;
173
169
174
170
/** Selects the first item in the queue. */
@@ -179,7 +175,7 @@ export class StagingAreaApi {
179
175
return ;
180
176
}
181
177
this . $selectedItemId . set ( first . item_id ) ;
182
- this . _app . onSelectFirst ?.( ) ;
178
+ this . _app ? .onSelectFirst ?.( ) ;
183
179
} ;
184
180
185
181
/** Selects the last item in the queue. */
@@ -190,7 +186,7 @@ export class StagingAreaApi {
190
186
return ;
191
187
}
192
188
this . $selectedItemId . set ( last . item_id ) ;
193
- this . _app . onSelectLast ?.( ) ;
189
+ this . _app ? .onSelectLast ?.( ) ;
194
190
} ;
195
191
196
192
/** Discards the currently selected item and selects the next available item. */
@@ -207,7 +203,7 @@ export class StagingAreaApi {
207
203
} else {
208
204
this . $selectedItemId . set ( null ) ;
209
205
}
210
- this . _app . onDiscard ?.( selectedItem . item ) ;
206
+ this . _app ? .onDiscard ?.( selectedItem . item ) ;
211
207
} ;
212
208
213
209
/** Whether the discard selected action is enabled. */
@@ -218,10 +214,23 @@ export class StagingAreaApi {
218
214
return true ;
219
215
} ) ;
220
216
217
+ /** Connects to the app, registering listeners and such */
218
+ connectToApp = ( sessionId : string , app : StagingAreaAppApi ) => {
219
+ if ( this . _sessionId !== sessionId ) {
220
+ this . cleanup ( ) ;
221
+ this . _sessionId = sessionId ;
222
+ }
223
+ this . _app = app ;
224
+
225
+ this . _subscriptions . add ( this . _app . onItemsChanged ( this . onItemsChangedEvent ) ) ;
226
+ this . _subscriptions . add ( this . _app . onQueueItemStatusChanged ( this . onQueueItemStatusChangedEvent ) ) ;
227
+ this . _subscriptions . add ( this . _app . onInvocationProgress ( this . onInvocationProgressEvent ) ) ;
228
+ } ;
229
+
221
230
/** Discards all items in the queue. */
222
231
discardAll = ( ) => {
223
232
this . $selectedItemId . set ( null ) ;
224
- this . _app . onDiscardAll ?.( ) ;
233
+ this . _app ? .onDiscardAll ?.( ) ;
225
234
} ;
226
235
227
236
/** Accepts the currently selected item if an image is available. */
@@ -235,7 +244,7 @@ export class StagingAreaApi {
235
244
if ( ! datum || ! datum . imageDTO ) {
236
245
return ;
237
246
}
238
- this . _app . onAccept ?.( selectedItem . item , datum . imageDTO ) ;
247
+ this . _app ? .onAccept ?.( selectedItem . item , datum . imageDTO ) ;
239
248
} ;
240
249
241
250
/** Whether the accept selected action is enabled. */
@@ -249,20 +258,20 @@ export class StagingAreaApi {
249
258
250
259
/** Sets the auto-switch mode. */
251
260
setAutoSwitch = ( mode : AutoSwitchMode ) => {
252
- this . _app . onAutoSwitchChange ?.( mode ) ;
261
+ this . _app ? .onAutoSwitchChange ?.( mode ) ;
253
262
} ;
254
263
255
264
/** Handles invocation progress events from the WebSocket. */
256
265
onInvocationProgressEvent = ( data : S [ 'InvocationProgressEvent' ] ) => {
257
- if ( data . destination !== this . sessionId ) {
266
+ if ( data . destination !== this . _sessionId ) {
258
267
return ;
259
268
}
260
269
setProgress ( this . $progressData , data ) ;
261
270
} ;
262
271
263
272
/** Handles queue item status change events from the WebSocket. */
264
273
onQueueItemStatusChangedEvent = ( data : S [ 'QueueItemStatusChangedEvent' ] ) => {
265
- if ( data . destination !== this . sessionId ) {
274
+ if ( data . destination !== this . _sessionId ) {
266
275
return ;
267
276
}
268
277
if ( data . status === 'completed' ) {
@@ -277,7 +286,7 @@ export class StagingAreaApi {
277
286
*/
278
287
this . $lastCompletedItemId . set ( data . item_id ) ;
279
288
}
280
- if ( data . status === 'in_progress' && this . _app . getAutoSwitch ( ) === 'switch_on_start' ) {
289
+ if ( data . status === 'in_progress' && this . _app ? .getAutoSwitch ( ) === 'switch_on_start' ) {
281
290
this . $lastStartedItemId . set ( data . item_id ) ;
282
291
}
283
292
} ;
@@ -327,7 +336,7 @@ export class StagingAreaApi {
327
336
for ( const item of items ) {
328
337
const datum = progressData [ item . item_id ] ;
329
338
330
- if ( this . $lastStartedItemId . get ( ) === item . item_id && this . _app . getAutoSwitch ( ) === 'switch_on_start' ) {
339
+ if ( this . $lastStartedItemId . get ( ) === item . item_id && this . _app ? .getAutoSwitch ( ) === 'switch_on_start' ) {
331
340
this . $selectedItemId . set ( item . item_id ) ;
332
341
this . $lastStartedItemId . set ( null ) ;
333
342
}
@@ -339,13 +348,13 @@ export class StagingAreaApi {
339
348
if ( ! outputImageName ) {
340
349
continue ;
341
350
}
342
- const imageDTO = await this . _app . getImageDTO ( outputImageName ) ;
351
+ const imageDTO = await this . _app ? .getImageDTO ( outputImageName ) ;
343
352
if ( ! imageDTO ) {
344
353
continue ;
345
354
}
346
355
347
356
// This is the load logic mentioned in the comment in the QueueItemStatusChangedEvent handler above.
348
- if ( this . $lastCompletedItemId . get ( ) === item . item_id && this . _app . getAutoSwitch ( ) === 'switch_on_finish' ) {
357
+ if ( this . $lastCompletedItemId . get ( ) === item . item_id && this . _app ? .getAutoSwitch ( ) === 'switch_on_finish' ) {
349
358
this . _app . loadImage ( imageDTO . image_url ) . then ( ( ) => {
350
359
this . $selectedItemId . set ( item . item_id ) ;
351
360
this . $lastCompletedItemId . set ( null ) ;
0 commit comments