| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 utils; | 5 library utils; |
| 6 | 6 |
| 7 import 'dart:coreimpl'; | 7 import 'dart:coreimpl'; |
| 8 import 'package:web_components/src/messages.dart'; | 8 import 'package:web_components/src/messages.dart'; |
| 9 | 9 |
| 10 /** | 10 /** |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 /** | 26 /** |
| 27 * Invokes [callback], logs how long it took to execute in ms, and returns | 27 * Invokes [callback], logs how long it took to execute in ms, and returns |
| 28 * whatever [callback] returns. The log message will be printed if [printTime] | 28 * whatever [callback] returns. The log message will be printed if [printTime] |
| 29 * is true. | 29 * is true. |
| 30 */ | 30 */ |
| 31 time(String logMessage, callback(), {bool printTime: false}) { | 31 time(String logMessage, callback(), {bool printTime: false}) { |
| 32 final watch = new Stopwatch(); | 32 final watch = new Stopwatch(); |
| 33 watch.start(); | 33 watch.start(); |
| 34 var result = callback(); | 34 var result = callback(); |
| 35 watch.stop(); | 35 watch.stop(); |
| 36 final duration = watch.elapsedInMs(); | 36 final duration = watch.elapsedMilliseconds; |
| 37 if (printTime) { | 37 if (printTime) { |
| 38 _printMessage(logMessage, duration); | 38 _printMessage(logMessage, duration); |
| 39 } | 39 } |
| 40 return result; | 40 return result; |
| 41 } | 41 } |
| 42 | 42 |
| 43 /** | 43 /** |
| 44 * Invokes [callback], logs how long it takes from the moment [callback] is | 44 * Invokes [callback], logs how long it takes from the moment [callback] is |
| 45 * executed until the future it returns is completed. Returns the future | 45 * executed until the future it returns is completed. Returns the future |
| 46 * returned by [callback]. The log message will be printed if [printTime] | 46 * returned by [callback]. The log message will be printed if [printTime] |
| 47 * is true. | 47 * is true. |
| 48 */ | 48 */ |
| 49 Future asyncTime(String logMessage, Future callback(), | 49 Future asyncTime(String logMessage, Future callback(), |
| 50 {bool printTime: false}) { | 50 {bool printTime: false}) { |
| 51 final watch = new Stopwatch(); | 51 final watch = new Stopwatch(); |
| 52 watch.start(); | 52 watch.start(); |
| 53 return callback()..then((_) { | 53 return callback()..then((_) { |
| 54 watch.stop(); | 54 watch.stop(); |
| 55 final duration = watch.elapsedInMs(); | 55 final duration = watch.elapsedMilliseconds; |
| 56 if (printTime) { | 56 if (printTime) { |
| 57 _printMessage(logMessage, duration); | 57 _printMessage(logMessage, duration); |
| 58 } | 58 } |
| 59 }); | 59 }); |
| 60 } | 60 } |
| 61 | 61 |
| 62 void _printMessage(String logMessage, int duration) { | 62 void _printMessage(String logMessage, int duration) { |
| 63 var buf = new StringBuffer(); | 63 var buf = new StringBuffer(); |
| 64 buf.add(logMessage); | 64 buf.add(logMessage); |
| 65 for (int i = logMessage.length; i < 60; i++) buf.add(' '); | 65 for (int i = logMessage.length; i < 60; i++) buf.add(' '); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 _pending--; | 110 _pending--; |
| 111 if (_pending == 0) { | 111 if (_pending == 0) { |
| 112 _pending = _FINISHED; | 112 _pending = _FINISHED; |
| 113 _completer.complete(futures); | 113 _completer.complete(futures); |
| 114 } | 114 } |
| 115 }); | 115 }); |
| 116 } | 116 } |
| 117 | 117 |
| 118 Future<List> get future => _completer.future; | 118 Future<List> get future => _completer.future; |
| 119 } | 119 } |
| OLD | NEW |