Files: 3fc0d209961f54388d2c9d9096675af5c1f12a6c / unread.js
1207 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 | |
23 | if(!unread.filter) { |
24 | unread.filter = new Filter(10000, 0.001) |
25 | } |
26 | else { |
27 | var filter = new BloomFilter(10000, 0.001) |
28 | filter.importData(unread.filter) |
29 | unread.filter = filter |
30 | } |
31 | } |
32 | |
33 | function isUnread(msg) { |
34 | //ignore messages which do not have timestamps |
35 | if(!msg.timestamp) return false |
36 | if(msg.timestamp < unread.timestamp) return false |
37 | return !unread.filter.checkEntry(msg.key) |
38 | } |
39 | |
40 | function addRead(msg) { |
41 | unread.filter.addEntry(msg.key) |
42 | } |
43 | |
44 | document.body.onunload = function () { |
45 | localStorage.unread = JSON.stringify({ |
46 | timestamp: unread.timestamp, |
47 | filter: unread.filter.exportData() |
48 | }) |
49 | } |
50 | |
51 | return nest({ |
52 | 'unread.sync.isUnread': isUnread, |
53 | 'unread.sync.markRead': markRead |
54 | }) |
55 | } |
56 |
Built with git-ssb-web