From 2cb55af89006b5c215eee4df480c8922ec487167 Mon Sep 17 00:00:00 2001 From: projectmushroom Date: Fri, 11 Nov 2016 14:17:41 +0200 Subject: [PATCH 1/2] add 30sec refresh interval --- karma/test-controllers.coffee | 16 ++++++++++++++-- static/coffee/controllers.coffee | 14 ++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/karma/test-controllers.coffee b/karma/test-controllers.coffee index 350781aa0..f40f98052 100644 --- a/karma/test-controllers.coffee +++ b/karma/test-controllers.coffee @@ -335,9 +335,14 @@ describe('controllers:', () -> # Tests for MessagesController #======================================================================= describe('MessagesController', () -> + $intervalSpy = null beforeEach(() -> - $controller('MessagesController', {$scope: $scope}) + inject((_$interval_) -> + $intervalSpy = jasmine.createSpy('$interval', _$interval_).and.callThrough() + ) + + $controller('MessagesController', {$scope: $scope, $interval: $intervalSpy}) $inboxScope.init('inbox', serverTime) $scope.init() @@ -355,7 +360,7 @@ describe('controllers:', () -> expect($scope.inactiveLabels).toEqual([test.tea, test.coffee]) ) - it('loadOldItems', () -> + it('should loadOldItems and auto-refresh on interval', () -> fetchOld = spyOnPromise($q, $scope, MessageService, 'fetchOld') $scope.loadOldItems() @@ -366,12 +371,19 @@ describe('controllers:', () -> }, $scope.startTime, 1) fetchOld.resolve({results: [test.msg3, test.msg2], hasMore: true}) + + expect($intervalSpy).toHaveBeenCalledWith($scope.autoRefresh, 30000) expect($scope.items).toEqual([test.msg3, test.msg2]) expect($scope.oldItemsMore).toEqual(true) expect($scope.isInfiniteScrollEnabled()).toEqual(true) ) + it 'should cancel the intervals on destroy', -> + spyOn($intervalSpy, 'cancel') + $scope.$destroy() + expect($intervalSpy.cancel).toHaveBeenCalledWith($scope.autoRefresh) + it('getItemFilter', () -> filter = $scope.getItemFilter() expect(filter({archived: false})).toEqual(true) diff --git a/static/coffee/controllers.coffee b/static/coffee/controllers.coffee index f6059c8d0..de2960e8b 100644 --- a/static/coffee/controllers.coffee +++ b/static/coffee/controllers.coffee @@ -216,7 +216,7 @@ controllers.controller('BaseItemsController', ['$scope', 'UtilsService', ($scope #============================================================================ # Incoming messages controller #============================================================================ -controllers.controller('MessagesController', ['$scope', '$timeout', '$uibModal', '$controller', 'CaseService', 'MessageService', 'PartnerService', 'UserService', 'UtilsService', ($scope, $timeout, $uibModal, $controller, CaseService, MessageService, PartnerService, UserService, UtilsService) -> +controllers.controller('MessagesController', ['$scope', '$timeout', '$interval', '$uibModal', '$controller', 'CaseService', 'MessageService', 'PartnerService', 'UserService', 'UtilsService', ($scope, $timeout, $interval, $uibModal, $controller, CaseService, MessageService, PartnerService, UserService, UtilsService) -> $controller('BaseItemsController', {$scope: $scope}) $scope.advancedSearch = false @@ -270,7 +270,17 @@ controllers.controller('MessagesController', ['$scope', '$timeout', '$uibModal', ) $scope.fetchOldItems = (search, startTime, page) -> - return MessageService.fetchOld(search, startTime, page) + $scope.autoRefresh = -> + MessageService.fetchOld(search, startTime, page).then((data) -> + $scope.items = data.results + $scope.oldItemsMore = data.hasMore + $scope.oldItemsLoading = false + ) + $scope.autoRefresh() + $interval($scope.autoRefresh, 30000) + + $scope.$on '$destroy', -> + $interval.cancel($scope.autoRefresh) $scope.onExpandMessage = (message) -> $scope.expandedMessageId = message.id From 8b5229f79f12d6caa4d5cc23d27068e425d82be2 Mon Sep 17 00:00:00 2001 From: projectmushroom Date: Mon, 14 Nov 2016 16:25:17 +0200 Subject: [PATCH 2/2] change interval to 10sec --- karma/test-controllers.coffee | 2 +- static/coffee/controllers.coffee | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/karma/test-controllers.coffee b/karma/test-controllers.coffee index f40f98052..a01d11455 100644 --- a/karma/test-controllers.coffee +++ b/karma/test-controllers.coffee @@ -372,7 +372,7 @@ describe('controllers:', () -> fetchOld.resolve({results: [test.msg3, test.msg2], hasMore: true}) - expect($intervalSpy).toHaveBeenCalledWith($scope.autoRefresh, 30000) + expect($intervalSpy).toHaveBeenCalledWith($scope.autoRefresh, 10000) expect($scope.items).toEqual([test.msg3, test.msg2]) expect($scope.oldItemsMore).toEqual(true) diff --git a/static/coffee/controllers.coffee b/static/coffee/controllers.coffee index de2960e8b..cab6db19f 100644 --- a/static/coffee/controllers.coffee +++ b/static/coffee/controllers.coffee @@ -277,7 +277,8 @@ controllers.controller('MessagesController', ['$scope', '$timeout', '$interval', $scope.oldItemsLoading = false ) $scope.autoRefresh() - $interval($scope.autoRefresh, 30000) + $interval.cancel($scope.autoRefresh) + $interval($scope.autoRefresh, 10000) $scope.$on '$destroy', -> $interval.cancel($scope.autoRefresh)