Use a promise (with Angular's $q) in waitFor

This commit is contained in:
Shane Kilkelly 2018-05-25 11:42:13 +01:00
parent 8be4279165
commit cfc17d56e8
3 changed files with 23 additions and 16 deletions

View file

@ -55,7 +55,7 @@ define [
SafariScrollPatcher
) ->
App.controller "IdeController", ($scope, $timeout, ide, localStorage, sixpack, event_tracking, metadata) ->
App.controller "IdeController", ($scope, $timeout, ide, localStorage, sixpack, event_tracking, metadata, $q) ->
# Don't freak out if we're already in an apply callback
$scope.$originalApply = $scope.$apply
$scope.$apply = (fn = () ->) ->
@ -229,17 +229,20 @@ define [
ide.$scope.project.publicAccesLevel = data.newAccessLevel
$scope.$digest()
ide.waitFor = (testFunction, callback, timeout, pollInterval=500) ->
ide.waitFor = (testFunction, timeout, pollInterval=500) ->
iterationLimit = Math.floor(timeout / pollInterval)
iterations = 0
do tryIteration = () ->
if iterations > iterationLimit
return
iterations += 1
result = testFunction()
if result?
callback(result)
else
setTimeout(tryIteration, pollInterval)
$q(
(resolve, reject) ->
do tryIteration = () ->
if iterations > iterationLimit
return reject(new Error("waiting too long, #{JSON.stringify({timeout, pollInterval})}"))
iterations += 1
result = testFunction()
if result?
resolve(result)
else
setTimeout(tryIteration, pollInterval)
)
angular.bootstrap(document.body, ["SharelatexApp"])

View file

@ -59,10 +59,12 @@ define [
ide.waitFor(
() ->
ide.fileTreeManager.findEntityById(new_file_id)
(newFile) ->
ide.binaryFilesManager.openFile(newFile)
5000
)
.then (newFile) ->
ide.binaryFilesManager.openFile(newFile)
.catch (err) ->
console.warn(err)
, 0
)
$scope.refreshError = null

View file

@ -28,10 +28,12 @@ define [
ide.waitFor(
() ->
ide.fileTreeManager.findEntityById(id)
(entity) ->
3000
)
.then (entity) ->
if type == 'doc'
ide.editorManager.openDoc(entity)
else if type == 'file'
ide.binaryFilesManager.openFile(entity)
3000
)
.catch (err) ->
console.warn(err)