-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchat_thread_store.js
More file actions
44 lines (38 loc) · 979 Bytes
/
Copy pathchat_thread_store.js
File metadata and controls
44 lines (38 loc) · 979 Bytes
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
function normalizeString(value) {
return String(value || '').trim();
}
function createChatThreadStore() {
const threads = new Map();
function upsertThread(walletId, patch = {}) {
const safeWalletId = normalizeString(walletId);
if (!safeWalletId) throw new Error('walletId is required');
const current = threads.get(safeWalletId) || {
walletId: safeWalletId,
unread: 0,
lastMessage: '',
lastTs: '',
};
const next = {
...current,
...patch,
walletId: safeWalletId,
};
threads.set(safeWalletId, next);
return { ...next };
}
function getThread(walletId) {
const safeWalletId = normalizeString(walletId);
return safeWalletId ? threads.get(safeWalletId) || null : null;
}
function listThreads() {
return Array.from(threads.values()).map((row) => ({ ...row }));
}
return {
upsertThread,
getThread,
listThreads,
};
}
module.exports = {
createChatThreadStore,
};