| OLD | NEW |
| (Empty) | |
| 1 library server.operation; |
| 2 |
| 3 import 'dart:async'; |
| 4 |
| 5 import 'package:analysis_server/src/protocol.dart'; |
| 6 import 'package:logging/logging.dart'; |
| 7 |
| 8 import 'driver.dart'; |
| 9 |
| 10 class InfoOperation extends Operation { |
| 11 final String message; |
| 12 |
| 13 InfoOperation(this.message); |
| 14 |
| 15 @override |
| 16 Future perform(Driver driver) { |
| 17 driver.logger.log(Level.INFO, message); |
| 18 return null; |
| 19 } |
| 20 } |
| 21 |
| 22 /** |
| 23 * An [Operation] represents an action such as sending a request to the server. |
| 24 */ |
| 25 abstract class Operation { |
| 26 Future perform(Driver driver); |
| 27 } |
| 28 |
| 29 /** |
| 30 * A [RequestOperation] sends a [JSON] request to the server. |
| 31 */ |
| 32 class RequestOperation extends Operation { |
| 33 final Map<String, dynamic> json; |
| 34 |
| 35 RequestOperation(this.json); |
| 36 |
| 37 @override |
| 38 Future perform(Driver driver) { |
| 39 String method = json['method']; |
| 40 driver.logger.log(Level.FINE, 'Sending request: $method\n $json'); |
| 41 driver.send(method, json['params']).then((response) { |
| 42 driver.logger.log(Level.FINE, 'Response received: $method : $response'); |
| 43 }).catchError((e, s) { |
| 44 driver.logger.log(Level.WARNING, 'Request failed: $method\n $e\n$s'); |
| 45 throw 'Send request failed: $e'; |
| 46 }); |
| 47 return null; |
| 48 } |
| 49 } |
| 50 |
| 51 class StartServerOperation extends Operation { |
| 52 @override |
| 53 Future perform(Driver driver) { |
| 54 return driver.startServer(); |
| 55 } |
| 56 } |
| 57 |
| 58 class WaitForAnalysisCompleteOperation extends Operation { |
| 59 @override |
| 60 Future perform(Driver driver) { |
| 61 DateTime start = new DateTime.now(); |
| 62 driver.logger.log(Level.FINE, 'waiting for analysis to complete'); |
| 63 StreamSubscription<ServerStatusParams> subscription; |
| 64 Timer timer; |
| 65 Completer completer = new Completer(); |
| 66 bool isAnalyzing = false; |
| 67 subscription = driver.onServerStatus.listen((ServerStatusParams params) { |
| 68 // TODO (danrubel) ensure that server.setSubscriptions STATUS is set |
| 69 if (params.analysis != null) { |
| 70 if (params.analysis.isAnalyzing) { |
| 71 isAnalyzing = true; |
| 72 } else { |
| 73 subscription.cancel(); |
| 74 timer.cancel(); |
| 75 DateTime end = new DateTime.now(); |
| 76 Duration delta = end.difference(start); |
| 77 driver.logger.log(Level.FINE, 'analysis complete after $delta'); |
| 78 completer.complete(); |
| 79 driver.results.record('analysis complete', delta); |
| 80 } |
| 81 } |
| 82 }); |
| 83 timer = new Timer.periodic(new Duration(milliseconds: 20), (_) { |
| 84 if (!isAnalyzing) { |
| 85 // TODO (danrubel) revisit this once source change requests are implemen
ted |
| 86 subscription.cancel(); |
| 87 timer.cancel(); |
| 88 driver.logger.log(Level.INFO, 'analysis never started'); |
| 89 completer.complete(); |
| 90 return; |
| 91 } |
| 92 // Timeout if no communcation received within the last 10 seconds. |
| 93 double currentTime = driver.server.currentElapseTime; |
| 94 double lastTime = driver.server.lastCommunicationTime; |
| 95 if (currentTime - lastTime > 10) { |
| 96 subscription.cancel(); |
| 97 timer.cancel(); |
| 98 String message = 'gave up waiting for analysis to complete'; |
| 99 driver.logger.log(Level.WARNING, message); |
| 100 completer.completeError(message); |
| 101 } |
| 102 }); |
| 103 return completer.future; |
| 104 } |
| 105 } |
| OLD | NEW |