瀏覽代碼

Improve UX of triggering autocompile.

If a user is making infrequent edits (i.e. if reading and making small
changes), then waiting 5 seconds for a recompile is bad. Therefore we track
the time since the last recompile and use this to decide whether a recompile
should be run. This reduces the time to recompile, unless the user is typing
for a significant amount of time.
Alasdair Smith 9 年之前
父節點
當前提交
4a490aafbf
共有 1 個文件被更改,包括 18 次插入 和 3 次删除
  1. 18 3
      services/web/public/coffee/ide/pdf/controllers/PdfController.coffee

+ 18 - 3
services/web/public/coffee/ide/pdf/controllers/PdfController.coffee

@@ -6,6 +6,7 @@ define [
 	"services/log-hints-feedback"
 ], (App, Ace, HumanReadableLogs, BibLogParser) ->
 	AUTO_COMPILE_TIMEOUT = 5000
+	OP_ACKNOWLEDGEMENT_TIMEOUT = 1100
 
 	App.controller "PdfController", ($scope, $http, ide, $modal, synctex, event_tracking, logHintsFeedback, localStorage) ->
 		# enable per-user containers by default
@@ -74,14 +75,26 @@ define [
 			$scope.pdf.view = 'errors'
 			$scope.pdf.renderingError = true
 
+		autoCompileTimeout = null
 		triggerAutoCompile = () ->
-			if (!ide.$scope.hasLintingError)
-				$scope.recompile(isBackgroundAutoCompile: true)
+			return if autoCompileTimeout
+
+			timeSinceLastCompile = Date.now() - $scope.recompiledAt
+
+			if timeSinceLastCompile >= AUTO_COMPILE_TIMEOUT
+				if (!ide.$scope.hasLintingError)
+					$scope.recompile(isBackgroundAutoCompile: true)
+			else
+				# Extend remainder of timeout
+				autoCompileTimeout = setTimeout () ->
+					autoCompileTimeout = null
+					triggerAutoCompile()
+				, AUTO_COMPILE_TIMEOUT - timeSinceLastCompile
 
 		autoCompileListener = null
 		toggleAutoCompile = (enabling) ->
 			if enabling
-				autoCompileListener = ide.$scope.$on "ide:opAcknowledged", _.debounce(triggerAutoCompile, AUTO_COMPILE_TIMEOUT)
+				autoCompileListener = ide.$scope.$on "ide:opAcknowledged", _.debounce(triggerAutoCompile, OP_ACKNOWLEDGEMENT_TIMEOUT)
 			else
 				autoCompileListener() if autoCompileListener
 				autoCompileListener = null
@@ -422,6 +435,8 @@ define [
 					$scope.pdf.renderingError = false
 					$scope.pdf.error = true
 					$scope.pdf.view = 'errors'
+				.finally () ->
+					$scope.recompiledAt = Date.now()
 
 		# This needs to be public.
 		ide.$scope.recompile = $scope.recompile