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

View file

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

View file

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