Last active
March 15, 2017 01:26
-
-
Save blackmiaool/5fd74919bcc6955fd6401fa9119dd1fc to your computer and use it in GitHub Desktop.
getDataGradually
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getDataGradually(data, childKey, gap, count, updateCb, finishCb) { | |
const lastPositon = []; | |
const linearData = []; | |
const ret = []; | |
function getLinearData(arr, position) { | |
arr.forEach(function (obj, index) { | |
const pos = position.concat([index]); | |
if (obj[childKey] && obj[childKey].length) { | |
var children = obj[childKey]; | |
obj[childKey] = []; | |
linearData.push({ | |
obj, | |
pos | |
}); | |
getLinearData(children, pos); | |
} else { | |
linearData.push({ | |
obj, | |
pos | |
}); | |
} | |
}); | |
} | |
getLinearData(data, []); | |
function insertData({ | |
obj, | |
pos | |
}) { | |
let target = ret; | |
pos.forEach(function (i, index) { | |
if (index === pos.length - 1) { | |
target[i] = obj; | |
} else { | |
target = target[i][childKey]; | |
} | |
}); | |
} | |
let handled = 0; | |
function doInsert() { | |
let start = handled; | |
let end; | |
if (handled + count > linearData.length) { | |
end = linearData.length; | |
} else { | |
end = handled + count; | |
} | |
for (let i = start; i < end; i++) { | |
insertData(linearData[i]); | |
} | |
handled += count; | |
if (handled < linearData.length) { | |
setTimeout(function () { | |
doInsert(); | |
updateCb && updateCb(); | |
}, gap); | |
} else { | |
finishCb && finishCb(); | |
} | |
} | |
doInsert(); | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment