TrackChangesApp.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* eslint-disable
  2. handle-callback-err,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS103: Rewrite code to no longer use __guard__
  11. * DS205: Consider reworking code to avoid use of IIFEs
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const app = require('../../../../app')
  16. const { waitForDb } = require('../../../../app/js/mongodb')
  17. const logger = require('logger-sharelatex')
  18. const Settings = require('@overleaf/settings')
  19. module.exports = {
  20. running: false,
  21. initing: false,
  22. callbacks: [],
  23. ensureRunning(callback) {
  24. if (callback == null) {
  25. callback = function (error) {}
  26. }
  27. if (this.running) {
  28. return callback()
  29. } else if (this.initing) {
  30. return this.callbacks.push(callback)
  31. }
  32. this.initing = true
  33. this.callbacks.push(callback)
  34. waitForDb().then(() => {
  35. return app.listen(
  36. __guard__(
  37. Settings.internal != null
  38. ? Settings.internal.trackchanges
  39. : undefined,
  40. x => x.port
  41. ),
  42. 'localhost',
  43. error => {
  44. if (error != null) {
  45. throw error
  46. }
  47. this.running = true
  48. logger.log('track changes running in dev mode')
  49. return (() => {
  50. const result = []
  51. for (callback of Array.from(this.callbacks)) {
  52. result.push(callback())
  53. }
  54. return result
  55. })()
  56. }
  57. )
  58. })
  59. },
  60. }
  61. function __guard__(value, transform) {
  62. return typeof value !== 'undefined' && value !== null
  63. ? transform(value)
  64. : undefined
  65. }