@@ -8,6 +8,7 @@ import { MatDialog } from '@angular/material';
88import { ConnectDialogComponent } from '../connect-dialog/connect-dialog.component' ;
99import { ComponentTypeEnum , ComponentType } from 'src/app/model/models/instance' ;
1010import { GraphConfig } from '../GraphConfig' ;
11+ import { Subscription } from 'rxjs' ;
1112
1213@Component ( {
1314 selector : 'app-graph-view' ,
@@ -18,11 +19,11 @@ export class GraphViewComponent implements OnInit, OnDestroy {
1819 @ViewChild ( 'cy' ) cyDiv : ElementRef ;
1920 private cy : cytoscape . Core ;
2021 private config : GraphConfig ;
22+ private elementSubscription : Subscription ;
23+ private elementRemoveSubscription : Subscription ;
2124
2225 constructor ( private graphViewService : GraphViewService , public dialog : MatDialog ) {
23- if ( ! ( cytoscape as any ) . edgehandles ) {
24- cytoscape . use ( edgehandles ) ;
25- }
26+
2627 }
2728
2829
@@ -35,14 +36,16 @@ export class GraphViewComponent implements OnInit, OnDestroy {
3536
3637 ngOnDestroy ( ) {
3738 this . cy . destroy ( ) ;
39+ this . elementSubscription . unsubscribe ( ) ;
40+ this . elementRemoveSubscription . unsubscribe ( ) ;
3841 }
3942
4043 /**
4144 * Adds listeners to the graphViewService and describe how the
4245 * view should be updated if it receives any changes / updates.
4346 */
4447 private addChangeListener ( ) {
45- this . graphViewService . getElementObservable ( ) . subscribe ( ( update : ElementUpdate ) => {
48+ this . elementSubscription = this . graphViewService . getElementObservable ( ) . subscribe ( ( update : ElementUpdate ) => {
4649 if ( update . elements ) {
4750 if ( update . type === Actions . ADD ) {
4851 this . cy . add ( update . elements ) ;
@@ -65,7 +68,7 @@ export class GraphViewComponent implements OnInit, OnDestroy {
6568 }
6669 } ) ;
6770
68- this . graphViewService . getElementRemoveObservable ( ) . subscribe ( ( ids : Array < string > ) => {
71+ this . elementRemoveSubscription = this . graphViewService . getElementRemoveObservable ( ) . subscribe ( ( ids : Array < string > ) => {
6972 if ( ids ) {
7073 for ( let i = 0 ; i < ids . length ; i ++ ) {
7174 this . cy . remove ( this . cy . getElementById ( ids [ i ] ) ) ;
@@ -183,6 +186,9 @@ export class GraphViewComponent implements OnInit, OnDestroy {
183186 this . config . cytoscapeConfig . container = this . cyDiv . nativeElement ;
184187
185188 this . cy = cytoscape ( this . config . cytoscapeConfig ) ;
189+ if ( ! Object . getPrototypeOf ( this . cy ) . edgehandles ) {
190+ cytoscape . use ( edgehandles ) ;
191+ }
186192
187193 ( this . cy as any ) . edgehandles ( this . config . edgeDragConfig ) ;
188194 }
0 commit comments