Skip to content

Commit 6260a67

Browse files
cci: fix session close when all sections closed
1 parent b9c394e commit 6260a67

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/session/DeputyRootSession.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -385,25 +385,31 @@ export default class DeputyRootSession {
385385
// TODO: Do interface functions
386386
this.sections = [];
387387

388-
const foundSections: string[] = [];
388+
const activeSectionPromises = [];
389389
for ( const heading of this.casePage.findContributionSurveyHeadings() ) {
390390
const headingName = sectionHeadingName( heading );
391391

392392
if ( this.session.caseSections.indexOf( headingName ) !== -1 ) {
393-
foundSections.push( headingName );
394-
( () => {
395-
// Placed in self-executing function for asynchronicity.
396-
this.activateSection( this.casePage, heading );
397-
} )();
393+
activeSectionPromises.push(
394+
this.activateSection( this.casePage, heading )
395+
.then( v => v ? headingName : null )
396+
);
398397
} else {
399398
this.addSectionOverlay( this.casePage, heading );
400399
}
401400
}
402401

403402
// Strip missing sections from caseSections.
404-
this.session.caseSections = foundSections;
403+
this.session.caseSections = ( await Promise.all( activeSectionPromises ) )
404+
.filter( v => !!v );
405405
await DeputyRootSession.setSession( this.session );
406406

407+
if ( this.session.caseSections.length === 0 ) {
408+
// No sections re-opened. All of them might have been removed or closed already.
409+
// Close this entire session.
410+
await this.closeSession();
411+
}
412+
407413
res();
408414
} );
409415
} );
@@ -502,14 +508,15 @@ export default class DeputyRootSession {
502508
*
503509
* @param casePage
504510
* @param heading
511+
* @return `true` if the section was activated successfully
505512
*/
506513
async activateSection(
507514
casePage: DeputyCasePage,
508515
heading: ContributionSurveyHeading
509-
): Promise<void> {
516+
): Promise<boolean> {
510517
const el = new DeputyContributionSurveySection( casePage, heading );
511518
if ( !( await el.prepare() ) ) {
512-
return;
519+
return false;
513520
}
514521

515522
const sectionName = sectionHeadingName( heading );
@@ -522,6 +529,8 @@ export default class DeputyRootSession {
522529
await casePage.addActiveSection( sectionName );
523530

524531
heading.insertAdjacentElement( 'afterend', el.render() );
532+
533+
return true;
525534
}
526535

527536
/**

0 commit comments

Comments
 (0)