-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
folders-screen-utils.js
77 lines (65 loc) · 1.82 KB
/
folders-screen-utils.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const Folder = require('lib/models/Folder.js');
const Setting = require('lib/models/Setting').default;
const shim = require('lib/shim').default;
class FoldersScreenUtils {
static async allForDisplay(options = {}) {
const orderDir = Setting.value('folders.sortOrder.reverse') ? 'DESC' : 'ASC';
const folderOptions = Object.assign(
{},
{
caseInsensitive: true,
order: [
{
by: 'title',
dir: orderDir,
},
],
},
options
);
let folders = await Folder.all(folderOptions);
if (Setting.value('folders.sortOrder.field') === 'last_note_user_updated_time') {
folders = await Folder.orderByLastModified(folders, orderDir);
}
if (Setting.value('showNoteCounts')) {
await Folder.addNoteCounts(folders,
Setting.value('showCompletedTodos'));
}
return folders;
}
static async refreshFolders() {
FoldersScreenUtils.refreshCalls_.push(true);
try {
const folders = await this.allForDisplay({ includeConflictFolder: true });
this.dispatch({
type: 'FOLDER_UPDATE_ALL',
items: folders,
});
} finally {
FoldersScreenUtils.refreshCalls_.pop();
}
}
static scheduleRefreshFolders() {
if (this.scheduleRefreshFoldersIID_) shim.clearTimeout(this.scheduleRefreshFoldersIID_);
this.scheduleRefreshFoldersIID_ = shim.setTimeout(() => {
this.scheduleRefreshFoldersIID_ = null;
this.refreshFolders();
}, 1000);
}
static async cancelTimers() {
if (this.scheduleRefreshFoldersIID_) {
shim.clearTimeout(this.scheduleRefreshFoldersIID_);
this.scheduleRefreshFoldersIID_ = null;
}
return new Promise((resolve) => {
const iid = shim.setInterval(() => {
if (!FoldersScreenUtils.refreshCalls_.length) {
shim.clearInterval(iid);
resolve();
}
}, 100);
});
}
}
FoldersScreenUtils.refreshCalls_ = [];
module.exports = { FoldersScreenUtils };