forked from nylas/nylas-mail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriority-ui-coordinator.es6
60 lines (52 loc) · 1.56 KB
/
priority-ui-coordinator.es6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { generateTempId } from './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() {
this.tasks = {};
this.settle = Promise.resolve();
setInterval(() => this.detectOrphanedTasks(), 1000);
}
beginPriorityTask() {
if (Object.keys(this.tasks).length === 0) {
this.settle = new Promise((resolve) => {
this.settlePromiseResolve = resolve;
});
}
const id = generateTempId();
this.tasks[id] = Date.now();
return id;
}
endPriorityTask(id) {
if (!id) {
throw new Error("You must provide a task id to endPriorityTask");
}
delete this.tasks[id];
if (Object.keys(this.tasks).length === 0) {
if (this.settlePromiseResolve) {
this.settlePromiseResolve();
}
this.settlePromiseResolve = null;
}
}
detectOrphanedTasks() {
const now = Date.now();
const threshold = 15000; // milliseconds
for (const id of Object.keys(this.tasks)) {
const timestamp = this.tasks[id];
if (now - timestamp > threshold) {
console.log(`PriorityUICoordinator detected oprhaned priority task lasting ${threshold}ms. Ending.`);
this.endPriorityTask(id);
}
}
}
busy() {
return Object.keys(this.tasks).length > 0;
}
}
export default new PriorityUICoordinator();