Files: 9fff2bb9c8cf22a5430644eaf74038867de5071d / unread.js
1226 bytesRaw
1 | var nest = require('depnest') |
2 | |
3 | exports.gives = nest({ |
4 | 'unread.sync.isUnread': true, |
5 | 'unread.sync.markRead': true, |
6 | }) |
7 | |
8 | //load current state of unread messages. |
9 | |
10 | exports.create = function (api) { |
11 | |
12 | var unread = null |
13 | if(localStorage.unread) { |
14 | try { |
15 | unread = JSON.parse(localStorage.unread) |
16 | } catch (err) {} |
17 | } |
18 | if(!unread) |
19 | unread = {timestamp: Date.now()} |
20 | |
21 | unread.timestamp = unread.timestamp || Date.now() |
22 | |
23 | if(!unread.filter) |
24 | unread.filter = {} |
25 | |
26 | var timer |
27 | function save () { |
28 | if(timer) return |
29 | |
30 | timer = setTimeout(function () { |
31 | timer = null |
32 | localStorage.unread = JSON.stringify(unread) |
33 | }, 2e3) |
34 | } |
35 | |
36 | function isUnread(msg) { |
37 | //ignore messages which do not have timestamps |
38 | if(!msg.timestamp) return false |
39 | if(msg.timestamp < unread.timestamp) return false |
40 | if(unread.filter[msg.key]) { |
41 | return false |
42 | } |
43 | return true |
44 | } |
45 | |
46 | function markRead(msg) { |
47 | if('string' === typeof msg.key) { |
48 | //if(isUnread(msg)) { |
49 | unread.filter[msg.key] = true |
50 | save() |
51 | return true |
52 | //} |
53 | } |
54 | } |
55 | |
56 | document.body.onunload = save |
57 | |
58 | return nest({ |
59 | 'unread.sync.isUnread': isUnread, |
60 | 'unread.sync.markRead': markRead |
61 | }) |
62 | } |
63 | |
64 |
Built with git-ssb-web