git ssb

0+

dangerousbeans / yap



forked from Dominic / yap

Tree: d539d5796cb627355c42b05a73c611edc1f4edf9

Files: d539d5796cb627355c42b05a73c611edc1f4edf9 / frontend.js

1948 bytesRaw
1var morph = require('morphdom')
2
3var cached = {}
4var start = Date.now(), _focus = Date.now()
5function scan () {
6 cached = {}
7 ;[].forEach.call(document.querySelectorAll('link[data-cache]'), function (el) {
8 cached[el.id] = +el.dataset.cache
9 })
10}
11
12window.onload = function () {
13 var start2 = Date.now()
14 scan()
15}
16
17window.onfocus = function () {
18 _focus = Date.now()
19 check(1)
20}
21
22function 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
58function 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
79

Built with git-ssb-web