-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraftManager.js
More file actions
83 lines (69 loc) · 2.05 KB
/
draftManager.js
File metadata and controls
83 lines (69 loc) · 2.05 KB
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
78
79
80
81
82
83
const Draft = require('./draft');
const placeholder = new Draft();
const defaultFindOptions = {
user: true,
wide: false,
owner: false,
admin: false,
};
class Manager {
constructor() {
this.drafts = new Set([placeholder]);
this.drafts.delete(placeholder);
}
first() {
let value;
for (value of this.drafts) return value;
return value;
}
last() {
let value;
for (value of this.drafts) /* Go to last */;
return value;
}
running() {
return [...this.drafts.values()].find(draft => draft.running && draft.running !== 'finished');
}
finished() {
return [...this.drafts.values()].find(draft => draft.running === 'finished');
}
find(context, {
user,
wide,
owner,
admin,
} = defaultFindOptions) {
const drafts = [...this.drafts.values()];
const userID = context.user.id;
return (user && (wide ? drafts : drafts.filter(draft => draft.channels.includes(context.channel.id)))
// Is channel owner (be more specific first)
.find(draft => draft.participants.find(draftee => draftee.user === userID))) ||
// Is draft owner (widen the net now)
(owner && drafts.find(draft => (draft.owner.id || draft.owner) === userID)) ||
// Is admin
(admin && context.isAdmin() && drafts[0]);
}
get(id = '0') {
return [...this.drafts.values()].find(draft => draft.id == id);
}
register(draft = placeholder) {
if (draft === placeholder || !(draft instanceof Draft)) throw new Error('Missing Draft');
if (this.drafts.has(draft)) throw new Error('Duplicate Draft');
const last = this.last();
// Set Draft ID
draft.id = last ? last.id + 1 : 1;
// Delete on clear
draft.on('cleared', err => err || this.drafts.delete(draft));
this.drafts.add(draft);
}
static for(key = '') {
if (!key.trim()) throw new Error('Missing Server Key');
if (!registry.has(key)) {
registry.set(key, new Manager())
}
return registry.get(key);
}
}
const registry = new Map([['', new Manager()]]);
registry.delete('');
module.exports = Manager;