Merge pull request #597 from sharelatex/sk-add-waitfor-function-to-ide

Add a `waitFor` helper to the ide
This commit is contained in:
Shane Kilkelly 2018-05-25 15:16:50 +01:00 committed by GitHub
commit 0802403ee5
4 changed files with 45 additions and 29 deletions

View file

@ -35,6 +35,7 @@ define [
"directives/videoPlayState"
"services/queued-http"
"services/validateCaptcha"
"services/wait-for"
"filters/formatDate"
"main/event"
"main/account-upgrade"
@ -55,7 +56,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 = () ->) ->

View file

@ -2,7 +2,7 @@ define [
"base"
"moment"
], (App, moment) ->
App.controller "BinaryFileController", ["$scope", "$rootScope", "$http", "$timeout", "$element", "ide", ($scope, $rootScope, $http, $timeout, $element, ide) ->
App.controller "BinaryFileController", ["$scope", "$rootScope", "$http", "$timeout", "$element", "ide", "waitFor", ($scope, $rootScope, $http, $timeout, $element, ide, waitFor) ->
TWO_MEGABYTES = 2 * 1024 * 1024
@ -47,18 +47,6 @@ define [
else
return url
_tryOpenFile = (new_file_id) ->
iterations = 0
do tryOpen = () ->
if iterations > 10
return
iterations += 1
newFile = ide.fileTreeManager.findEntityById(new_file_id)
if newFile?
ide.binaryFilesManager.openFile(newFile)
else
setTimeout(tryOpen, 500)
$scope.refreshFile = (file) ->
$scope.refreshing = true
$scope.refreshError = null
@ -68,7 +56,15 @@ define [
{ new_file_id } = data
$timeout(
() ->
_tryOpenFile(new_file_id)
waitFor(
() ->
ide.fileTreeManager.findEntityById(new_file_id)
5000
)
.then (newFile) ->
ide.binaryFilesManager.openFile(newFile)
.catch (err) ->
console.warn(err)
, 0
)
$scope.refreshError = null

View file

@ -1,7 +1,7 @@
define [
"base"
], (App) ->
App.controller "HistoryV2DiffController", ($scope, ide, event_tracking) ->
App.controller "HistoryV2DiffController", ($scope, ide, event_tracking, waitFor) ->
$scope.restoreState =
inflight: false
error: false
@ -24,17 +24,16 @@ define [
$scope.restoreState.inflight = false
openEntity = (data) ->
iterations = 0
{id, type} = data
do tryOpen = () ->
if iterations > 5
return
iterations += 1
entity = ide.fileTreeManager.findEntityById(id)
if entity? and type == 'doc'
ide.editorManager.openDoc(entity)
else if entity? and type == 'file'
ide.binaryFilesManager.openFile(entity)
else
setTimeout(tryOpen, 500)
waitFor(
() ->
ide.fileTreeManager.findEntityById(id)
3000
)
.then (entity) ->
if type == 'doc'
ide.editorManager.openDoc(entity)
else if type == 'file'
ide.binaryFilesManager.openFile(entity)
.catch (err) ->
console.warn(err)

View file

@ -0,0 +1,20 @@
define [
"base"
], (App) ->
App.factory "waitFor", ($q) ->
waitFor = (testFunction, timeout, pollInterval=500) ->
iterationLimit = Math.floor(timeout / pollInterval)
iterations = 0
$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)
)
return waitFor