Mailspring/src/priority-ui-coordinator.coffee
Evan Morikawa fc4b3b56d7 refactor(utils): switch to regular underscore
Summary:
Fixes: T1334

remove final InboxApp references

move out all underscore-plus methods

Mass find and replace of underscore-plus

sed -i '' -- 's/underscore-plus/underscore/g' **/*.coffee
sed -i '' -- 's/underscore-plus/underscore/g' **/*.cjsx

Test Plan: edgehill --test

Reviewers: bengotow

Reviewed By: bengotow

Differential Revision: https://phab.nylas.com/D1534
2015-05-19 16:06:59 -07:00

44 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 TimeoutTransitionGroup
#
# 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()