travis-web/assets/scripts/lib/travis/chunk_buffer.coffee
Piotr Sarnacki b4022f5f67 A few improvements for ChunkBuffer
* insert available items at once
* allow to use pushObject on buffer itself
* add a test using arrayObserver to make sure that it fires only when
  elements are added to arrangedContent
2013-03-09 22:01:05 +01:00

58 lines
1.3 KiB
CoffeeScript

Travis.ChunkBuffer = Em.ArrayProxy.extend Ember.MutableEnumerable,
timeout: 15000
start: 0
next: 0
init: ->
@_super.apply this, arguments
@set('next', @get('start'))
if @get('content.length')
@get('queue.content').pushObjects @get('content').toArray()
arrangedContent: (->
[]
).property('content')
addObject: (obj) ->
@get('content').pushObject(obj)
removeObject: (obj) ->
@get('content').removeObject(obj)
replaceContent: (idx, amt, objects) ->
@get('content').replace(idx, amt, objects)
queue: (->
Em.ArrayProxy.create(Em.SortableMixin,
content: []
sortProperties: ['number']
sortAscending: true
)
).property()
contentArrayDidChange: (array, index, removedCount, addedCount) ->
console.log 'content array did change'
@_super.apply this, arguments
if addedCount
queue = @get('queue.content')
queue.pushObjects array.slice(index, index + addedCount)
@check()
check: ->
queue = @get('queue')
next = @get('next')
arrangedContent = @get('arrangedContent')
toPush = []
while queue.get('firstObject.number') == next
toPush.pushObject queue.shiftObject().get('content')
next += 1
arrangedContent.pushObjects toPush if toPush.length
@set('next', next)