mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-31 21:21:03 -04:00
b83c35fdbb
add streaming zip export of history (migrated from track-changes#117) GitOrigin-RevId: 45e6a66332541f463241f148892817725c0be39c
42 lines
1 KiB
JavaScript
42 lines
1 KiB
JavaScript
module.exports = class DocIterator {
|
|
constructor(packs, getPackByIdFn) {
|
|
this.getPackByIdFn = getPackByIdFn
|
|
// sort packs in descending order by version (i.e. most recent first)
|
|
const byVersion = (a, b) => b.v - a.v
|
|
this.packs = packs.slice().sort(byVersion)
|
|
this.queue = []
|
|
}
|
|
|
|
next(callback) {
|
|
const update = this.queue.shift()
|
|
if (update) {
|
|
return callback(null, update)
|
|
}
|
|
if (!this.packs.length) {
|
|
this._done = true
|
|
return callback(null)
|
|
}
|
|
const nextPack = this.packs[0]
|
|
this.getPackByIdFn(
|
|
nextPack.project_id,
|
|
nextPack.doc_id,
|
|
nextPack._id,
|
|
(err, pack) => {
|
|
if (err != null) {
|
|
return callback(err)
|
|
}
|
|
this.packs.shift() // have now retrieved this pack, remove it
|
|
for (const op of pack.pack.reverse()) {
|
|
op.doc_id = nextPack.doc_id
|
|
op.project_id = nextPack.project_id
|
|
this.queue.push(op)
|
|
}
|
|
return this.next(callback)
|
|
}
|
|
)
|
|
}
|
|
|
|
done() {
|
|
return this._done
|
|
}
|
|
}
|