cache github data for 1 day w/ local storage

This commit is contained in:
Sami Samhuri 2010-11-26 08:19:18 -08:00
parent 5603093fea
commit 2c722b05a5
3 changed files with 116 additions and 71 deletions

View file

@ -56,6 +56,7 @@
})(); })();
</script> </script>
<script src=../gitter.js></script> <script src=../gitter.js></script>
<script src=../store.js></script>
<script> <script>
(function() { (function() {
function addClass(el, name) { function addClass(el, name) {
@ -94,63 +95,79 @@
}) })
} }
var data = {} function updateBranches(branches) {
html('branches', Object.keys(branches).map(branchLink).join('<br>'))
}
function updateContributors(contributors) {
html('contributors', contributors.map(userLink).join('<br>'))
}
function updateLangs(langs) {
html('langs', langsByUsage(langs).join('<br>'))
}
function updateN(name, things) {
textHighlight('n' + name, things.length)
if (things.length === 1) hide(name.charAt(0) + 'plural')
}
document.addEventListener('DOMContentLoaded', ready, false) document.addEventListener('DOMContentLoaded', ready, false)
function fakeReady() {
text('branches', 'master')
text('langs', 'JavaScript')
}
function ready() { function ready() {
GITR.repo('samsonjs/batteries') var t = ObjectStore.get('t-batteries')
.getBranches(function(err, branches) { if (!t || +new Date() - t > 86400000) {
if (err) { console.log('stale ' + String(t))
text('branches', '(oops)') ObjectStore.set('t-batteries', +new Date())
} else { GITR.repo('samsonjs/batteries')
data.branches = branches .getBranches(function(err, branches) {
html('branches', Object.keys(branches).map(branchLink).join('<br>')) if (err) {
} text('branches', '(oops)')
}) } else {
.getLanguages(function(err, langs) { ObjectStore.set('branches', branches)
if (err) { updateBranches(branches)
text('(oops)') }
return })
} .getLanguages(function(err, langs) {
data.langs = langs if (err) {
html('langs', langsByUsage(langs).join('<br>')) text('langs', '(oops)')
}) return
.getWatchers(function(err, users) { }
if (err) { ObjectStore.set('langs', langs)
text('nwatchers', '?') updateLangs(langs)
} else { })
data.watchers = users .getContributors(function(err, users) {
textHighlight('nwatchers', users.length) if (err) {
if (users.length === 1) hide('wplural') text('contributors', '(oops)')
} } else {
}) ObjectStore.set('contributors', users)
.getNetwork(function(err, repos) { updateContributors(users)
if (err) { }
text('nforks', '?') })
} else { .getWatchers(function(err, users) {
data.forks = repos if (err) {
textHighlight('nforks', repos.length) text('nwatchers', '?')
if (repos.length === 1) hide('fplural') } else {
} ObjectStore.set('watchers', users)
}) updateN('watchers', users)
.getContributors(function(err, users) { }
if (err) { })
text('contributors', '(oops)') .getNetwork(function(err, repos) {
} else { if (err) {
data.contributors = users text('nforks', '?')
html('contributors', users.map(userLink).join('<br>')) } else {
} ObjectStore.set('forks', repos)
}) updateN('forks', repos)
}
// })
// integrate github finder into project pages } else {
// console.log('hit ' + t + ' (' + (+new Date() - t) + ')')
updateBranches(ObjectStore.get('branches'))
updateLangs(ObjectStore.get('langs'))
updateContributors(ObjectStore.get('contributors'))
updateN('watchers', ObjectStore.get('watchers'))
updateN('forks', ObjectStore.get('forks'))
}
} }
}()) }())
</script> </script>

View file

@ -27,6 +27,7 @@
})(); })();
</script> </script>
<script src=gitter.js></script> <script src=gitter.js></script>
<script src=store.js></script>
<script> <script>
(function() { (function() {
function addClass(el, name) { function addClass(el, name) {
@ -44,29 +45,34 @@
el.innerText = text el.innerText = text
el.className = ' highlight' el.className = ' highlight'
} }
function updateN(name, things) {
var data = {} textHighlight('n' + name, things.length)
}
document.addEventListener('DOMContentLoaded', ready, false) document.addEventListener('DOMContentLoaded', ready, false)
function fakeReady() {
textHighlight('nrepos', '20')
textHighlight('nfollowers', '13')
textHighlight('nwatched', '100')
textHighlight('nfollowing', '69')
}
function ready() { function ready() {
['followers', 'following', 'repos', 'watched'].forEach(function(thing) { var t = ObjectStore.get('t-proj')
GITR[thing]('samsonjs', function(err, things) { , names = ['followers', 'following', 'repos', 'watched']
if (err) { if (!t || +new Date() - t > 86400000) {
text('n' + thing, '?') console.log('stale ' + String(t))
} else { ObjectStore.set('t-proj', +new Date())
data[thing] = things names.forEach(function(name) {
textHighlight('n' + thing, things.length) GITR[name]('samsonjs', function(err, things) {
} if (err) {
text('n' + name, '?')
} else {
ObjectStore.set(name, things)
updateN(name, things)
}
})
}) })
}) } else {
console.log('hit ' + t + ' (' + (+new Date() - t) + ')')
names.forEach(function(name) {
updateN(name, ObjectStore.get(name))
})
}
} }
}()) }())
</script> </script>

22
proj/store.js Normal file
View file

@ -0,0 +1,22 @@
(function() {
if (typeof localStorage === 'undefined') return
var global = this
global.ObjectStore = {
clear: function() {
localStorage.clear()
},
get: function(key) {
var val = localStorage.getItem(key)
return typeof val === 'string' ? JSON.parse(val) : val
},
key: function(n) {
return localStorage.key(n)
},
set: function(key, val) {
localStorage.setItem(key, JSON.stringify(val))
},
remove: function(key) {
localStorage.remove(key)
}
}
}())