Skip to content

Commit edbd2d9

Browse files
authored
Merge pull request #2810 from adumesny/master
ng memory leak
2 parents 1339a8b + 67175b3 commit edbd2d9

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

angular/projects/demo/src/app/app.component.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@
101101
</div>
102102

103103
<div *ngIf="show===8">
104-
<p>load() memory leak test</p>
104+
<p>load() + clear() to memory leak test between the two</p>
105105
<button (click)="clearGrid()">Clear</button>
106-
<button (click)="load(sub0)">load 0</button>
106+
<button (click)="load(sub0)">load</button>
107107
<button (click)="load(sub2)">load 2</button>
108-
<gridstack [options]="gridOptionsFull"></gridstack>
108+
<gridstack [options]="gridOptions"></gridstack>
109109
</div>
110110

111111
<div class="grid-container"></div>

angular/projects/demo/src/app/dummy.component.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
// dummy testing component that will be grid items content
77

88
import { Component, OnDestroy, Input } from '@angular/core';
9+
10+
// local testing
11+
// import { BaseWidget } from './base-widget';
12+
// import { NgCompInputs } from './gridstack.component';
913
import { BaseWidget, NgCompInputs } from 'gridstack/dist/angular';
1014

1115
@Component({

angular/projects/lib/src/lib/gridstack-item.component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ export class GridstackItemComponent implements OnDestroy {
7272
}
7373

7474
public ngOnDestroy(): void {
75-
delete this.ref;
75+
this.clearOptions();
76+
delete this.childWidget
7677
delete this.el._gridItemComp;
78+
delete this.container;
79+
delete this.ref;
7780
}
7881
}

angular/projects/lib/src/lib/gridstack.component.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
import { AfterContentInit, Component, ContentChildren, ElementRef, EventEmitter, Input,
77
OnDestroy, OnInit, Output, QueryList, Type, ViewChild, ViewContainerRef, reflectComponentType, ComponentRef } from '@angular/core';
8-
import { Subject } from 'rxjs';
9-
import { takeUntil } from 'rxjs/operators';
8+
import { Subscription } from 'rxjs';
109
import { GridHTMLElement, GridItemHTMLElement, GridStack, GridStackNode, GridStackOptions, GridStackWidget } from 'gridstack';
1110

1211
import { GridItemCompHTMLElement, GridstackItemComponent } from './gridstack-item.component';
@@ -119,8 +118,8 @@ export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
119118

120119
private _options?: GridStackOptions;
121120
private _grid?: GridStack;
121+
private _sub: Subscription | undefined;
122122
private loaded?: boolean;
123-
private ngUnsubscribe: Subject<void> = new Subject();
124123

125124
constructor(
126125
// private readonly zone: NgZone,
@@ -142,21 +141,20 @@ export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
142141
/** wait until after all DOM is ready to init gridstack children (after angular ngFor and sub-components run first) */
143142
public ngAfterContentInit(): void {
144143
// track whenever the children list changes and update the layout...
145-
this.gridstackItems?.changes
146-
.pipe(takeUntil(this.ngUnsubscribe))
147-
.subscribe(() => this.updateAll());
144+
this._sub = this.gridstackItems?.changes.subscribe(() => this.updateAll());
148145
// ...and do this once at least unless we loaded children already
149146
if (!this.loaded) this.updateAll();
150147
this.hookEvents(this.grid);
151148
}
152149

153150
public ngOnDestroy(): void {
154-
delete this.ref;
155-
this.ngUnsubscribe.next();
156-
this.ngUnsubscribe.complete();
157-
this.grid?.destroy();
151+
this.unhookEvents(this._grid);
152+
this._sub?.unsubscribe();
153+
this._grid?.destroy();
158154
delete this._grid;
159155
delete this.el._gridComp;
156+
delete this.container;
157+
delete this.ref;
160158
}
161159

162160
/**
@@ -199,6 +197,11 @@ export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
199197
.on('resizestart', (event: Event, el: GridItemHTMLElement) => this.resizeStartCB.emit({event, el}))
200198
.on('resizestop', (event: Event, el: GridItemHTMLElement) => this.resizeStopCB.emit({event, el}))
201199
}
200+
201+
private unhookEvents(grid?: GridStack) {
202+
if (!grid) return;
203+
grid.off('added change disable drag dragstart dragstop dropped enable removed resize resizestart resizestop');
204+
}
202205
}
203206

204207
/**

0 commit comments

Comments
 (0)