mirror of
https://github.com/samsonjs/samhuri.net.git
synced 2026-03-25 09:05:47 +00:00
cache github data for 1 day w/ local storage
This commit is contained in:
parent
5603093fea
commit
2c722b05a5
3 changed files with 116 additions and 71 deletions
|
|
@ -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() {
|
||||
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 {
|
||||
data.branches = branches
|
||||
html('branches', Object.keys(branches).map(branchLink).join('<br>'))
|
||||
ObjectStore.set('branches', branches)
|
||||
updateBranches(branches)
|
||||
}
|
||||
})
|
||||
.getLanguages(function(err, langs) {
|
||||
if (err) {
|
||||
text('(oops)')
|
||||
text('langs', '(oops)')
|
||||
return
|
||||
}
|
||||
data.langs = langs
|
||||
html('langs', langsByUsage(langs).join('<br>'))
|
||||
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 {
|
||||
data.watchers = users
|
||||
textHighlight('nwatchers', users.length)
|
||||
if (users.length === 1) hide('wplural')
|
||||
ObjectStore.set('watchers', users)
|
||||
updateN('watchers', users)
|
||||
}
|
||||
})
|
||||
.getNetwork(function(err, repos) {
|
||||
if (err) {
|
||||
text('nforks', '?')
|
||||
} else {
|
||||
data.forks = repos
|
||||
textHighlight('nforks', repos.length)
|
||||
if (repos.length === 1) hide('fplural')
|
||||
ObjectStore.set('forks', repos)
|
||||
updateN('forks', repos)
|
||||
}
|
||||
})
|
||||
.getContributors(function(err, users) {
|
||||
if (err) {
|
||||
text('contributors', '(oops)')
|
||||
} else {
|
||||
data.contributors = users
|
||||
html('contributors', users.map(userLink).join('<br>'))
|
||||
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'))
|
||||
}
|
||||
})
|
||||
|
||||
//
|
||||
// integrate github finder into project pages
|
||||
//
|
||||
}
|
||||
}())
|
||||
</script>
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
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' + thing, '?')
|
||||
text('n' + name, '?')
|
||||
} else {
|
||||
data[thing] = things
|
||||
textHighlight('n' + thing, things.length)
|
||||
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
22
proj/store.js
Normal 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)
|
||||
}
|
||||
}
|
||||
}())
|
||||
Loading…
Reference in a new issue