| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library operation.queue; | 5 library operation.queue; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/analysis_server.dart'; | 9 import 'package:analysis_server/src/analysis_server.dart'; |
| 10 import 'package:analysis_server/src/operation/operation.dart'; | 10 import 'package:analysis_server/src/operation/operation.dart'; |
| 11 import 'package:analyzer/src/generated/source.dart'; |
| 11 | 12 |
| 12 | 13 |
| 13 /** | 14 /** |
| 14 * A queue of operations in an [AnalysisServer]. | 15 * A queue of operations in an [AnalysisServer]. |
| 15 */ | 16 */ |
| 16 class ServerOperationQueue { | 17 class ServerOperationQueue { |
| 17 final List<Queue<ServerOperation>> _queues = <Queue<ServerOperation>>[]; | 18 final List<Queue<ServerOperation>> _queues = <Queue<ServerOperation>>[]; |
| 18 | 19 |
| 19 ServerOperationQueue() { | 20 ServerOperationQueue() { |
| 20 for (int i = 0; i < ServerOperationPriority.COUNT; i++) { | 21 for (int i = 0; i < ServerOperationPriority.COUNT; i++) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 44 /** | 45 /** |
| 45 * Removes all elements in the queue. | 46 * Removes all elements in the queue. |
| 46 */ | 47 */ |
| 47 void clear() { | 48 void clear() { |
| 48 for (Queue<ServerOperation> queue in _queues) { | 49 for (Queue<ServerOperation> queue in _queues) { |
| 49 queue.clear(); | 50 queue.clear(); |
| 50 } | 51 } |
| 51 } | 52 } |
| 52 | 53 |
| 53 /** | 54 /** |
| 54 * Return the next operation to perform, or `null` if the queue is empty. This | 55 * Return the next operation to perform, or `null` if the queue is empty. |
| 55 * method does not change the queue. | 56 * This method does not change the queue. |
| 56 */ | 57 */ |
| 57 ServerOperation peek() { | 58 ServerOperation peek() { |
| 58 for (Queue<ServerOperation> queue in _queues) { | 59 for (Queue<ServerOperation> queue in _queues) { |
| 59 if (!queue.isEmpty) { | 60 if (!queue.isEmpty) { |
| 60 return queue.first; | 61 return queue.first; |
| 61 } | 62 } |
| 62 } | 63 } |
| 63 return null; | 64 return null; |
| 64 } | 65 } |
| 65 | 66 |
| 66 /** | 67 /** |
| 67 * Reschedules queued operations according their current priorities. | 68 * Reschedules queued operations according their current priorities. |
| 68 */ | 69 */ |
| 69 void reschedule() { | 70 void reschedule() { |
| 70 // prepare all operations | 71 // prepare all operations |
| 71 List<ServerOperation> operations = <ServerOperation>[]; | 72 List<ServerOperation> operations = <ServerOperation>[]; |
| 72 for (Queue<ServerOperation> queue in _queues) { | 73 for (Queue<ServerOperation> queue in _queues) { |
| 73 operations.addAll(queue); | 74 operations.addAll(queue); |
| 74 queue.clear(); | 75 queue.clear(); |
| 75 } | 76 } |
| 76 // add all operations | 77 // add all operations |
| 77 operations.forEach(add); | 78 operations.forEach(add); |
| 78 } | 79 } |
| 79 | 80 |
| 80 /** | 81 /** |
| 82 * The given [source] if about to changed. |
| 83 */ |
| 84 void sourceAboutToChange(Source source) { |
| 85 for (Queue<ServerOperation> queue in _queues) { |
| 86 queue.removeWhere((operation) { |
| 87 if (operation is SourceSensitiveOperation) { |
| 88 return operation.shouldBeDiscardedOnSourceChange(source); |
| 89 } |
| 90 return false; |
| 91 }); |
| 92 } |
| 93 } |
| 94 |
| 95 /** |
| 81 * Returns the next operation to perform or `null` if empty. | 96 * Returns the next operation to perform or `null` if empty. |
| 82 */ | 97 */ |
| 83 ServerOperation take() { | 98 ServerOperation take() { |
| 84 for (Queue<ServerOperation> queue in _queues) { | 99 for (Queue<ServerOperation> queue in _queues) { |
| 85 if (!queue.isEmpty) { | 100 if (!queue.isEmpty) { |
| 86 return queue.removeFirst(); | 101 return queue.removeFirst(); |
| 87 } | 102 } |
| 88 } | 103 } |
| 89 return null; | 104 return null; |
| 90 } | 105 } |
| 106 |
| 107 /** |
| 108 * Returns an operation that satisfies the given [test] or `null`. |
| 109 */ |
| 110 ServerOperation takeIf(bool test(ServerOperation operation)) { |
| 111 for (Queue<ServerOperation> queue in _queues) { |
| 112 for (var operation in queue) { |
| 113 if (test(operation)) { |
| 114 queue.remove(operation); |
| 115 return operation; |
| 116 } |
| 117 } |
| 118 } |
| 119 return null; |
| 120 } |
| 91 } | 121 } |
| OLD | NEW |