-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseRoomUnread.ts
More file actions
131 lines (117 loc) · 3.33 KB
/
Copy pathuseRoomUnread.ts
File metadata and controls
131 lines (117 loc) · 3.33 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
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
import { RoomEvent } from 'matrix-js-sdk'
import type { MatrixClient } from 'matrix-js-sdk'
import { buildUnreadByRoomId } from '~/utils/roomUnread'
const MARK_ROOM_READ_DEBOUNCE_MS = 300
const REFRESH_UNREAD_DEBOUNCE_MS = 120
export function useRoomUnread(options: {
client: Ref<MatrixClient | null>
matrixRooms: Ref<Array<Record<string, unknown>>>
selectedRoomId: Ref<string | null>
markRoomAsRead: (roomId: string) => Promise<void>
}) {
const unreadVersion = ref(0)
let markReadTimerId: number | null = null
let refreshUnreadTimerId: number | null = null
function refreshUnread(): void {
unreadVersion.value += 1
}
function scheduleRefreshUnread(): void {
if (!import.meta.client) {
refreshUnread()
return
}
if (refreshUnreadTimerId !== null) {
window.clearTimeout(refreshUnreadTimerId)
}
refreshUnreadTimerId = window.setTimeout(() => {
refreshUnreadTimerId = null
refreshUnread()
}, REFRESH_UNREAD_DEBOUNCE_MS)
}
const unreadByRoomId = computed(() => {
unreadVersion.value
return buildUnreadByRoomId(options.matrixRooms.value, {
activeRoomId: options.selectedRoomId.value,
})
})
async function markActiveRoomRead(): Promise<void> {
const roomId = options.selectedRoomId.value
if (!roomId) {
return
}
await options.markRoomAsRead(roomId)
refreshUnread()
}
function scheduleMarkActiveRoomRead(): void {
if (!import.meta.client) {
void markActiveRoomRead()
return
}
const roomId = options.selectedRoomId.value
if (!roomId) {
return
}
if (markReadTimerId !== null) {
window.clearTimeout(markReadTimerId)
}
markReadTimerId = window.setTimeout(() => {
markReadTimerId = null
void markActiveRoomRead()
}, MARK_ROOM_READ_DEBOUNCE_MS)
}
watch(
() => options.client.value,
(matrixClient, _previousClient, onCleanup) => {
if (!matrixClient) {
return
}
const receiptHandler = (): void => {
scheduleRefreshUnread()
}
const unreadNotificationsHandler = (): void => {
scheduleRefreshUnread()
}
const timelineHandler = (
_timelineEvent: unknown,
room: { roomId?: string } | undefined,
): void => {
if (!room?.roomId) {
return
}
if (room.roomId === options.selectedRoomId.value) {
return
}
scheduleRefreshUnread()
}
matrixClient.on(RoomEvent.Receipt, receiptHandler)
matrixClient.on(
RoomEvent.UnreadNotifications,
unreadNotificationsHandler,
)
matrixClient.on(RoomEvent.Timeline, timelineHandler)
onCleanup(() => {
matrixClient.off(RoomEvent.Receipt, receiptHandler)
matrixClient.off(
RoomEvent.UnreadNotifications,
unreadNotificationsHandler,
)
matrixClient.off(RoomEvent.Timeline, timelineHandler)
if (markReadTimerId !== null) {
window.clearTimeout(markReadTimerId)
markReadTimerId = null
}
if (refreshUnreadTimerId !== null) {
window.clearTimeout(refreshUnreadTimerId)
refreshUnreadTimerId = null
}
})
},
{ immediate: true },
)
return {
unreadByRoomId,
refreshUnread,
markActiveRoomRead,
scheduleMarkActiveRoomRead,
}
}