Просмотр исходного кода

modfied setRootDocAutomatically to work async

was causing cpu to block for along time on big projects
Henry Oswald 12 лет назад
Родитель
Сommit
5e570d52a0

+ 18 - 9
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()
+			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()
 

+ 11 - 10
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
-