Mercurial > hg > thermostat-ng > web-client
changeset 206:8b0dfd9701e1
Implement lifecycle hooks in jvm-memory
Reviewed-by: jkang
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-September/025050.html
author | Andrew Azores <aazores@redhat.com> |
---|---|
date | Fri, 15 Sep 2017 17:28:00 -0400 |
parents | 599c230e22e5 |
children | 07e0f128e67c |
files | src/app/components/jvm-info/jvm-memory/jvm-memory.controller.js src/app/components/jvm-info/jvm-memory/jvm-memory.controller.spec.js |
diffstat | 2 files changed, 37 insertions(+), 41 deletions(-) [+] |
line wrap: on
line diff
--- a/src/app/components/jvm-info/jvm-memory/jvm-memory.controller.js Fri Sep 15 13:36:13 2017 -0400 +++ b/src/app/components/jvm-info/jvm-memory/jvm-memory.controller.js Fri Sep 15 17:28:00 2017 -0400 @@ -31,7 +31,7 @@ import service from './jvm-memory.service.js'; class JvmMemoryController { - constructor ($stateParams, $scope, $interval, jvmMemoryService, metricToBigIntFilter, + constructor ($stateParams, $interval, jvmMemoryService, metricToBigIntFilter, bigIntToStringFilter, stringToNumberFilter, scaleBytesService, sanitizeService) { 'ngInject'; @@ -44,27 +44,36 @@ this._bigIntToString = bigIntToStringFilter; this._stringToNumber = stringToNumberFilter; this._scaleBytes = scaleBytesService; + } + $onInit () { this.metaspaceData = { used: 0, total: 0 }; - this.metaspaceConfig = { chartId: 'metaspaceChart', units: 'B' }; - this.spaceConfigs = []; - this.generationData = {}; - $scope.$on('$destroy', () => this.cancel()); + this._refreshRate = 2000; - this.refreshRate = '2000'; + this._start(); } - cancel () { + $onDestroy () { + this._stop(); + } + + _start () { + this._stop(); + this._update(); + this._refresh = this._interval(() => this._update(), this.refreshRate); + } + + _stop () { if (angular.isDefined(this._refresh)) { this._interval.cancel(this._refresh); delete this._refresh; @@ -72,11 +81,10 @@ } set refreshRate (val) { - this.cancel(); + this._stop(); this._refreshRate = parseInt(val); if (this._refreshRate > 0) { - this._refresh = this._interval(() => this.update(), this._refreshRate); - this.update(); + this._start(); } } @@ -105,7 +113,7 @@ ); } - update () { + _update () { this._jvmMemoryService.getJvmMemory(this.jvmId).then(resp => { let data = resp.data.response[0];
--- a/src/app/components/jvm-info/jvm-memory/jvm-memory.controller.spec.js Fri Sep 15 13:36:13 2017 -0400 +++ b/src/app/components/jvm-info/jvm-memory/jvm-memory.controller.spec.js Fri Sep 15 17:28:00 2017 -0400 @@ -29,15 +29,10 @@ beforeEach(angular.mock.module('jvmMemory.controller')); - let scope, interval, memSvc, scaleSvc, promise, ctrl, sanitizeSvc; + let interval, memSvc, scaleSvc, promise, ctrl, sanitizeSvc; beforeEach(inject($controller => { 'ngInject'; - scope = { - $on: sinon.spy(), - $watch: sinon.spy() - }; - interval = sinon.stub().returns('interval-sentinel'); interval.cancel = sinon.spy(); @@ -60,20 +55,20 @@ ctrl = $controller('JvmMemoryController', { $stateParams: { jvmId: 'foo-jvmId' }, - $scope: scope, $interval: interval, jvmMemoryService: memSvc, scaleBytesService: scaleSvc, sanitizeService: sanitizeSvc }); + ctrl.$onInit(); - sinon.spy(ctrl, 'update'); - sinon.spy(ctrl, 'cancel'); + sinon.spy(ctrl, '_update'); + sinon.spy(ctrl, '_stop'); })); afterEach(() => { - ctrl.update.restore(); - ctrl.cancel.restore(); + ctrl._update.restore(); + ctrl._stop.restore(); }); it('should exist', () => { @@ -113,45 +108,38 @@ }); it('should disable when set refreshRate is called with a non-positive value', () => { - ctrl.cancel.should.not.be.called(); - ctrl.update.should.not.be.called(); + ctrl._stop.should.not.be.called(); + ctrl._update.should.not.be.called(); ctrl.refreshRate = -1; - ctrl.cancel.should.be.calledOnce(); - ctrl.update.should.not.be.called(); + ctrl._stop.should.be.calledOnce(); + ctrl._update.should.not.be.called(); ctrl.should.not.have.ownProperty('_refresh'); }); - it('should call controller#update() on refresh', () => { + it('should call controller#_update() on refresh', () => { ctrl.refreshRate = 1; let func = interval.args[0][0]; - let callCount = ctrl.update.callCount; + let callCount = ctrl._update.callCount; func(); - ctrl.update.callCount.should.equal(callCount + 1); + ctrl._update.callCount.should.equal(callCount + 1); }); describe('ondestroy handler', () => { - it('should exist', () => { - scope.$on.should.be.calledWith(sinon.match('$destroy'), sinon.match.func); - }); - it('should cancel refresh', () => { - ctrl._refresh = 'interval-sentinel'; - let func = scope.$on.args[0][1]; - func(); + ctrl.$onDestroy(); interval.cancel.should.be.calledWith('interval-sentinel'); }); - it('should do nothing if refresh is undefined', () => { - ctrl._refresh = undefined; - let func = scope.$on.args[0][1]; - func(); + it('should do nothing if _refresh is undefined', () => { + delete ctrl._refresh; + ctrl.$onDestroy(); interval.cancel.should.not.be.called(); }); }); - describe('update', () => { + describe('_update', () => { let data, func; beforeEach(() => { func = promise.then.args[0][0];