I'm attempting to build a "notification system" of sorts for a broadcasting overlay. Essentially I catch events, add them to an array, then I run a function that observes that array. Each time an event is added, I run an animation that takes a given amount of time, remove that from the array and then move onto the next one.
I found that debounce
gets me a part of the way there. I'm able to add any number of events while the animations are running and it's able to empty the queue.
Problem being, I have to wait that specified time (in this case 5 seconds) before the first event is handled. However, when I set debounce to be immediate, everything breaks in that the first event will be immediately handled, but nothing else will.
# Pool handling.
pool: []
# Function adds events to the pool array.
addEventToPool: (event, data) ->
console.log "added #{event} with #{data} to pool!"
@get('pool').pushObject(data)
# Function that observes the pool array and runs debounce
# if there are any items in the pool.
observePool: (->
Ember.run.debounce(@, @handleEvent, 5000, false) if @get('pool').length
).observes('pool.[]')
# Event handling.
handleEvent: ->
pool = @get('pool')
object = pool.get('firstObject')
@set('payload', object)
Ember.$(".event-message__#{object.event}").addClass('active')
Ember.run.later (->
Ember.$(".event-message__#{object.event}").removeClass('active')
pool.removeObject(object)
), 2000
console.log "Number of remaining objects: #{pool.length}."
console.log "Objects remaining: #{JSON.stringify pool}."
I have a feeling that I need to move off of debounce
to fix this, but I'm not sure what that solution is.
Please let me know if you need any clarification!
Aucun commentaire:
Enregistrer un commentaire