Split work into chunks to improve UI responsiveness on large repos; update sort to match GitHub's algorithm
This commit is contained in:
parent
5a26a4794d
commit
ebd5e74f7f
|
@ -177,8 +177,7 @@
|
||||||
function fetchData(repo, done) {
|
function fetchData(repo, done) {
|
||||||
var github = new Github({ token: store.get(STORE_TOKEN) })
|
var github = new Github({ token: store.get(STORE_TOKEN) })
|
||||||
, api = github.getRepo(repo.username, repo.reponame)
|
, api = github.getRepo(repo.username, repo.reponame)
|
||||||
, root = []
|
, folders = { '': [] }
|
||||||
, folders = { '': root }
|
|
||||||
, encodedBranch = encodeURIComponent(decodeURIComponent(repo.branch))
|
, encodedBranch = encodeURIComponent(decodeURIComponent(repo.branch))
|
||||||
|
|
||||||
api.getTree(encodedBranch + '?recursive=true', function(err, tree) {
|
api.getTree(encodedBranch + '?recursive=true', function(err, tree) {
|
||||||
|
@ -187,39 +186,52 @@
|
||||||
fetchSubmodules(function(err, submodules) {
|
fetchSubmodules(function(err, submodules) {
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
|
|
||||||
tree.forEach(function(item) {
|
// split work in chunks to prevent blocking UI on large repos
|
||||||
var path = item.path
|
nextChunk(0)
|
||||||
, type = item.type
|
function nextChunk(iteration) {
|
||||||
, index = path.lastIndexOf('/')
|
var chunkSize = 500
|
||||||
, name = $dummyDiv.text(path.substring(index + 1)).html() // sanitizes, closes #9
|
, baseIndex = iteration * chunkSize
|
||||||
, folder = folders[path.substring(0, index)]
|
, i
|
||||||
|
, item, path, type, index, name
|
||||||
|
|
||||||
folder.push(item)
|
for (i = 0; i < chunkSize; i++) {
|
||||||
item.id = PREFIX + path
|
item = tree[baseIndex + i]
|
||||||
item.text = name
|
if (item === undefined) return done(null, sort(folders['']))
|
||||||
item.icon = type // use `type` as class name for tree node
|
|
||||||
|
|
||||||
if (type === 'tree') {
|
path = item.path
|
||||||
folders[item.path] = item.children = []
|
type = item.type
|
||||||
item.a_attr = { href: '#' }
|
index = path.lastIndexOf('/')
|
||||||
}
|
name = $dummyDiv.text(path.substring(index + 1)).html() // sanitizes, closes #9
|
||||||
else if (type === 'blob') {
|
item.id = PREFIX + path
|
||||||
item.a_attr = { href: '/' + repo.username + '/' + repo.reponame + '/' + type + '/' + repo.branch + '/' + path }
|
item.text = name
|
||||||
}
|
item.icon = type // use `type` as class name for tree node
|
||||||
else if (type === 'commit') {
|
|
||||||
var moduleUrl = submodules[item.path]
|
|
||||||
|
|
||||||
// Special handling for submodules hosted in GitHub
|
folders[path.substring(0, index)].push(item)
|
||||||
if (~moduleUrl.indexOf('github.com')) {
|
|
||||||
item.text = '<a href="' + moduleUrl + '" class="jstree-anchor">' + name + '</a>' +
|
if (type === 'tree') {
|
||||||
'<span>@ </span>' +
|
folders[item.path] = item.children = []
|
||||||
'<a href="' + moduleUrl.replace(/.git$/, '') + '/tree/' + item.sha + '" class="jstree-anchor">' + item.sha.substr(0, 7) + '</a>'
|
item.a_attr = { href: '#' }
|
||||||
}
|
}
|
||||||
item.a_attr = { href: moduleUrl }
|
else if (type === 'blob') {
|
||||||
}
|
item.a_attr = { href: '/' + repo.username + '/' + repo.reponame + '/' + type + '/' + repo.branch + '/' + path }
|
||||||
})
|
}
|
||||||
|
else if (type === 'commit') {
|
||||||
|
var moduleUrl = submodules[item.path]
|
||||||
|
|
||||||
done(null, sort(root))
|
// special handling for submodules hosted in GitHub
|
||||||
|
if (~moduleUrl.indexOf('github.com')) {
|
||||||
|
item.text = '<a href="' + moduleUrl + '" class="jstree-anchor">' + name + '</a>' +
|
||||||
|
'<span>@ </span>' +
|
||||||
|
'<a href="' + moduleUrl.replace(/.git$/, '') + '/tree/' + item.sha + '" class="jstree-anchor">' + item.sha.substr(0, 7) + '</a>'
|
||||||
|
}
|
||||||
|
item.a_attr = { href: moduleUrl }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setTimeout(function() {
|
||||||
|
nextChunk(iteration + 1)
|
||||||
|
}, 0)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
function fetchSubmodules(cb) {
|
function fetchSubmodules(cb) {
|
||||||
|
@ -244,9 +256,10 @@
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sorts (try matching GitHub's sort order)
|
||||||
function sort(folder) {
|
function sort(folder) {
|
||||||
folder.sort(function(a, b) {
|
folder.sort(function(a, b) {
|
||||||
if (a.type === b.type) return a.text.localeCompare(b.text)
|
if (a.type === b.type) return a.text === b.text ? 0 : a.text < b.text ? -1 : 1
|
||||||
return a.type === 'blob' ? 1 : -1
|
return a.type === 'blob' ? 1 : -1
|
||||||
})
|
})
|
||||||
folder.forEach(function(item) {
|
folder.forEach(function(item) {
|
||||||
|
|
Loading…
Reference in New Issue