From 5e570d52a0e52debcab5db37c0e0b8fff757ee5e Mon Sep 17 00:00:00 2001 From: Henry Oswald Date: Wed, 26 Nov 2014 17:19:21 +0000 Subject: [PATCH] modfied setRootDocAutomatically to work async was causing cpu to block for along time on big projects --- .../Project/ProjectRootDocManager.coffee | 29 ++++++++++++------- .../Project/ProjectRootDocManagerTests.coffee | 21 +++++++------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/services/web/app/coffee/Features/Project/ProjectRootDocManager.coffee b/services/web/app/coffee/Features/Project/ProjectRootDocManager.coffee index ca9dba02c6..579c12c742 100644 --- a/services/web/app/coffee/Features/Project/ProjectRootDocManager.coffee +++ b/services/web/app/coffee/Features/Project/ProjectRootDocManager.coffee @@ -1,18 +1,27 @@ ProjectEntityHandler = require "./ProjectEntityHandler" Path = require "path" +async = require("async") +_ = require("underscore") module.exports = ProjectRootDocManager = setRootDocAutomatically: (project_id, callback = (error) ->) -> ProjectEntityHandler.getAllDocs project_id, (error, docs) -> return callback(error) if error? - root_doc_id = null - for path, doc of docs - for line in doc.lines || [] - match = line.match /(.*)\\documentclass/ # no lookbehind in js regexp :( - if Path.extname(path).match(/\.R?tex$/) and match and !match[1].match /%/ - root_doc_id = doc._id - if root_doc_id? - ProjectEntityHandler.setRootDoc project_id, root_doc_id, callback - else - callback() + + + root_doc_id = null + jobs = _.map docs, (doc, path)-> + return (cb)-> + for line in doc.lines || [] + match = line.match /(.*)\\documentclass/ # no lookbehind in js regexp :( + isRootDoc = Path.extname(path).match(/\.R?tex$/) and match and !match[1].match /%/ + if isRootDoc + return cb(doc?._id) + else + return cb() + async.parallel jobs, (root_doc_id)-> + if root_doc_id? + ProjectEntityHandler.setRootDoc project_id, root_doc_id, callback + else + callback() diff --git a/services/web/test/UnitTests/coffee/Project/ProjectRootDocManagerTests.coffee b/services/web/test/UnitTests/coffee/Project/ProjectRootDocManagerTests.coffee index fbd12c6ed4..a155b1b388 100644 --- a/services/web/test/UnitTests/coffee/Project/ProjectRootDocManagerTests.coffee +++ b/services/web/test/UnitTests/coffee/Project/ProjectRootDocManagerTests.coffee @@ -14,7 +14,7 @@ describe 'ProjectRootDocManager', -> describe "setRootDocAutomatically", -> describe "when there is a suitable root doc", -> - beforeEach -> + beforeEach (done)-> @docs = "/chapter1.tex": _id: "doc-id-1" @@ -22,9 +22,16 @@ describe 'ProjectRootDocManager', -> "/main.tex": _id: "doc-id-2" lines: ["\\documentclass{article}", "\\input{chapter1}"] + "/nested/chapter1a.tex": + _id: "doc-id-3" + lines: ["Hello world"] + "/nested/chapter1b.tex": + _id: "doc-id-4" + lines: ["Hello world"] + @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs) @ProjectEntityHandler.setRootDoc = sinon.stub().callsArgWith(2) - @ProjectRootDocManager.setRootDocAutomatically @project_id, @callback + @ProjectRootDocManager.setRootDocAutomatically @project_id, done it "should check the docs of the project", -> @ProjectEntityHandler.getAllDocs.calledWith(@project_id) @@ -34,9 +41,6 @@ describe 'ProjectRootDocManager', -> @ProjectEntityHandler.setRootDoc.calledWith(@project_id, "doc-id-2") .should.equal true - it "should call the callback", -> - @callback.called.should.equal true - describe "when the root doc is an Rtex file", -> beforeEach -> @docs = @@ -55,7 +59,7 @@ describe 'ProjectRootDocManager', -> .should.equal true describe "when there is no suitable root doc", -> - beforeEach -> + beforeEach (done)-> @docs = "/chapter1.tex": _id: "doc-id-1" @@ -65,11 +69,8 @@ describe 'ProjectRootDocManager', -> lines: ["%Example: \\documentclass{article}"] @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs) @ProjectEntityHandler.setRootDoc = sinon.stub().callsArgWith(2) - @ProjectRootDocManager.setRootDocAutomatically @project_id, @callback + @ProjectRootDocManager.setRootDocAutomatically @project_id, done it "should not set the root doc to the doc containing a documentclass", -> @ProjectEntityHandler.setRootDoc.called.should.equal false - it "should call the callback", -> - @callback.called.should.equal true -