Commit cc1907f44ad723805e449e2380dc1fe439c74871
remove old files not needed anymore
Dominic Tarr committed on 2/9/2019, 6:00:00 AMParent: d539d5796cb627355c42b05a73c611edc1f4edf9
Files changed
cache-watcher.js | deleted |
frontend.js | deleted |
cache-watcher.js | ||
---|---|---|
@@ -1,113 +1,0 @@ | ||
1 | -var cacheId = require('./util').cacheId | |
2 | -var HashLRU = require('hashlru') | |
3 | -var pull = require('pull-stream') | |
4 | -var nested = require('libnested') | |
5 | - | |
6 | -module.exports = function (sbot) { | |
7 | - //TODO: switch to a cache implementation | |
8 | - //where you can access the oldest value | |
9 | - //so that when an eviction happens can increase | |
10 | - //`since`. | |
11 | - var cache = HashLRU(1024) | |
12 | - function latest (cb) { | |
13 | - var source = sbot.createLogStream({limit: 1, reverse:true, raw: true, values: false}) | |
14 | - source(null, function (err, seq) { | |
15 | - if(err) cb(err) | |
16 | - source(true, function () { | |
17 | - cb(null, seq) | |
18 | - }) | |
19 | - }) | |
20 | - } | |
21 | - | |
22 | - var since, start, timer, watching | |
23 | - ;(function watch () { | |
24 | - function reschedule (delay) { | |
25 | - if(watching) return | |
26 | - clearTimeout(timer) | |
27 | - timer = setTimeout(watch, delay*Math.random() + delay/2) | |
28 | - } | |
29 | - | |
30 | - watching = true | |
31 | - latest(function (err, seq) { | |
32 | - if(err) { | |
33 | - watching = false | |
34 | - //something wen't wrong, clear the whole cache, | |
35 | - //reload everything, but it's probably broken | |
36 | - //otherwise this would not have errored. | |
37 | - cache.clear() | |
38 | - start = since = null | |
39 | - rescheduce(10e3) | |
40 | - return | |
41 | - } | |
42 | - if(!start) { | |
43 | - start = since = seq //only the very first time | |
44 | - } | |
45 | - | |
46 | - //if it's been more that 256 messages (based on average size) | |
47 | - //since we last checked, just clear everything out. | |
48 | - if(seq > since + 256*1024) { | |
49 | - watching = false | |
50 | - start = seq | |
51 | - cache.clear() | |
52 | - } | |
53 | - | |
54 | - pull( | |
55 | - sbot.createLogStream({ | |
56 | - limit: 256, gt: since, raw: true, live: true | |
57 | - }), | |
58 | - pull.drain(function (data) { | |
59 | - var seq = data.seq | |
60 | - data = data.value | |
61 | - var content = data.value.content | |
62 | - nested.each(function (value) { | |
63 | - if(ref.isMsg(value) || ref.isFeed(value)) { | |
64 | - console.error('invalidate', value, seq) | |
65 | - cache.set(cacheId(value), seq) | |
66 | - } | |
67 | - }, true) | |
68 | - since = seq | |
69 | - }, function () { | |
70 | - watching = false | |
71 | - reschedule(10e3) | |
72 | - }) | |
73 | - ) | |
74 | - }) | |
75 | - })() | |
76 | - | |
77 | - //returns true if you need to revalidate | |
78 | - return { | |
79 | - since: function () { | |
80 | - if(since === undefined) throw new Error('since undefined') | |
81 | - return since | |
82 | - }, | |
83 | - // check wether an id needs to be revalidated. | |
84 | - // answer can be YES, NO or MAYBE. | |
85 | - // treating MAYBE like YES is called "optimistic" in computer science. | |
86 | - // we only keep track of recently updated things. | |
87 | - // user requests an update, and provides an id and the sequence. | |
88 | - // the sequence indicates how recent their value is. | |
89 | - // if we have a more recent sequence for that id, then YES. revalidate. | |
90 | - // if their sequence is older than the range we've been watching | |
91 | - // answer is MAYBE. | |
92 | - // if the sequence is is recent (since we've been watching) | |
93 | - // but hasn't changed then NO. | |
94 | - | |
95 | - check: function checkRevalidate (id, seq) { | |
96 | - var _seq = cache.get(id) | |
97 | - //the cached value is within the range we have been tracking | |
98 | - if(seq >= start) | |
99 | - //revalidate if we have a new value for that id | |
100 | - return _seq && _seq > seq ? _seq : undefined //"yes" or "no". | |
101 | - //if the value is really old "maybe" | |
102 | - else if(seq < since) | |
103 | - return -1 | |
104 | - //we just return true or false. true means yes or maybe. | |
105 | - }, | |
106 | - ///testing only | |
107 | - _dump: function () { | |
108 | - return cache._store | |
109 | - } | |
110 | - } | |
111 | -} | |
112 | - | |
113 | - |
frontend.js | ||
---|---|---|
@@ -1,78 +1,0 @@ | ||
1 | -var morph = require('morphdom') | |
2 | - | |
3 | -var cached = {} | |
4 | -var start = Date.now(), _focus = Date.now() | |
5 | -function scan () { | |
6 | - cached = {} | |
7 | - ;[].forEach.call(document.querySelectorAll('link[data-cache]'), function (el) { | |
8 | - cached[el.id] = +el.dataset.cache | |
9 | - }) | |
10 | -} | |
11 | - | |
12 | -window.onload = function () { | |
13 | - var start2 = Date.now() | |
14 | - scan() | |
15 | -} | |
16 | - | |
17 | -window.onfocus = function () { | |
18 | - _focus = Date.now() | |
19 | - check(1) | |
20 | -} | |
21 | - | |
22 | -function check () { | |
23 | - var xhr = new XMLHttpRequest() | |
24 | - xhr.onload = function () { | |
25 | - if(!xhr.responseText) return console.error('empty updates') | |
26 | - | |
27 | - var updates = JSON.parse(xhr.responseText) | |
28 | - console.log(updates) | |
29 | - | |
30 | - //mark any nodes that look like they need updating. | |
31 | - var elements = [], parents = [] | |
32 | - for(var k in updates) | |
33 | - [].forEach.call(document.querySelectorAll('#'+k), function (el) { | |
34 | - parents.push(el.parentNode) | |
35 | - elements.push(el) | |
36 | - }) | |
37 | - elements = elements.filter(function (e) { | |
38 | - e = e.parentNode | |
39 | - while(e) | |
40 | - if(~parents.indexOf(e.parentNode)) return false | |
41 | - else e = e.parentNode | |
42 | - return true | |
43 | - }).forEach(function (el) { | |
44 | - el.parentNode.classList.add('invalid') | |
45 | - update(el) | |
46 | - }) | |
47 | - } | |
48 | - //if the server fails, we'll just try again later. | |
49 | - xhr.onerror = function (ev) { | |
50 | - console.error('warning:', ev) | |
51 | - } | |
52 | - console.log('POST', '/check-cache') | |
53 | - xhr.open('POST', '/check-cache') | |
54 | - xhr.setRequestHeader('Content-Type', 'application/json') | |
55 | - xhr.send(JSON.stringify(cached)) | |
56 | -} | |
57 | - | |
58 | -function update (el) { | |
59 | - var href = el.href | |
60 | - var xhr = new XMLHttpRequest() | |
61 | - xhr.overrideMimeType("application/json"); | |
62 | - xhr.onload = function () { | |
63 | - //update html | |
64 | - console.log('update', el.href, xhr.statusCode) | |
65 | - if(xhr.statusCode == 200) { | |
66 | - morph(el.parentNode, xhr.responseText) | |
67 | - | |
68 | - //rebuild list of what might need to update | |
69 | - scan() | |
70 | - } | |
71 | - | |
72 | - } | |
73 | - href = href.substring(location.origin.length) | |
74 | - xhr.open('get', '/partial'+href) | |
75 | - xhr.send() | |
76 | -} | |
77 | - | |
78 | - |
Built with git-ssb-web