| 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 shelf.util; | 5 library shelf.util; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:stack_trace/stack_trace.dart'; | 9 import 'package:stack_trace/stack_trace.dart'; |
| 10 | 10 |
| 11 /// Like [new Future], but avoids around issue 11911 by using [new Future.value] |
| 12 /// under the covers. |
| 13 Future newFuture(callback()) => new Future.value().then((_) => callback()); |
| 14 |
| 11 /// Like [Future.sync], but wraps the Future in [Chain.track] as well. | 15 /// Like [Future.sync], but wraps the Future in [Chain.track] as well. |
| 12 Future syncFuture(callback()) => Chain.track(new Future.sync(callback)); | 16 Future syncFuture(callback()) => Chain.track(new Future.sync(callback)); |
| 13 | 17 |
| 14 /// Run [callback] and capture any errors that would otherwise be top-leveled. | 18 /// Run [callback] and capture any errors that would otherwise be top-leveled. |
| 15 /// | 19 /// |
| 16 /// If [this] is called in a non-root error zone, it will just run [callback] | 20 /// If [this] is called in a non-root error zone, it will just run [callback] |
| 17 /// and return the result. Otherwise, it will capture any errors using | 21 /// and return the result. Otherwise, it will capture any errors using |
| 18 /// [runZoned] and pass them to [onError]. | 22 /// [runZoned] and pass them to [onError]. |
| 19 catchTopLevelErrors(callback(), void onError(error, StackTrace stackTrace)) { | 23 catchTopLevelErrors(callback(), void onError(error, StackTrace stackTrace)) { |
| 20 if (Zone.current.inSameErrorZone(Zone.ROOT)) { | 24 if (Zone.current.inSameErrorZone(Zone.ROOT)) { |
| 21 return runZoned(callback, onError: onError); | 25 return runZoned(callback, onError: onError); |
| 22 } else { | 26 } else { |
| 23 return callback(); | 27 return callback(); |
| 24 } | 28 } |
| 25 } | 29 } |
| 26 | 30 |
| 27 /// Returns a [Map] with the values from [original] and the values from | 31 /// Returns a [Map] with the values from [original] and the values from |
| 28 /// [updates]. | 32 /// [updates]. |
| 29 /// | 33 /// |
| 30 /// For keys that are the same between [original] and [updates], the value in | 34 /// For keys that are the same between [original] and [updates], the value in |
| 31 /// [updates] is used. | 35 /// [updates] is used. |
| 32 /// | 36 /// |
| 33 /// If [updates] is `null` or empty, [original] is returned unchanged. | 37 /// If [updates] is `null` or empty, [original] is returned unchanged. |
| 34 Map updateMap(Map original, Map updates) { | 38 Map updateMap(Map original, Map updates) { |
| 35 if (updates == null || updates.isEmpty) return original; | 39 if (updates == null || updates.isEmpty) return original; |
| 36 | 40 |
| 37 return new Map.from(original) | 41 return new Map.from(original) |
| 38 ..addAll(updates); | 42 ..addAll(updates); |
| 39 } | 43 } |
| OLD | NEW |