3
假设我有一些复杂的链Single
,Maybe
和Observable
执行一些任务的对象。如何可视化RxJava图的执行?
例如:
// Download, unzip and parse a release
public static Single<Release> fetchRelease(final Release release) {
final Path cachePath = getCachePath(release);
return download(release.url, cachePath, true).ignoreElements()
.andThen(hash(cachePath))
.flatMap(fileHash -> unzip(cachePath)
.andThen(parseFile(unzipTargetPath)));
}
这些连锁店可能在多个线程中运行。
问题是,我想将这些任务的进度呈现给用户,而不会在可能的情况下混淆他们的逻辑。
例如,上面的任务可能会显示:
+-Fetching release 1.0... Done
+-Downloading https://www.example.com/v1.0.0.zip 100%
+-Hashing Library/Caches/Example/v1.0.0.zip... Done
+-Unpacking Library/Caches/Example/v1.0.0.zip
+-src.. Done
+-tests... Done
+-resources... Done
+-Parsing release... Done
理想情况下,我也想显示任务的层次结构。这目前只在Java调用图中编码。
我现在的想法是:
- 改变每
Single
一个Observable
,其中最后一个元素是结果,其他的是最新进展。 - 为任务的每个阶段编写事件类。
我认为这会使代码不易读,导致大量锅炉板。
public final class DownloadProgress {
public final float progress;
// etc..
}
// etc...
public final class FetchReleaseProgress {
public final Variant<DownloadProgress, HashingProgress, UnpackProgress, ParseProgress> progress;
// etc...
}
推荐的方法是什么?
你可以看看htrace – raam86
使用RxJavaPlugin –