Răsfoiți Sursa

Move `waitFor` into an angular service

Shane Kilkelly 8 ani în urmă
părinte
comite
105d858155

+ 1 - 16
services/web/public/coffee/ide.coffee

@@ -35,6 +35,7 @@ define [
 	"directives/videoPlayState"
 	"directives/videoPlayState"
 	"services/queued-http"
 	"services/queued-http"
 	"services/validateCaptcha"
 	"services/validateCaptcha"
+	"services/wait-for"
 	"filters/formatDate"
 	"filters/formatDate"
 	"main/event"
 	"main/event"
 	"main/account-upgrade"
 	"main/account-upgrade"
@@ -229,20 +230,4 @@ define [
 				ide.$scope.project.publicAccesLevel = data.newAccessLevel
 				ide.$scope.project.publicAccesLevel = data.newAccessLevel
 				$scope.$digest()
 				$scope.$digest()
 
 
-		ide.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)
-			)
-
 	angular.bootstrap(document.body, ["SharelatexApp"])
 	angular.bootstrap(document.body, ["SharelatexApp"])

+ 2 - 2
services/web/public/coffee/ide/binary-files/controllers/BinaryFileController.coffee

@@ -2,7 +2,7 @@ define [
 	"base"
 	"base"
 	"moment"
 	"moment"
 ], (App, 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
 		TWO_MEGABYTES = 2 * 1024 * 1024
 
 
@@ -56,7 +56,7 @@ define [
 					{ new_file_id } = data
 					{ new_file_id } = data
 					$timeout(
 					$timeout(
 						() ->
 						() ->
-							ide.waitFor(
+							waitFor(
 								() ->
 								() ->
 									ide.fileTreeManager.findEntityById(new_file_id)
 									ide.fileTreeManager.findEntityById(new_file_id)
 								5000
 								5000

+ 2 - 2
services/web/public/coffee/ide/history/controllers/HistoryV2DiffController.coffee

@@ -1,7 +1,7 @@
 define [
 define [
 	"base"
 	"base"
 ], (App) ->
 ], (App) ->
-	App.controller "HistoryV2DiffController", ($scope, ide, event_tracking) ->
+	App.controller "HistoryV2DiffController", ($scope, ide, event_tracking, waitFor) ->
 		$scope.restoreState =
 		$scope.restoreState =
 			inflight: false
 			inflight: false
 			error: false
 			error: false
@@ -25,7 +25,7 @@ define [
 
 
 		openEntity = (data) ->
 		openEntity = (data) ->
 			{id, type} = data
 			{id, type} = data
-			ide.waitFor(
+			waitFor(
 				() ->
 				() ->
 					ide.fileTreeManager.findEntityById(id)
 					ide.fileTreeManager.findEntityById(id)
 				3000
 				3000

+ 20 - 0
services/web/public/coffee/services/wait-for.coffee

@@ -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