-
Notifications
You must be signed in to change notification settings - Fork 0
/
unread.js
67 lines (52 loc) · 1.34 KB
/
unread.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
var nest = require('depnest')
var { Dict, Set } = require('mutant')
exports.gives = nest({
'unread.sync.isUnread': true,
'unread.sync.markRead': true,
'unread.obs.userMessages': true
})
//load current state of unread messages.
exports.create = function (api) {
var unread = null
if(localStorage.unread) {
try {
unread = JSON.parse(localStorage.unread)
} catch (err) {}
}
if(!unread)
unread = {timestamp: Date.now()}
unread.timestamp = unread.timestamp || Date.now()
if(!unread.filter)
unread.filter = {}
var timer
function save () {
if(timer) return
timer = setTimeout(function () {
timer = null
localStorage.unread = JSON.stringify(unread)
}, 2e3)
}
function isUnread(msg) {
if(msg.timestamp && msg.timestamp < unread.timestamp) return false
return !unread.filter[msg.key]
}
function markRead(msg) {
if(msg && 'string' === typeof msg.key) {
//note: there is a quirk where some messages don't have a timestamp
if(isUnread(msg)) {
var userUser
unread.filter[msg.key] = true
save()
return true
}
}
}
function userMessages(feedId) {
}
document.body.onunload = save
return nest({
'unread.sync.isUnread': isUnread,
'unread.sync.markRead': markRead,
'unread.obs.userMessages': userMessages
})
}