Files: 120199bf0f621f675ea6a87a71231c7e400eb81b / unread.js
1256 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 | console.log('save!', Object.keys(unread.filter).length) |
33 | localStorage.unread = JSON.stringify(unread) |
34 | }, 2e3) |
35 | } |
36 | |
37 | function isUnread(msg) { |
38 | if(msg.timestamp && msg.timestamp < unread.timestamp) return false |
39 | return !unread.filter[msg.key] |
40 | } |
41 | |
42 | function markRead(msg) { |
43 | if(msg && 'string' === typeof msg.key) { |
44 | //note: there is a quirk where some messages don't have a timestamp |
45 | if(isUnread(msg)) { |
46 | unread.filter[msg.key] = true |
47 | save() |
48 | return true |
49 | } |
50 | } |
51 | } |
52 | |
53 | document.body.onunload = save |
54 | |
55 | return nest({ |
56 | 'unread.sync.isUnread': isUnread, |
57 | 'unread.sync.markRead': markRead |
58 | }) |
59 | } |
60 | |
61 | |
62 | |
63 |
Built with git-ssb-web