| 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 part of service; | 5 part of service; |
| 6 | 6 |
| 7 /// Abstract [ServiceObjectCache]. | 7 /// Abstract [ServiceObjectCache]. |
| 8 abstract class ServiceObjectCache<T extends ServiceObject> { | 8 abstract class ServiceObjectCache<T extends ServiceObject> { |
| 9 final Isolate isolate; | 9 final Isolate isolate; |
| 10 final _cache = new ObservableMap<String, T>(); | 10 final _cache = new ObservableMap<String, T>(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 operator[]=(String id, T serviceObject) { | 35 operator[]=(String id, T serviceObject) { |
| 36 assert(cachesId(id)); | 36 assert(cachesId(id)); |
| 37 _cache[id] = serviceObject; | 37 _cache[id] = serviceObject; |
| 38 } | 38 } |
| 39 | 39 |
| 40 /// Gets [id] from the cache or makes a network request for [id]. | 40 /// Gets [id] from the cache or makes a network request for [id]. |
| 41 Future<T> get(String id) { | 41 Future<T> get(String id) { |
| 42 assert(cachesId(id)); | 42 assert(cachesId(id)); |
| 43 T cached = _cache[id]; | 43 T cached = _cache[id]; |
| 44 if (cached != null) { | 44 if (cached != null) { |
| 45 return new Future.value(cached); | 45 return cached.load(); |
| 46 } | 46 } |
| 47 return isolate.get(id).then(_addToCache); | 47 return isolate.getDirect(id); |
| 48 } | 48 } |
| 49 | 49 |
| 50 /// If [obj] is cached, return the cached object. Otherwise, upgrades [obj] | 50 /// If [obj] is cached, return the cached object. Otherwise, upgrades [obj] |
| 51 /// and adds the upgraded value to the cache. | 51 /// and adds the upgraded value to the cache. |
| 52 T putIfAbsent(ObservableMap obj) { | 52 T putIfAbsent(ObservableMap obj) { |
| 53 assert(ServiceObject.isServiceMap(obj)); | 53 assert(ServiceObject.isServiceMap(obj)); |
| 54 String id = obj['id']; | 54 String id = obj['id']; |
| 55 var type = obj['type']; | 55 var type = obj['type']; |
| 56 assert(cachesId(id)); | 56 assert(cachesId(id)); |
| 57 if (contains(id)) { | 57 if (contains(id)) { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 class ClassCache extends ServiceObjectCache<ServiceMap> { | 129 class ClassCache extends ServiceObjectCache<ServiceMap> { |
| 130 ClassCache(Isolate isolate) : super(isolate); | 130 ClassCache(Isolate isolate) : super(isolate); |
| 131 | 131 |
| 132 bool cachesId(String id) => _matcher.hasMatch(id); | 132 bool cachesId(String id) => _matcher.hasMatch(id); |
| 133 bool cachesType(String type) => ServiceObject.stripRef(type) == 'Class'; | 133 bool cachesType(String type) => ServiceObject.stripRef(type) == 'Class'; |
| 134 ServiceMap _upgrade(ObservableMap obj) => | 134 ServiceMap _upgrade(ObservableMap obj) => |
| 135 new ServiceMap.fromMap(isolate, obj); | 135 new ServiceMap.fromMap(isolate, obj); |
| 136 | 136 |
| 137 static final RegExp _matcher = new RegExp(r'classes/\d+$'); | 137 static final RegExp _matcher = new RegExp(r'classes/\d+$'); |
| 138 } | 138 } |
| OLD | NEW |