Skip to content
This repository has been archived by the owner on Sep 8, 2020. It is now read-only.

fix layout memory leak #230

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/ui-layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ angular.module('ui.layout', [])
// regex to verify size is properly set to pixels or percent
var sizePattern = /\d+\s*(px|%)\s*$/i;

Layout.addLayout(ctrl);
var unsubscribeLayout = Layout.addLayout(ctrl);

ctrl.containers = [];
ctrl.movingSplitbar = null;
Expand Down Expand Up @@ -638,6 +638,10 @@ angular.module('ui.layout', [])
return -1;
};

ctrl.destroy = function() {
unsubscribeLayout();
};

return ctrl;
}])

Expand All @@ -662,6 +666,7 @@ angular.module('ui.layout', [])

scope.$on('$destroy', function() {
angular.element($window).unbind('resize', onResize);
ctrl.destroy();
});
}
};
Expand Down Expand Up @@ -1002,7 +1007,8 @@ angular.module('ui.layout', [])
var layouts = [],
collapsing = [],
toBeCollapsed = 0,
toggledDeffered = null;
toggledDeffered = null,
layoutId = 0;

function toggleContainer(container) {
try {
Expand All @@ -1015,8 +1021,12 @@ angular.module('ui.layout', [])

return {
addLayout: function (ctrl) {
ctrl.id = layouts.length;
ctrl.id = layoutId++;
layouts.push(ctrl);

return function() {
layouts.splice(layouts.indexOf(ctrl), 1);
}
},
addCollapsed: function(container) {
collapsing.push(container);
Expand Down
28 changes: 26 additions & 2 deletions test/uiLayoutCtrl.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@


describe('Controller: uiLayoutCtrl', function () {
var scope, $controller;
var scope, $controller, Layout;

beforeEach(function () {

module('ui.layout');

inject(function ($rootScope, _$controller_) {
inject(function ($rootScope, _$controller_, _Layout_) {
scope = $rootScope.$new();
$controller = _$controller_;
Layout = _Layout_;
});
});

Expand All @@ -36,5 +38,27 @@ describe('Controller: uiLayoutCtrl', function () {
expect(uic.isLayoutElement(tagContainer)).toEqual(true);
expect(uic.isLayoutElement(notUiEl)).toEqual(false);
});

it('destroy should call the unsubscribe function returned by Layout.addLayout', function () {
var unsubscribeLayout = jasmine.createSpy('unsubscribeLayout');

spyOn(Layout, 'addLayout');
Layout.addLayout.and.callFake(function(){
return unsubscribeLayout;
});

var uic = $controller('uiLayoutCtrl', {
$scope: scope,
$attrs: {},
$element: angular.element('<div></div>')
});

expect(Layout.addLayout).toHaveBeenCalledWith(uic);
expect(unsubscribeLayout).not.toHaveBeenCalled();

uic.destroy();

expect(unsubscribeLayout).toHaveBeenCalled();
});
});
});