| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * The local cache of previously installed packages. | |
| 7 */ | |
| 8 class PackageCache { | |
| 9 /** | |
| 10 * The root directory where this package cache is located. | |
| 11 */ | |
| 12 final String rootDir; | |
| 13 | |
| 14 // TODO(rnystrom): When packages are versioned, String here and elsewhere will | |
| 15 // become a name/version/(source?) tuple. | |
| 16 final Map<String, Package> _loadedPackages; | |
| 17 | |
| 18 /** | |
| 19 * Packages which are currently being asynchronously loaded. | |
| 20 */ | |
| 21 final Map<String, Future<Package>> _pendingPackages; | |
| 22 | |
| 23 /** | |
| 24 * Creates a new package cache which is backed by the given directory on the | |
| 25 * user's file system. | |
| 26 */ | |
| 27 PackageCache(this.rootDir) | |
| 28 : _loadedPackages = <Package>{}, | |
| 29 _pendingPackages = <Future<Package>>{}; | |
| 30 | |
| 31 /** | |
| 32 * Loads all of the packages in the cache and returns them. | |
| 33 */ | |
| 34 Future<List<Package>> listAll() { | |
| 35 return listDir(rootDir).chain((paths) { | |
| 36 final packages = paths.map((path) => find(basename(path))); | |
| 37 return Futures.wait(packages); | |
| 38 }); | |
| 39 } | |
| 40 | |
| 41 /** | |
| 42 * Loads the package named [name] from this cache, if present. | |
| 43 */ | |
| 44 // TODO(rnystrom): What happens if the package isn't cached? | |
| 45 Future<Package> find(String name) { | |
| 46 // Use the previously loaded one. | |
| 47 final package = _loadedPackages[name]; | |
| 48 if (package != null) return new Future.immediate(package); | |
| 49 | |
| 50 // If we are already in-progress loading it, re-use that one. | |
| 51 final pending = _pendingPackages[name]; | |
| 52 if (pending != null) return pending; | |
| 53 | |
| 54 // Actually load it from the cache. | |
| 55 final future = Package.load(join(rootDir, name)).transform((package) { | |
| 56 _pendingPackages.remove(name); | |
| 57 _loadedPackages[name] = package; | |
| 58 return package; | |
| 59 }); | |
| 60 | |
| 61 _pendingPackages[name] = future; | |
| 62 return future; | |
| 63 } | |
| 64 } | |
| OLD | NEW |