@@ -369,14 +369,27 @@ export class ExperimentManager extends Service {
369369 }
370370
371371 // Return name for next cohort based on number of cohorts
372- getNextCohortName ( numCohorts = this . cohortList . length ) {
372+ getNextCohortName ( offset = 0 ) {
373373 const hasTransfer = this . sp . experimentService . stages . find (
374374 ( stage ) => stage . kind === StageKind . TRANSFER ,
375375 ) ;
376- if ( numCohorts > 0 || ! hasTransfer ) {
377- return `Cohort ${ String ( numCohorts ) . padStart ( 2 , '0' ) } ` ;
376+
377+ if ( this . cohortList . length === 0 && hasTransfer ) {
378+ return 'Lobby' ;
378379 }
379- return 'Lobby' ;
380+
381+ let maxNum = - 1 ;
382+ this . cohortList . forEach ( ( cohort ) => {
383+ const match = cohort . metadata . name . match ( / ^ C o h o r t ( \d + ) $ / ) ;
384+ if ( match ) {
385+ const num = parseInt ( match [ 1 ] , 10 ) ;
386+ if ( ! isNaN ( num ) && num > maxNum ) {
387+ maxNum = num ;
388+ }
389+ }
390+ } ) ;
391+
392+ return `Cohort ${ String ( maxNum + 1 + offset ) . padStart ( 2 , '0' ) } ` ;
380393 }
381394
382395 isFullCohort ( cohort : CohortConfig ) {
@@ -464,14 +477,13 @@ export class ExperimentManager extends Service {
464477 'alerts' ,
465478 ) ,
466479 ( snapshot ) => {
467- let changedDocs = snapshot . docChanges ( ) . map ( ( change ) => change . doc ) ;
468- if ( changedDocs . length === 0 ) {
469- changedDocs = snapshot . docs ;
470- }
471-
472- changedDocs . forEach ( ( doc ) => {
473- const data = doc . data ( ) as AlertMessage ;
474- this . alertMap [ data . id ] = data ;
480+ snapshot . docChanges ( ) . forEach ( ( change ) => {
481+ if ( change . type === 'removed' ) {
482+ delete this . alertMap [ change . doc . id ] ;
483+ } else {
484+ const data = change . doc . data ( ) as AlertMessage ;
485+ this . alertMap [ data . id ] = data ;
486+ }
475487 } ) ;
476488 } ,
477489 ) ,
@@ -487,21 +499,25 @@ export class ExperimentManager extends Service {
487499 'cohorts' ,
488500 ) ,
489501 ( snapshot ) => {
490- let changedDocs = snapshot . docChanges ( ) . map ( ( change ) => change . doc ) ;
491- if ( changedDocs . length === 0 ) {
492- changedDocs = snapshot . docs ;
493- }
494-
495- changedDocs . forEach ( ( doc ) => {
496- const data = doc . data ( ) as CohortConfig ;
497- this . cohortMap [ doc . id ] = data ;
498- this . currentCohortId = doc . id ;
502+ snapshot . docChanges ( ) . forEach ( ( change ) => {
503+ if ( change . type === 'removed' ) {
504+ delete this . cohortMap [ change . doc . id ] ;
505+ if ( this . currentCohortId === change . doc . id ) {
506+ this . currentCohortId = undefined ;
507+ }
508+ } else {
509+ const data = change . doc . data ( ) as CohortConfig ;
510+ this . cohortMap [ change . doc . id ] = data ;
511+ if ( ! this . currentCohortId ) {
512+ this . currentCohortId = change . doc . id ;
513+ }
514+ }
499515 } ) ;
500516
501517 // If multiple cohorts, show cohort list
502- if ( changedDocs . length > 1 ) {
518+ if ( Object . keys ( this . cohortMap ) . length > 1 ) {
503519 this . setShowCohortList ( true , true ) ;
504- } else if ( changedDocs . length === 0 ) {
520+ } else if ( Object . keys ( this . cohortMap ) . length === 0 ) {
505521 this . setShowCohortEditor ( true , true ) ;
506522 }
507523
@@ -523,17 +539,16 @@ export class ExperimentManager extends Service {
523539 where ( 'currentStatus' , '!=' , ParticipantStatus . DELETED ) ,
524540 ) ,
525541 ( snapshot ) => {
526- let changedDocs = snapshot . docChanges ( ) . map ( ( change ) => change . doc ) ;
527- if ( changedDocs . length === 0 ) {
528- changedDocs = snapshot . docs ;
529- }
530-
531- changedDocs . forEach ( ( doc ) => {
532- const data = {
533- agentConfig : null ,
534- ...doc . data ( ) ,
535- } as ParticipantProfileExtended ;
536- this . participantMap [ doc . id ] = data ;
542+ snapshot . docChanges ( ) . forEach ( ( change ) => {
543+ if ( change . type === 'removed' ) {
544+ delete this . participantMap [ change . doc . id ] ;
545+ } else {
546+ const data = {
547+ agentConfig : null ,
548+ ...change . doc . data ( ) ,
549+ } as ParticipantProfileExtended ;
550+ this . participantMap [ change . doc . id ] = data ;
551+ }
537552 } ) ;
538553
539554 this . isParticipantsLoading = false ;
@@ -554,17 +569,16 @@ export class ExperimentManager extends Service {
554569 where ( 'currentStatus' , '!=' , ParticipantStatus . DELETED ) ,
555570 ) ,
556571 ( snapshot ) => {
557- let changedDocs = snapshot . docChanges ( ) . map ( ( change ) => change . doc ) ;
558- if ( changedDocs . length === 0 ) {
559- changedDocs = snapshot . docs ;
560- }
561-
562- changedDocs . forEach ( ( doc ) => {
563- const data = {
564- agentConfig : null ,
565- ...doc . data ( ) ,
566- } as MediatorProfileExtended ;
567- this . mediatorMap [ doc . id ] = data ;
572+ snapshot . docChanges ( ) . forEach ( ( change ) => {
573+ if ( change . type === 'removed' ) {
574+ delete this . mediatorMap [ change . doc . id ] ;
575+ } else {
576+ const data = {
577+ agentConfig : null ,
578+ ...change . doc . data ( ) ,
579+ } as MediatorProfileExtended ;
580+ this . mediatorMap [ change . doc . id ] = data ;
581+ }
568582 } ) ;
569583
570584 this . isMediatorsLoading = false ;
@@ -586,14 +600,13 @@ export class ExperimentManager extends Service {
586600 ) ,
587601 ) ,
588602 ( snapshot ) => {
589- let changedDocs = snapshot . docChanges ( ) . map ( ( change ) => change . doc ) ;
590- if ( changedDocs . length === 0 ) {
591- changedDocs = snapshot . docs ;
592- }
593-
594- changedDocs . forEach ( ( doc ) => {
595- const data = doc . data ( ) as AgentPersonaConfig ;
596- this . agentPersonaMap [ doc . id ] = data ;
603+ snapshot . docChanges ( ) . forEach ( ( change ) => {
604+ if ( change . type === 'removed' ) {
605+ delete this . agentPersonaMap [ change . doc . id ] ;
606+ } else {
607+ const data = change . doc . data ( ) as AgentPersonaConfig ;
608+ this . agentPersonaMap [ change . doc . id ] = data ;
609+ }
597610 } ) ;
598611
599612 this . isAgentsLoading = false ;
@@ -710,7 +723,7 @@ export class ExperimentManager extends Service {
710723 cohortId,
711724 } ,
712725 ) ;
713- this . loadExperimentData ( this . experimentId ) ;
726+
714727 this . cohortEditing = undefined ;
715728 return response ;
716729 }
0 commit comments