Commit c98c520dc293d99e6462eda47eb8183248adcf78
simpler unread state
Dominic Tarr committed on 8/17/2017, 5:11:24 AMParent: 3fc0d209961f54388d2c9d9096675af5c1f12a6c
Files changed
unread.js | changed |
unread.js | ||
---|---|---|
@@ -1,6 +1,6 @@ | ||
1 | 1 | var nest = require('depnest') |
2 | -var BloomFilter = require('jsbloom').filter | |
2 | +//var BloomFilter = require('jsbloom').filter | |
3 | 3 | |
4 | 4 | exports.gives = nest({ |
5 | 5 | 'unread.sync.isUnread': true, |
6 | 6 | 'unread.sync.markRead': true, |
@@ -14,42 +14,58 @@ | ||
14 | 14 | if(localStorage.unread) { |
15 | 15 | try { |
16 | 16 | unread = JSON.parse(localStorage.unread) |
17 | 17 | } catch (err) {} |
18 | + } | |
19 | + if(!unread) | |
20 | + unread = {timestamp: Date.now()} | |
18 | 21 | |
19 | - if(!unread) { | |
20 | - unread = {timestamp: Date.now()} | |
21 | - } | |
22 | + unread.timestamp = unread.timestamp || Date.now() | |
22 | 23 | |
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 | - } | |
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) | |
31 | 36 | } |
32 | 37 | |
33 | 38 | function isUnread(msg) { |
34 | 39 | //ignore messages which do not have timestamps |
35 | 40 | if(!msg.timestamp) return false |
36 | 41 | if(msg.timestamp < unread.timestamp) return false |
37 | - return !unread.filter.checkEntry(msg.key) | |
42 | + if(unread.filter[msg.key]) { | |
43 | + return false | |
44 | + } | |
45 | + return true | |
38 | 46 | } |
39 | 47 | |
40 | - function addRead(msg) { | |
41 | - unread.filter.addEntry(msg.key) | |
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 | + } | |
42 | 57 | } |
43 | 58 | |
44 | - document.body.onunload = function () { | |
45 | - localStorage.unread = JSON.stringify({ | |
46 | - timestamp: unread.timestamp, | |
47 | - filter: unread.filter.exportData() | |
48 | - }) | |
49 | - } | |
59 | + document.body.onunload = save | |
50 | 60 | |
51 | 61 | return nest({ |
52 | 62 | 'unread.sync.isUnread': isUnread, |
53 | 63 | 'unread.sync.markRead': markRead |
54 | 64 | }) |
55 | 65 | } |
66 | + | |
67 | + | |
68 | + | |
69 | + | |
70 | + | |
71 | + |
Built with git-ssb-web