travis-web/assets/scripts/app/controllers/repos.coffee
Piotr Sarnacki 4288e79044 Change Ember.run.later to setTimeout in periodic actions
It seems that running Ember.run.later periodically can cause CPU usage
to increase over time. Such increase adds up to already increasing CPU
usage because of data amount growing.

This commit tries to mitigate the issue by using setTimeout instead
2013-05-29 11:17:58 +03:00

79 lines
2.1 KiB
CoffeeScript

require 'travis/limited_array'
Travis.ReposController = Ember.ArrayController.extend
defaultTab: ( ->
if @get('currentUser.id')
'owned'
else
'recent'
).property('currentUser')
currentUserIdDidChange: (->
if @get('currentUser.id')
@activate('owned')
else if @get('tab') == 'owned'
@activate('recent')
).observes('currentUser.id')
isLoadedBinding: 'content.isLoaded'
needs: ['currentUser', 'repo']
currentUserBinding: 'controllers.currentUser'
selectedRepo: (->
# we need to observe also repo.content here, because we use
# ObjectProxy in repo controller
# TODO: get rid of ObjectProxy there
@get('controllers.repo.repo.content') || @get('controllers.repo.repo')
).property('controllers.repo.repo', 'controllers.repo.repo.content')
init: ->
@_super.apply this, arguments
setTimeout(@updateTimes.bind(this), Travis.INTERVALS.updateTimes)
recentRepos: (->
Travis.Repo.find()
Travis.LimitedArray.create
content: Em.ArrayProxy.extend(Em.SortableMixin).create(
sortProperties: ['sortOrder']
content: Travis.Repo.withLastBuild()
isLoadedBinding: 'content.isLoaded'
)
limit: 30
).property()
updateTimes: ->
if content = @get('content')
content.forEach (r) -> r.updateTimes()
setTimeout(@updateTimes.bind(this), Travis.INTERVALS.updateTimes)
activate: (tab, params) ->
@set('sortProperties', null)
tab ||= @get('defaultTab')
@set('tab', tab)
this["view#{$.camelize(tab)}"](params)
viewRecent: ->
@set('content', @get('recentRepos'))
viewOwned: ->
@set('sortProperties', ['sortOrder'])
@set('content', Travis.Repo.accessibleBy(@get('currentUser.login')))
viewSearch: (params) ->
@set('content', Travis.Repo.search(params.search))
searchObserver: (->
search = @get('search')
if search
@searchFor search
else
@activate 'recent'
'recent'
).observes('search')
searchFor: (phrase) ->
Ember.run.cancel(@searchLater) if @searchLater
@searchLater = Ember.run.later(this, (->
@activate 'search', search: phrase
), 500)