Skip to content

Commit

Permalink
dataservice: unsubscribe debouncing
Browse files Browse the repository at this point in the history
to avoid lots of subscribe/unscubscribe upon navigation, we only unsubscribe
after a delay

Signed-off-by: Pierre Tardy <tardyp@gmail.com>
  • Loading branch information
tardyp authored and Pierre Tardy committed Nov 4, 2015
1 parent d31bc58 commit a5163f9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 11 additions & 4 deletions www/data_module/src/services/data/data.service.coffee
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
class Data extends Provider
cache: true
unsubscribe_debounce_timeout: 60 * 1000 # 1 min
config = null
constructor: ->
config = cache: @cache
config =
cache: @cache
unsubscribe_debounce_timeout: @unsubscribe_debounce_timeout

### @ngInject ###
$get: ($log, $injector, $q, $window, Collection, restService, dataUtilsService, tabexService, indexedDBService, SPECIFICATION) ->
$get: ($log, $injector, $q, $window, $timeout,
Collection, restService, dataUtilsService, tabexService, indexedDBService, SPECIFICATION,) ->
return new class DataService
self = null
constructor: ->
Expand Down Expand Up @@ -104,9 +108,12 @@ class Data extends Provider

if scope? then @closeOnDestroy(scope)

# calls unsubscribe on each root classes
# calls unsubscribe on each root classes, but after a timeout
# so that we can keep data sync for data shared with next views, or if user goes back
close: ->
collections.forEach (c) -> c.unsubscribe?()
$timeout ->
collections.forEach (c) -> c.unsubscribe?()
, @unsubscribe_debounce_timeout

# closes the group when the scope is destroyed
closeOnDestroy: (scope) ->
Expand Down
4 changes: 3 additions & 1 deletion www/data_module/src/services/data/data.service.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ describe 'Data service', ->
$provide.constant '$state', new class State
reload: jasmine.createSpy('reload')

dataService = $q = $rootScope = $state = restService = indexedDBService = Collection = undefined
dataService = $q = $rootScope = $state = restService = indexedDBService = Collection = $timeout = undefined
injected = ($injector) ->
$q = $injector.get('$q')
$rootScope = $injector.get('$rootScope')
$state = $injector.get('$state')
$timeout = $injector.get('$timeout')
indexedDBService = $injector.get('indexedDBService')
restService = $injector.get('restService')
dataService = $injector.invoke(_dataServiceProvider.$get)
Expand Down Expand Up @@ -165,5 +166,6 @@ describe 'Data service', ->
expect(el1.unsubscribe).not.toHaveBeenCalled()
expect(el2.unsubscribe).not.toHaveBeenCalled()
dataAccessor.close()
$timeout.flush()
expect(el1.unsubscribe).toHaveBeenCalled()
expect(el2.unsubscribe).toHaveBeenCalled()

0 comments on commit a5163f9

Please sign in to comment.