changeset 207:07e0f128e67c

Implement lifecycle hooks in jvm-gc 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:29:12 -0400
parents 8b0dfd9701e1
children 0f0ca4f48c22
files src/app/components/jvm-info/jvm-gc/jvm-gc.controller.js src/app/components/jvm-info/jvm-gc/jvm-gc.controller.spec.js
diffstat 2 files changed, 20 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/src/app/components/jvm-info/jvm-gc/jvm-gc.controller.js	Fri Sep 15 17:28:00 2017 -0400
+++ b/src/app/components/jvm-info/jvm-gc/jvm-gc.controller.js	Fri Sep 15 17:29:12 2017 -0400
@@ -30,7 +30,7 @@
 import service from './jvm-gc.service.js';
 
 class JvmGcController {
-  constructor ($stateParams, $scope, $interval, dateFilter, DATE_FORMAT,
+  constructor ($stateParams, $interval, dateFilter, DATE_FORMAT,
     metricToNumberFilter, jvmGcService, sanitizeService, $translate) {
     'ngInject';
     this.jvmId = $stateParams.jvmId;
@@ -41,21 +41,29 @@
     this.jvmGcService = jvmGcService;
     this.sanitizeService = sanitizeService;
     this.translate = $translate;
+  }
 
-    $scope.$on('$destroy', () => this.stop());
-
+  $onInit () {
     this.collectors = [];
     this.chartConfigs = {};
     this.chartData = {};
     this.collectorData = new Map();
     this.constructChartData();
 
-    this.refreshRate = '1000';
-    this.dataAgeLimit = '30000';
+    this._refreshRate = 1000;
+    this._dataAgeLimit = 30000;
+
+    this.start();
+  }
+
+  $onDestroy () {
+    this.stop();
   }
 
   start () {
-    this.refreshRate = this._refreshRate.toString();
+    this.stop();
+    this.update();
+    this._refresh = this.interval(() => this.update(), this.refreshRate);
   }
 
   stop () {
@@ -69,8 +77,7 @@
     this.stop();
     this._refreshRate = parseInt(val);
     if (this._refreshRate > 0) {
-      this._refresh = this.interval(() => this.update(), this._refreshRate);
-      this.update();
+      this.start();
     }
   }
 
--- a/src/app/components/jvm-info/jvm-gc/jvm-gc.controller.spec.js	Fri Sep 15 17:28:00 2017 -0400
+++ b/src/app/components/jvm-info/jvm-gc/jvm-gc.controller.spec.js	Fri Sep 15 17:29:12 2017 -0400
@@ -30,7 +30,7 @@
   beforeEach(angular.mock.module('app.filters'));
   beforeEach(angular.mock.module('jvmGc.controller'));
 
-  let scope, interval, dateFilterStub, dateFormatSpy, svc, promise, ctrl, translate, sanitizeService;
+  let interval, dateFilterStub, dateFormatSpy, svc, promise, ctrl, translate, sanitizeService;
   beforeEach(inject(($controller) => {
     'ngInject';
 
@@ -41,8 +41,6 @@
       }
     };
 
-    scope = { $on: sinon.spy() };
-
     interval = sinon.stub().returns('interval-sentinel');
     interval.cancel = sinon.spy();
 
@@ -61,7 +59,6 @@
 
     ctrl = $controller('JvmGcController', {
       $stateParams: { jvmId: 'foo-jvmId' },
-      $scope: scope,
       $interval: interval,
       dateFilter: dateFilterStub,
       DATE_FORMAT: dateFormatSpy,
@@ -69,6 +66,7 @@
       sanitizeService: sanitizeService,
       $translate: translate
     });
+    ctrl.$onInit();
   }));
 
   it('should exist', () => {
@@ -148,7 +146,7 @@
 
   it('should set interval on start', () => {
     interval.should.be.calledOnce();
-    interval.should.be.calledWith(sinon.match.func, 1000);
+    interval.should.be.calledWith(sinon.match.func, '1000');
     interval.cancel.should.not.be.called();
   });
 
@@ -410,21 +408,14 @@
   });
 
   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', () => {
       delete ctrl._refresh;
-      let func = scope.$on.args[0][1];
-      func();
+      ctrl.$onDestroy();
       interval.cancel.should.not.be.called();
     });
   });