@@ -160,7 +160,7 @@ export class WidgetModel extends Backbone.Model {
160
160
// Attributes should be initialized here, since user initialization may depend on it
161
161
this . widget_manager = options . widget_manager ;
162
162
this . model_id = options . model_id ;
163
- const comm = options . comm ;
163
+ this . comm = options . comm ;
164
164
165
165
this . views = Object . create ( null ) ;
166
166
this . state_change = Promise . resolve ( ) ;
@@ -174,27 +174,23 @@ export class WidgetModel extends Backbone.Model {
174
174
// _buffered_state_diff must be created *after* the super.initialize
175
175
// call above. See the note in the set() method below.
176
176
this . _buffered_state_diff = { } ;
177
+ }
177
178
178
- if ( comm ) {
179
- // Remember comm associated with the model.
180
- this . comm = comm ;
179
+ get comm ( ) {
180
+ return this . _comm ;
181
+ }
181
182
182
- // Hook comm messages up to model.
183
+ set comm ( comm : IClassicComm | undefined ) {
184
+ this . _comm = comm ;
185
+ if ( comm ) {
183
186
comm . on_close ( this . _handle_comm_closed . bind ( this ) ) ;
184
187
comm . on_msg ( this . _handle_comm_msg . bind ( this ) ) ;
185
-
186
- this . comm_live = true ;
187
- } else {
188
- this . comm_live = false ;
189
188
}
189
+ this . trigger ( 'comm_live_update' ) ;
190
190
}
191
191
192
- get comm_live ( ) : boolean {
193
- return this . _comm_live ;
194
- }
195
- set comm_live ( x ) {
196
- this . _comm_live = x ;
197
- this . trigger ( 'comm_live_update' ) ;
192
+ get comm_live ( ) {
193
+ return Boolean ( this . comm ) ;
198
194
}
199
195
200
196
/**
@@ -218,42 +214,42 @@ export class WidgetModel extends Backbone.Model {
218
214
*
219
215
* @returns - a promise that is fulfilled when all the associated views have been removed.
220
216
*/
221
- close ( comm_closed = false ) : Promise < void > {
217
+ async close ( comm_closed = false ) : Promise < void > {
222
218
// can only be closed once.
223
219
if ( this . _closed ) {
224
- return Promise . resolve ( ) ;
220
+ return ;
225
221
}
226
222
this . _closed = true ;
227
- if ( this . comm && ! comm_closed && this . comm_live ) {
223
+ if ( this . _comm && ! comm_closed ) {
228
224
try {
229
- this . comm . close ( ) ;
225
+ this . _comm . close ( ) ;
230
226
} catch ( err ) {
231
227
// Do Nothing
232
228
}
233
229
}
234
230
this . stopListening ( ) ;
235
231
this . trigger ( 'destroy' , this ) ;
236
- if ( this . comm ) {
237
- delete this . comm ;
238
- }
232
+ delete this . _comm ;
233
+
239
234
// Delete all views of this model
240
235
if ( this . views ) {
241
236
const views = Object . keys ( this . views ) . map ( ( id : string ) => {
242
237
return this . views ! [ id ] . then ( ( view ) => view . remove ( ) ) ;
243
238
} ) ;
244
239
delete this . views ;
245
- return Promise . all ( views ) . then ( ( ) => {
246
- return ;
247
- } ) ;
240
+ await Promise . all ( views ) ;
241
+ return ;
248
242
}
249
- return Promise . resolve ( ) ;
250
243
}
251
244
252
245
/**
253
246
* Handle when a widget comm is closed.
254
247
*/
255
248
_handle_comm_closed ( msg : KernelMessage . ICommCloseMsg ) : void {
256
- this . comm_live = false ;
249
+ if ( ! this . comm ) {
250
+ return ;
251
+ }
252
+ this . comm = undefined ;
257
253
this . trigger ( 'comm:close' ) ;
258
254
if ( ! this . _closed ) {
259
255
this . close ( true ) ;
@@ -642,7 +638,7 @@ export class WidgetModel extends Backbone.Model {
642
638
* This invokes a Backbone.Sync.
643
639
*/
644
640
save_changes ( callbacks ?: { } ) : void {
645
- if ( this . comm_live ) {
641
+ if ( this . comm ) {
646
642
const options : any = { patch : true } ;
647
643
if ( callbacks ) {
648
644
options . callbacks = callbacks ;
@@ -728,11 +724,9 @@ export class WidgetModel extends Backbone.Model {
728
724
model_id : string ;
729
725
views ?: { [ key : string ] : Promise < WidgetView > } ;
730
726
state_change : Promise < any > ;
731
- comm ?: IClassicComm ;
732
727
name : string ;
733
728
module : string ;
734
-
735
- private _comm_live : boolean ;
729
+ private _comm ?: IClassicComm ;
736
730
private _closed : boolean ;
737
731
private _state_lock : any ;
738
732
private _buffered_state_diff : any ;
0 commit comments