@@ -112,7 +112,7 @@ export class PlotlyExpressChartModel extends ChartModel {
112112 /**
113113 * Map of table index to cleanup function for the subscription.
114114 */
115- subscriptionCleanupMap : Map < number , ( ) => void > = new Map ( ) ;
115+ subscriptionCleanupMap : Map < number , Set < ( ) => void > > = new Map ( ) ;
116116
117117 /**
118118 * Map of table index to map of column names to array of paths where the data should be replaced.
@@ -189,6 +189,21 @@ export class PlotlyExpressChartModel extends ChartModel {
189189 */
190190 requiredColumns : Set < string > = new Set ( ) ;
191191
192+ cleanupSubscriptions ( id : number ) : void {
193+ this . subscriptionCleanupMap . get ( id ) ?. forEach ( cleanup => {
194+ cleanup ( ) ;
195+ } ) ;
196+
197+ try {
198+ this . tableSubscriptionMap . get ( id ) ?. close ( ) ;
199+ } catch {
200+ // ignore
201+ }
202+
203+ this . subscriptionCleanupMap . delete ( id ) ;
204+ this . tableSubscriptionMap . delete ( id ) ;
205+ }
206+
192207 override getData ( ) : Partial < Data > [ ] {
193208 const hydratedData = [ ...this . plotlyData ] ;
194209
@@ -688,11 +703,11 @@ export class PlotlyExpressChartModel extends ChartModel {
688703 log . debug ( 'Updating downsampled table' , downsampleInfo ) ;
689704
690705 this . fireDownsampleStart ( null ) ;
691- this . subscriptionCleanupMap . get ( id ) ?.( ) ;
692- this . tableSubscriptionMap . get ( id ) ?. close ( ) ;
693- this . subscriptionCleanupMap . delete ( id ) ;
694- this . tableSubscriptionMap . delete ( id ) ;
706+
707+ this . cleanupSubscriptions ( id ) ;
708+
695709 this . tableReferenceMap . delete ( id ) ;
710+
696711 this . addTable ( id , oldDownsampleInfo . originalTable ) ;
697712 }
698713
@@ -807,24 +822,35 @@ export class PlotlyExpressChartModel extends ChartModel {
807822 const columns = table . columns . filter ( ( { name } ) => columnNames . has ( name ) ) ;
808823 const subscription = table . subscribe ( columns ) ;
809824 this . tableSubscriptionMap . set ( id , subscription ) ;
810- this . subscriptionCleanupMap . set (
811- id ,
825+
826+ if ( ! this . subscriptionCleanupMap . has ( id ) ) {
827+ this . subscriptionCleanupMap . set ( id , new Set ( ) ) ;
828+ }
829+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
830+ const cleanupSet = this . subscriptionCleanupMap . get ( id ) ! ;
831+
832+ cleanupSet . add (
812833 subscription . addEventListener < DhType . SubscriptionTableData > (
813834 this . dh . Table . EVENT_UPDATED ,
814835 e => this . handleFigureUpdated ( e , id )
815836 )
816837 ) ;
838+
839+ cleanupSet . add (
840+ table . addEventListener < DhType . Table > (
841+ this . dh . Table . EVENT_DISCONNECT ,
842+ e => this . fireDisconnect ( )
843+ )
844+ ) ;
817845 }
818846 }
819847
820848 removeTable ( id : number ) : void {
821- this . subscriptionCleanupMap . get ( id ) ?.( ) ;
822- this . tableSubscriptionMap . get ( id ) ?. close ( ) ;
849+ this . cleanupSubscriptions ( id ) ;
823850
824851 this . tableReferenceMap . delete ( id ) ;
852+
825853 this . downsampleMap . delete ( id ) ;
826- this . subscriptionCleanupMap . delete ( id ) ;
827- this . tableSubscriptionMap . delete ( id ) ;
828854 this . chartDataMap . delete ( id ) ;
829855 this . tableDataMap . delete ( id ) ;
830856 this . tableColumnReplacementMap . delete ( id ) ;
0 commit comments