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