我想完成一些非常简单的事情,但是对于Rx世界来说我是一个新手,在将我的逻辑转换为代码方面遇到一些问题。过程如下:
我当前的代码为:
class Bloc {
final _code = PublishSubject<String>();
final _status = BehaviorSubject<Status>();
// stream
Observable<Status> get status => _status.stream;
// sink
Function(String) get postCode => _code.sink.add;
Bloc() {
_code.stream
.map(_processCode)
.listen((status) => _status.sink.add(status));
}
_processCode(code) {
// here I would like to do multiple http/db requests
// and wait for end with await while posting status update
// to the user something like:
// _status.sink.add(Status(status: 'RETRIEVING USERS'));
// var users = await Api.GetUsers(code);
// await DB.SaveUsers(users);
// _status.sink.add(Status(status: 'RETRIEVING IMAGES'));
// var images = await Api.GetUsersImages(users);
// _status.sink.add(Status(status: 'SAVING IMAGES'));
// images.forEach((image) => await FileUtils.SaveImage(image));
return Status(status: 'DONE');
}
}
我不确定我该如何解决-最好的方法是什么?