55 lines
1.2 kB
1
import { Store } from "@moonlight-mod/wp/discord/packages/flux";
2
import Dispatcher from "@moonlight-mod/wp/discord/Dispatcher";
3
import type { Notice, Notices } from "@moonlight-mod/types/coreExtensions/notices";
4
5
// very lazy way of doing this, FIXME
6
let open = false;
7
8
class NoticesStore extends Store<any> {
9
private notices: Notice[] = [];
10
11
constructor() {
12
super(Dispatcher);
13
}
14
15
addNotice(notice: Notice) {
16
this.notices.push(notice);
17
if (open && this.notices.length !== 0) {
18
Dispatcher.dispatch({
19
type: "NOTICE_SHOW",
20
notice: { type: "__moonlight_notice" }
21
});
22
}
23
this.emitChange();
24
}
25
26
popNotice() {
27
this.notices.shift();
28
this.emitChange();
29
}
30
31
getCurrentNotice() {
32
return this.notices.length > 0 ? this.notices[0] : null;
33
}
34
35
shouldShowNotice() {
36
return this.notices.length > 0;
37
}
38
}
39
40
const store: Notices = new NoticesStore();
41
42
function showNotice() {
43
open = true;
44
if (store.shouldShowNotice()) {
45
Dispatcher.dispatch({
46
type: "NOTICE_SHOW",
47
notice: { type: "__moonlight_notice" }
48
});
49
}
50
}
51
52
Dispatcher.subscribe("CONNECTION_OPEN", showNotice);
53
Dispatcher.subscribe("CONNECTION_OPEN_SUPPLEMENTAL", showNotice);
54
55
export default store;
56