Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(460)

Unified Diff: chrome/common/extensions/docs/server2/caching_file_system.py

Issue 417163004: Docserver: Update Future.Then() to be more Promise-like (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/common/extensions/docs/server2/caching_file_system.py
diff --git a/chrome/common/extensions/docs/server2/caching_file_system.py b/chrome/common/extensions/docs/server2/caching_file_system.py
index 59d8030c33af19dbdf04f0c092ad32d776009f3b..c0ff5d0b92d2256cf64464ed8b26f706837b1daa 100644
--- a/chrome/common/extensions/docs/server2/caching_file_system.py
+++ b/chrome/common/extensions/docs/server2/caching_file_system.py
@@ -63,14 +63,12 @@ class CachingFileSystem(FileSystem):
if dir_stat is not None:
return Future(value=make_stat_info(dir_stat))
- dir_stat_future = self._MemoizedStatAsyncFromFileSystem(dir_path)
- def resolve():
- dir_stat = dir_stat_future.Get()
+ def next(dir_stat):
assert dir_stat is not None # should have raised a FileNotFoundError
# We only ever need to cache the dir stat.
self._stat_object_store.Set(dir_path, dir_stat)
return make_stat_info(dir_stat)
- return Future(callback=resolve)
+ return self._MemoizedStatAsyncFromFileSystem(dir_path).Then(next)
@memoize
def _MemoizedStatAsyncFromFileSystem(self, dir_path):
@@ -93,18 +91,17 @@ class CachingFileSystem(FileSystem):
# with a value.
stat_futures = {}
- def swallow_file_not_found_error(future):
- def resolve():
- try: return future.Get()
- except FileNotFoundError: return Nnone
- return Future(callback=resolve)
+ def handle(error):
+ if isinstance(error, FileNotFoundError):
+ return None
+ raise error
for path in paths:
stat_value = cached_stat_values.get(path)
if stat_value is None:
stat_future = self.StatAsync(path)
if skip_not_found:
- stat_future = swallow_file_not_found_error(stat_future)
+ stat_future = stat_future.Then(lambda x: x, handle)
else:
stat_future = Future(value=stat_value)
stat_futures[path] = stat_future
@@ -120,19 +117,16 @@ class CachingFileSystem(FileSystem):
# Everything was cached and up-to-date.
return Future(value=fresh_data)
- # Read in the values that were uncached or old.
- read_futures = self._file_system.Read(
- set(paths) - set(fresh_data.iterkeys()),
- skip_not_found=skip_not_found)
- def resolve():
- new_results = read_futures.Get()
+ def next(new_results):
# Update the cache. This is a path -> (data, version) mapping.
self._read_object_store.SetMulti(
dict((path, (new_result, stat_futures[path].Get().version))
for path, new_result in new_results.iteritems()))
new_results.update(fresh_data)
return new_results
- return Future(callback=resolve)
+ # Read in the values that were uncached or old.
+ return self._file_system.Read(set(paths) - set(fresh_data.iterkeys()),
+ skip_not_found=skip_not_found).Then(next)
def GetIdentity(self):
return self._file_system.GetIdentity()

Powered by Google App Engine
This is Rietveld 408576698