modfied setRootDocAutomatically to work async

was causing cpu to block for along time on big projects
This commit is contained in:
Henry Oswald 2014-11-26 17:19:21 +00:00
parent 393a637673
commit 5e570d52a0
2 changed files with 30 additions and 20 deletions

View file

@ -1,18 +1,27 @@
ProjectEntityHandler = require "./ProjectEntityHandler" ProjectEntityHandler = require "./ProjectEntityHandler"
Path = require "path" Path = require "path"
async = require("async")
_ = require("underscore")
module.exports = ProjectRootDocManager = module.exports = ProjectRootDocManager =
setRootDocAutomatically: (project_id, callback = (error) ->) -> setRootDocAutomatically: (project_id, callback = (error) ->) ->
ProjectEntityHandler.getAllDocs project_id, (error, docs) -> ProjectEntityHandler.getAllDocs project_id, (error, docs) ->
return callback(error) if error? return callback(error) if error?
root_doc_id = null
for path, doc of docs
for line in doc.lines || [] root_doc_id = null
match = line.match /(.*)\\documentclass/ # no lookbehind in js regexp :( jobs = _.map docs, (doc, path)->
if Path.extname(path).match(/\.R?tex$/) and match and !match[1].match /%/ return (cb)->
root_doc_id = doc._id for line in doc.lines || []
if root_doc_id? match = line.match /(.*)\\documentclass/ # no lookbehind in js regexp :(
ProjectEntityHandler.setRootDoc project_id, root_doc_id, callback isRootDoc = Path.extname(path).match(/\.R?tex$/) and match and !match[1].match /%/
else if isRootDoc
callback() 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()

View file

@ -14,7 +14,7 @@ describe 'ProjectRootDocManager', ->
describe "setRootDocAutomatically", -> describe "setRootDocAutomatically", ->
describe "when there is a suitable root doc", -> describe "when there is a suitable root doc", ->
beforeEach -> beforeEach (done)->
@docs = @docs =
"/chapter1.tex": "/chapter1.tex":
_id: "doc-id-1" _id: "doc-id-1"
@ -22,9 +22,16 @@ describe 'ProjectRootDocManager', ->
"/main.tex": "/main.tex":
_id: "doc-id-2" _id: "doc-id-2"
lines: ["\\documentclass{article}", "\\input{chapter1}"] 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.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
@ProjectEntityHandler.setRootDoc = sinon.stub().callsArgWith(2) @ProjectEntityHandler.setRootDoc = sinon.stub().callsArgWith(2)
@ProjectRootDocManager.setRootDocAutomatically @project_id, @callback @ProjectRootDocManager.setRootDocAutomatically @project_id, done
it "should check the docs of the project", -> it "should check the docs of the project", ->
@ProjectEntityHandler.getAllDocs.calledWith(@project_id) @ProjectEntityHandler.getAllDocs.calledWith(@project_id)
@ -34,9 +41,6 @@ describe 'ProjectRootDocManager', ->
@ProjectEntityHandler.setRootDoc.calledWith(@project_id, "doc-id-2") @ProjectEntityHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
.should.equal true .should.equal true
it "should call the callback", ->
@callback.called.should.equal true
describe "when the root doc is an Rtex file", -> describe "when the root doc is an Rtex file", ->
beforeEach -> beforeEach ->
@docs = @docs =
@ -55,7 +59,7 @@ describe 'ProjectRootDocManager', ->
.should.equal true .should.equal true
describe "when there is no suitable root doc", -> describe "when there is no suitable root doc", ->
beforeEach -> beforeEach (done)->
@docs = @docs =
"/chapter1.tex": "/chapter1.tex":
_id: "doc-id-1" _id: "doc-id-1"
@ -65,11 +69,8 @@ describe 'ProjectRootDocManager', ->
lines: ["%Example: \\documentclass{article}"] lines: ["%Example: \\documentclass{article}"]
@ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs) @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
@ProjectEntityHandler.setRootDoc = sinon.stub().callsArgWith(2) @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", -> it "should not set the root doc to the doc containing a documentclass", ->
@ProjectEntityHandler.setRootDoc.called.should.equal false @ProjectEntityHandler.setRootDoc.called.should.equal false
it "should call the callback", ->
@callback.called.should.equal true