Files: 6c2d62189f2bd14a50a90b56b6e4b20393233a56 / unread.js
1310 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 | console.log("SAVE") |
34 | localStorage.unread = JSON.stringify(unread) |
35 | }, 2e3) |
36 | } |
37 | |
38 | function isUnread(msg) { |
39 | //ignore messages which do not have timestamps |
40 | if(!msg.timestamp) return false |
41 | if(msg.timestamp < unread.timestamp) return false |
42 | if(unread.filter[msg.key]) { |
43 | return false |
44 | } |
45 | return true |
46 | } |
47 | |
48 | function markRead(msg) { |
49 | if('string' === typeof msg.key) { |
50 | //if(isUnread(msg)) { |
51 | unread.filter[msg.key] = true |
52 | save() |
53 | return true |
54 | //} |
55 | |
56 | } |
57 | } |
58 | |
59 | document.body.onunload = save |
60 | |
61 | return nest({ |
62 | 'unread.sync.isUnread': isUnread, |
63 | 'unread.sync.markRead': markRead |
64 | }) |
65 | } |
66 | |
67 | |
68 | |
69 | |
70 | |
71 | |
72 |
Built with git-ssb-web