Mailspring/src/priority-ui-coordinator.coffee
Ben Gotow 886328ff7a bump(react): 0.13.2 => 0.14.7
Great breakdown of React changes here:
https://github.com/facebook/react/blob/master/CHANGELOG.md#0140-october-7-2015

Due to deprecation warnings, I don't think this will break third-party extensions unless they were doing really bad things.
2016-03-29 01:43:12 -07:00

43 lines
1.3 KiB
CoffeeScript

{generateTempId} = require './flux/models/utils'
# A small object that keeps track of the current animation state of the
# application. You can use it to defer work until animations have finished.
# Integrated with our fork of ReactCSSTransitionGroup
#
# PriorityUICoordinator.settle.then ->
# # Do something expensive
#
class PriorityUICoordinator
constructor: ->
@tasks = {}
@settle = Promise.resolve()
setInterval(( => @detectOrphanedTasks()), 1000)
beginPriorityTask: ->
if Object.keys(@tasks).length is 0
@settle = new Promise (resolve, reject) =>
@settlePromiseResolve = resolve
id = generateTempId()
@tasks[id] = Date.now()
id
endPriorityTask: (id) ->
throw new Error("You must provide a task id to endPriorityTask") unless id
delete @tasks[id]
if Object.keys(@tasks).length is 0
@settlePromiseResolve() if @settlePromiseResolve
@settlePromiseResolve = null
detectOrphanedTasks: ->
now = Date.now()
threshold = 15000 # milliseconds
for id, timestamp of @tasks
if now - timestamp > threshold
console.log("PriorityUICoordinator detected oprhaned priority task lasting #{threshold}ms. Ending.")
@endPriorityTask(id)
busy: ->
Object.keys(@tasks).length > 0
module.exports = new PriorityUICoordinator()