Merge pull request #5984 from overleaf/em-unnecessarily-async-functions
Clean up unnecessarily async functions GitOrigin-RevId: 59f0f0a76b4436f3b99a09b747670d443bac4582
This commit is contained in:
@@ -637,28 +637,22 @@ const ClsiManager = {
|
||||
},
|
||||
|
||||
getContentFromDocUpdaterIfMatch(projectId, project, options, callback) {
|
||||
ClsiStateManager.computeHash(project, options, (err, projectStateHash) => {
|
||||
if (err != null) {
|
||||
return callback(
|
||||
OError.tag(err, 'Failed to compute project state hash', { projectId })
|
||||
)
|
||||
}
|
||||
DocumentUpdaterHandler.getProjectDocsIfMatch(
|
||||
projectId,
|
||||
projectStateHash,
|
||||
(err, docs) => {
|
||||
if (err != null) {
|
||||
return callback(
|
||||
OError.tag(err, 'Failed to get project documents', {
|
||||
projectId,
|
||||
projectStateHash,
|
||||
})
|
||||
)
|
||||
}
|
||||
callback(null, projectStateHash, docs)
|
||||
const projectStateHash = ClsiStateManager.computeHash(project, options)
|
||||
DocumentUpdaterHandler.getProjectDocsIfMatch(
|
||||
projectId,
|
||||
projectStateHash,
|
||||
(err, docs) => {
|
||||
if (err != null) {
|
||||
return callback(
|
||||
OError.tag(err, 'Failed to get project documents', {
|
||||
projectId,
|
||||
projectStateHash,
|
||||
})
|
||||
)
|
||||
}
|
||||
)
|
||||
})
|
||||
callback(null, projectStateHash, docs)
|
||||
}
|
||||
)
|
||||
},
|
||||
|
||||
getOutputFileStream(projectId, userId, buildId, outputFilePath, callback) {
|
||||
@@ -688,42 +682,36 @@ const ClsiManager = {
|
||||
docUpdaterDocs,
|
||||
callback
|
||||
) {
|
||||
ProjectEntityHandler.getAllDocPathsFromProject(project, (err, docPath) => {
|
||||
if (err != null) {
|
||||
return callback(
|
||||
OError.tag(err, 'Failed to get doc paths', { projectId })
|
||||
)
|
||||
}
|
||||
const docs = {}
|
||||
for (const doc of docUpdaterDocs || []) {
|
||||
const path = docPath[doc._id]
|
||||
docs[path] = doc
|
||||
}
|
||||
// send new docs but not files as those are already on the clsi
|
||||
options = _.clone(options)
|
||||
options.syncType = 'incremental'
|
||||
options.syncState = projectStateHash
|
||||
// create stub doc entries for any possible root docs, if not
|
||||
// present in the docupdater. This allows finaliseRequest to
|
||||
// identify the root doc.
|
||||
const possibleRootDocIds = [options.rootDoc_id, project.rootDoc_id]
|
||||
for (const rootDocId of possibleRootDocIds) {
|
||||
if (rootDocId != null && rootDocId in docPath) {
|
||||
const path = docPath[rootDocId]
|
||||
if (docs[path] == null) {
|
||||
docs[path] = { _id: rootDocId, path }
|
||||
}
|
||||
const docPath = ProjectEntityHandler.getAllDocPathsFromProject(project)
|
||||
const docs = {}
|
||||
for (const doc of docUpdaterDocs || []) {
|
||||
const path = docPath[doc._id]
|
||||
docs[path] = doc
|
||||
}
|
||||
// send new docs but not files as those are already on the clsi
|
||||
options = _.clone(options)
|
||||
options.syncType = 'incremental'
|
||||
options.syncState = projectStateHash
|
||||
// create stub doc entries for any possible root docs, if not
|
||||
// present in the docupdater. This allows finaliseRequest to
|
||||
// identify the root doc.
|
||||
const possibleRootDocIds = [options.rootDoc_id, project.rootDoc_id]
|
||||
for (const rootDocId of possibleRootDocIds) {
|
||||
if (rootDocId != null && rootDocId in docPath) {
|
||||
const path = docPath[rootDocId]
|
||||
if (docs[path] == null) {
|
||||
docs[path] = { _id: rootDocId, path }
|
||||
}
|
||||
}
|
||||
ClsiManager._finaliseRequest(
|
||||
projectId,
|
||||
options,
|
||||
project,
|
||||
docs,
|
||||
[],
|
||||
callback
|
||||
)
|
||||
})
|
||||
}
|
||||
ClsiManager._finaliseRequest(
|
||||
projectId,
|
||||
options,
|
||||
project,
|
||||
docs,
|
||||
[],
|
||||
callback
|
||||
)
|
||||
},
|
||||
|
||||
_buildRequestFromMongo(
|
||||
|
||||
@@ -37,45 +37,37 @@ const buildState = s =>
|
||||
crypto.createHash('sha1').update(s, 'utf8').digest('hex')
|
||||
|
||||
module.exports = ClsiStateManager = {
|
||||
computeHash(project, options, callback) {
|
||||
if (callback == null) {
|
||||
callback = function () {}
|
||||
}
|
||||
return ProjectEntityHandler.getAllEntitiesFromProject(
|
||||
project,
|
||||
function (err, docs, files) {
|
||||
const fileList = Array.from(files || []).map(
|
||||
f => `${f.file._id}:${f.file.rev}:${f.file.created}:${f.path}`
|
||||
)
|
||||
const docList = Array.from(docs || []).map(
|
||||
d => `${d.doc._id}:${d.path}`
|
||||
)
|
||||
const sortedEntityList = [
|
||||
...Array.from(docList),
|
||||
...Array.from(fileList),
|
||||
].sort()
|
||||
// ignore the isAutoCompile options as it doesn't affect the
|
||||
// output, but include all other options e.g. draft
|
||||
const optionsList = (() => {
|
||||
const result = []
|
||||
const object = options || {}
|
||||
for (const key in object) {
|
||||
const value = object[key]
|
||||
if (!['isAutoCompile'].includes(key)) {
|
||||
result.push(`option ${key}:${value}`)
|
||||
}
|
||||
}
|
||||
return result
|
||||
})()
|
||||
const sortedOptionsList = optionsList.sort()
|
||||
const hash = buildState(
|
||||
[
|
||||
...Array.from(sortedEntityList),
|
||||
...Array.from(sortedOptionsList),
|
||||
].join('\n')
|
||||
)
|
||||
return callback(null, hash)
|
||||
}
|
||||
computeHash(project, options) {
|
||||
const { docs, files } = ProjectEntityHandler.getAllEntitiesFromProject(
|
||||
project
|
||||
)
|
||||
const fileList = Array.from(files || []).map(
|
||||
f => `${f.file._id}:${f.file.rev}:${f.file.created}:${f.path}`
|
||||
)
|
||||
const docList = Array.from(docs || []).map(d => `${d.doc._id}:${d.path}`)
|
||||
const sortedEntityList = [
|
||||
...Array.from(docList),
|
||||
...Array.from(fileList),
|
||||
].sort()
|
||||
// ignore the isAutoCompile options as it doesn't affect the
|
||||
// output, but include all other options e.g. draft
|
||||
const optionsList = (() => {
|
||||
const result = []
|
||||
const object = options || {}
|
||||
for (const key in object) {
|
||||
const value = object[key]
|
||||
if (!['isAutoCompile'].includes(key)) {
|
||||
result.push(`option ${key}:${value}`)
|
||||
}
|
||||
}
|
||||
return result
|
||||
})()
|
||||
const sortedOptionsList = optionsList.sort()
|
||||
const hash = buildState(
|
||||
[...Array.from(sortedEntityList), ...Array.from(sortedOptionsList)].join(
|
||||
'\n'
|
||||
)
|
||||
)
|
||||
return hash
|
||||
},
|
||||
}
|
||||
|
||||
@@ -356,23 +356,18 @@ const ProjectController = {
|
||||
if (err != null) {
|
||||
return next(err)
|
||||
}
|
||||
ProjectEntityHandler.getAllEntitiesFromProject(
|
||||
project,
|
||||
(err, docs, files) => {
|
||||
if (err != null) {
|
||||
return next(err)
|
||||
}
|
||||
const entities = docs
|
||||
.concat(files)
|
||||
// Sort by path ascending
|
||||
.sort((a, b) => (a.path > b.path ? 1 : a.path < b.path ? -1 : 0))
|
||||
.map(e => ({
|
||||
path: e.path,
|
||||
type: e.doc != null ? 'doc' : 'file',
|
||||
}))
|
||||
res.json({ project_id: projectId, entities })
|
||||
}
|
||||
const { docs, files } = ProjectEntityHandler.getAllEntitiesFromProject(
|
||||
project
|
||||
)
|
||||
const entities = docs
|
||||
.concat(files)
|
||||
// Sort by path ascending
|
||||
.sort((a, b) => (a.path > b.path ? 1 : a.path < b.path ? -1 : 0))
|
||||
.map(e => ({
|
||||
path: e.path,
|
||||
type: e.doc != null ? 'doc' : 'file',
|
||||
}))
|
||||
res.json({ project_id: projectId, entities })
|
||||
})
|
||||
},
|
||||
|
||||
|
||||
@@ -69,31 +69,28 @@ const ProjectEntityHandler = {
|
||||
return callback(new Errors.NotFoundError('project not found'))
|
||||
}
|
||||
|
||||
ProjectEntityHandler.getAllEntitiesFromProject(project, callback)
|
||||
const entities = ProjectEntityHandler.getAllEntitiesFromProject(project)
|
||||
callback(null, entities)
|
||||
})
|
||||
},
|
||||
|
||||
getAllEntitiesFromProject(project, callback) {
|
||||
ProjectEntityHandler._getAllFoldersFromProject(project, (err, folders) => {
|
||||
if (err != null) {
|
||||
return callback(err)
|
||||
}
|
||||
const docs = []
|
||||
const files = []
|
||||
for (const { path: folderPath, folder } of folders) {
|
||||
for (const doc of folder.docs || []) {
|
||||
if (doc != null) {
|
||||
docs.push({ path: path.join(folderPath, doc.name), doc })
|
||||
}
|
||||
}
|
||||
for (const file of folder.fileRefs || []) {
|
||||
if (file != null) {
|
||||
files.push({ path: path.join(folderPath, file.name), file })
|
||||
}
|
||||
getAllEntitiesFromProject(project) {
|
||||
const folders = ProjectEntityHandler._getAllFoldersFromProject(project)
|
||||
const docs = []
|
||||
const files = []
|
||||
for (const { path: folderPath, folder } of folders) {
|
||||
for (const doc of folder.docs || []) {
|
||||
if (doc != null) {
|
||||
docs.push({ path: path.join(folderPath, doc.name), doc })
|
||||
}
|
||||
}
|
||||
callback(null, docs, files, folders)
|
||||
})
|
||||
for (const file of folder.fileRefs || []) {
|
||||
if (file != null) {
|
||||
files.push({ path: path.join(folderPath, file.name), file })
|
||||
}
|
||||
}
|
||||
}
|
||||
return { docs, files, folders }
|
||||
},
|
||||
|
||||
getAllDocPathsFromProjectById(projectId, callback) {
|
||||
@@ -104,23 +101,20 @@ const ProjectEntityHandler = {
|
||||
if (project == null) {
|
||||
return callback(Errors.NotFoundError('no project'))
|
||||
}
|
||||
ProjectEntityHandler.getAllDocPathsFromProject(project, callback)
|
||||
const docPaths = ProjectEntityHandler.getAllDocPathsFromProject(project)
|
||||
callback(null, docPaths)
|
||||
})
|
||||
},
|
||||
|
||||
getAllDocPathsFromProject(project, callback) {
|
||||
ProjectEntityHandler._getAllFoldersFromProject(project, (err, folders) => {
|
||||
if (err != null) {
|
||||
return callback(err)
|
||||
getAllDocPathsFromProject(project) {
|
||||
const folders = ProjectEntityHandler._getAllFoldersFromProject(project)
|
||||
const docPath = {}
|
||||
for (const { path: folderPath, folder } of folders) {
|
||||
for (const doc of folder.docs || []) {
|
||||
docPath[doc._id] = path.join(folderPath, doc.name)
|
||||
}
|
||||
const docPath = {}
|
||||
for (const { path: folderPath, folder } of folders) {
|
||||
for (const doc of folder.docs || []) {
|
||||
docPath[doc._id] = path.join(folderPath, doc.name)
|
||||
}
|
||||
}
|
||||
callback(null, docPath)
|
||||
})
|
||||
}
|
||||
return docPath
|
||||
},
|
||||
|
||||
getDoc(projectId, docId, options, callback) {
|
||||
@@ -205,11 +199,12 @@ const ProjectEntityHandler = {
|
||||
if (project == null) {
|
||||
return callback(new Errors.NotFoundError('no project'))
|
||||
}
|
||||
ProjectEntityHandler._getAllFoldersFromProject(project, callback)
|
||||
const folders = ProjectEntityHandler._getAllFoldersFromProject(project)
|
||||
callback(null, folders)
|
||||
})
|
||||
},
|
||||
|
||||
_getAllFoldersFromProject(project, callback) {
|
||||
_getAllFoldersFromProject(project) {
|
||||
const folders = []
|
||||
function processFolder(basePath, folder) {
|
||||
folders.push({ path: basePath, folder })
|
||||
@@ -221,15 +216,15 @@ const ProjectEntityHandler = {
|
||||
}
|
||||
|
||||
processFolder('/', project.rootFolder[0])
|
||||
callback(null, folders)
|
||||
return folders
|
||||
},
|
||||
}
|
||||
|
||||
module.exports = ProjectEntityHandler
|
||||
module.exports.promises = promisifyAll(ProjectEntityHandler, {
|
||||
without: ['getAllEntitiesFromProject'],
|
||||
multiResult: {
|
||||
getAllEntities: ['docs', 'files'],
|
||||
getAllEntitiesFromProject: ['docs', 'files'],
|
||||
getDoc: ['lines', 'rev', 'version', 'ranges'],
|
||||
},
|
||||
})
|
||||
|
||||
@@ -301,7 +301,7 @@ async function moveEntity(projectId, entityId, destFolderId, entityType) {
|
||||
const {
|
||||
docs: oldDocs,
|
||||
files: oldFiles,
|
||||
} = await ProjectEntityHandler.promises.getAllEntitiesFromProject(project)
|
||||
} = ProjectEntityHandler.getAllEntitiesFromProject(project)
|
||||
// For safety, insert the entity in the destination
|
||||
// location first, and then remove the original. If
|
||||
// there is an error the entity may appear twice. This
|
||||
@@ -331,7 +331,7 @@ async function moveEntity(projectId, entityId, destFolderId, entityType) {
|
||||
const {
|
||||
docs: newDocs,
|
||||
files: newFiles,
|
||||
} = await ProjectEntityHandler.promises.getAllEntitiesFromProject(newProject)
|
||||
} = ProjectEntityHandler.getAllEntitiesFromProject(newProject)
|
||||
const startPath = entityPath.fileSystem
|
||||
const endPath = result.path.fileSystem
|
||||
const changes = {
|
||||
@@ -421,7 +421,7 @@ async function renameEntity(
|
||||
const {
|
||||
docs: oldDocs,
|
||||
files: oldFiles,
|
||||
} = await ProjectEntityHandler.promises.getAllEntitiesFromProject(project)
|
||||
} = ProjectEntityHandler.getAllEntitiesFromProject(project)
|
||||
|
||||
// we need to increment the project version number for any structure change
|
||||
const newProject = await Project.findOneAndUpdate(
|
||||
@@ -433,7 +433,7 @@ async function renameEntity(
|
||||
const {
|
||||
docs: newDocs,
|
||||
files: newFiles,
|
||||
} = await ProjectEntityHandler.promises.getAllEntitiesFromProject(newProject)
|
||||
} = ProjectEntityHandler.getAllEntitiesFromProject(newProject)
|
||||
return {
|
||||
project,
|
||||
startPath,
|
||||
|
||||
@@ -1349,42 +1349,39 @@ const ProjectEntityUpdateHandler = {
|
||||
return callback(error)
|
||||
}
|
||||
|
||||
ProjectEntityHandler.getAllEntitiesFromProject(
|
||||
project,
|
||||
(error, docs, files, folders) => {
|
||||
if (error != null) {
|
||||
let {
|
||||
docs,
|
||||
files,
|
||||
folders,
|
||||
} = ProjectEntityHandler.getAllEntitiesFromProject(project)
|
||||
// _checkFileTree() must be passed the folders before docs and
|
||||
// files
|
||||
ProjectEntityUpdateHandler._checkFiletree(
|
||||
projectId,
|
||||
projectHistoryId,
|
||||
[...folders, ...docs, ...files],
|
||||
error => {
|
||||
if (error) {
|
||||
return callback(error)
|
||||
}
|
||||
// _checkFileTree() must be passed the folders before docs and
|
||||
// files
|
||||
ProjectEntityUpdateHandler._checkFiletree(
|
||||
docs = _.map(docs, doc => ({
|
||||
doc: doc.doc._id,
|
||||
path: doc.path,
|
||||
}))
|
||||
|
||||
files = _.map(files, file => ({
|
||||
file: file.file._id,
|
||||
path: file.path,
|
||||
url: FileStoreHandler._buildUrl(projectId, file.file._id),
|
||||
_hash: file.file.hash,
|
||||
}))
|
||||
|
||||
DocumentUpdaterHandler.resyncProjectHistory(
|
||||
projectId,
|
||||
projectHistoryId,
|
||||
[...folders, ...docs, ...files],
|
||||
error => {
|
||||
if (error) {
|
||||
return callback(error)
|
||||
}
|
||||
docs = _.map(docs, doc => ({
|
||||
doc: doc.doc._id,
|
||||
path: doc.path,
|
||||
}))
|
||||
|
||||
files = _.map(files, file => ({
|
||||
file: file.file._id,
|
||||
path: file.path,
|
||||
url: FileStoreHandler._buildUrl(projectId, file.file._id),
|
||||
_hash: file.file.hash,
|
||||
}))
|
||||
|
||||
DocumentUpdaterHandler.resyncProjectHistory(
|
||||
projectId,
|
||||
projectHistoryId,
|
||||
docs,
|
||||
files,
|
||||
callback
|
||||
)
|
||||
}
|
||||
docs,
|
||||
files,
|
||||
callback
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user