DocstoreApp.js 965 B

12345678910111213141516171819202122232425262728293031323334353637
  1. const app = require('../../../../app')
  2. const { waitForDb } = require('../../../../app/js/mongodb')
  3. require('@overleaf/logger').logger.level('error')
  4. const settings = require('@overleaf/settings')
  5. module.exports = {
  6. running: false,
  7. initing: false,
  8. callbacks: [],
  9. ensureRunning(callback) {
  10. if (callback == null) {
  11. callback = function () {}
  12. }
  13. if (this.running) {
  14. return callback()
  15. } else if (this.initing) {
  16. return this.callbacks.push(callback)
  17. }
  18. this.initing = true
  19. this.callbacks.push(callback)
  20. waitForDb().then(() => {
  21. return app.listen(settings.internal.docstore.port, 'localhost', error => {
  22. if (error != null) {
  23. throw error
  24. }
  25. this.running = true
  26. return (() => {
  27. const result = []
  28. for (callback of Array.from(this.callbacks)) {
  29. result.push(callback())
  30. }
  31. return result
  32. })()
  33. })
  34. })
  35. },
  36. }