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