-
Notifications
You must be signed in to change notification settings - Fork 2
/
store.js
166 lines (142 loc) · 3.17 KB
/
store.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { ReceiptType, Unread } from "./state.js";
const PREFIX = "gamja_";
class Item {
constructor(k) {
this.k = PREFIX + k;
}
load() {
let v = localStorage.getItem(this.k);
if (!v) {
return null;
}
return JSON.parse(v);
}
put(v) {
if (v) {
localStorage.setItem(this.k, JSON.stringify(v));
} else {
localStorage.removeItem(this.k);
}
}
}
export const autoconnect = new Item("autoconnect");
export const naggedProtocolHandler = new Item("naggedProtocolHandler");
export const settings = new Item("settings");
function debounce(f, delay) {
let timeout = null;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => {
timeout = null;
f(...args);
}, delay);
};
}
export class Buffer {
raw = new Item("buffers");
m = null;
constructor() {
let obj = this.raw.load();
this.m = new Map(Object.entries(obj || {}));
let saveImmediately = this.save.bind(this);
this.save = debounce(saveImmediately, 500);
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "hidden") {
saveImmediately();
}
});
}
key(buf) {
// TODO: use case-mapping here somehow
return JSON.stringify({
name: buf.name.toLowerCase(),
server: {
bouncerNetwork: buf.server.bouncerNetwork,
},
});
}
save() {
if (this.m.size > 0) {
this.raw.put(Object.fromEntries(this.m));
} else {
this.raw.put(null);
}
}
get(buf) {
return this.m.get(this.key(buf));
}
put(buf) {
let key = this.key(buf);
let updated = !this.m.has(key);
let prev = this.m.get(key) || {};
let unread = prev.unread || Unread.NONE;
if (buf.unread !== undefined && buf.unread !== prev.unread) {
unread = buf.unread;
updated = true;
}
let receipts = { ...prev.receipts };
if (buf.receipts) {
Object.keys(buf.receipts).forEach((k) => {
// Use a not-equals comparison here so that no-op receipt
// changes are correctly handled
if (!receipts[k] || receipts[k].time < buf.receipts[k].time) {
receipts[k] = buf.receipts[k];
updated = true;
}
});
if (receipts[ReceiptType.DELIVERED] < receipts[ReceiptType.READ]) {
receipts[ReceiptType.DELIVERED] = receipts[ReceiptType.READ];
updated = true;
}
}
let closed = prev.closed || false;
if (buf.closed !== undefined && buf.closed !== prev.closed) {
closed = buf.closed;
updated = true;
}
if (!updated) {
return false;
}
this.m.set(this.key(buf), {
name: buf.name,
unread,
receipts,
closed,
server: {
bouncerNetwork: buf.server.bouncerNetwork,
},
});
this.save();
return true;
}
delete(buf) {
this.m.delete(this.key(buf));
this.save();
}
list(server) {
// Some gamja versions would store the same buffer multiple times
let names = new Set();
let buffers = [];
for (const buf of this.m.values()) {
if (buf.server.bouncerNetwork !== server.bouncerNetwork) {
continue;
}
if (names.has(buf.name)) {
continue;
}
buffers.push(buf);
names.add(buf.name);
}
return buffers;
}
clear(server) {
if (server) {
for (const buf of this.list(server)) {
this.m.delete(this.key(buf));
}
} else {
this.m = new Map();
}
this.save();
}
}