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 src=../gitter.js></script>
<script src=../store.js></script>
<script>
(function() {
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)
function fakeReady() {
text('branches', 'master')
text('langs', 'JavaScript')
}
function ready() {
GITR.repo('samsonjs/batteries')
.getBranches(function(err, branches) {
if (err) {
text('branches', '(oops)')
} else {
data.branches = branches
html('branches', Object.keys(branches).map(branchLink).join('<br>'))
}
})
.getLanguages(function(err, langs) {
if (err) {
text('(oops)')
return
}
data.langs = langs
html('langs', langsByUsage(langs).join('<br>'))
})
.getWatchers(function(err, users) {
if (err) {
text('nwatchers', '?')
} else {
data.watchers = users
textHighlight('nwatchers', users.length)
if (users.length === 1) hide('wplural')
}
})
.getNetwork(function(err, repos) {
if (err) {
text('nforks', '?')
} else {
data.forks = repos
textHighlight('nforks', repos.length)
if (repos.length === 1) hide('fplural')
}
})
.getContributors(function(err, users) {
if (err) {
text('contributors', '(oops)')
} else {
data.contributors = users
html('contributors', users.map(userLink).join('<br>'))
}
})
//
// integrate github finder into project pages
//
var t = ObjectStore.get('t-batteries')
if (!t || +new Date() - t > 86400000) {
console.log('stale ' + String(t))
ObjectStore.set('t-batteries', +new Date())
GITR.repo('samsonjs/batteries')
.getBranches(function(err, branches) {
if (err) {
text('branches', '(oops)')
} else {
ObjectStore.set('branches', branches)
updateBranches(branches)
}
})
.getLanguages(function(err, langs) {
if (err) {
text('langs', '(oops)')
return
}
ObjectStore.set('langs', langs)
updateLangs(langs)
})
.getContributors(function(err, users) {
if (err) {
text('contributors', '(oops)')
} else {
ObjectStore.set('contributors', users)
updateContributors(users)
}
})
.getWatchers(function(err, users) {
if (err) {
text('nwatchers', '?')
} else {
ObjectStore.set('watchers', users)
updateN('watchers', users)
}
})
.getNetwork(function(err, repos) {
if (err) {
text('nforks', '?')
} else {
ObjectStore.set('forks', repos)
updateN('forks', repos)
}
})
} 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>

View file

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

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)
}
}
}())