我想要一个 BLoC 类,当两个输入(接收器)中的一个/两个都更新时,需要更新 Stream 输出。
当任一 Sink 更新/不同时,如何连接 Steam 以触发?
import 'package:escapemodels/src/bloc/blocbase.dart';
import 'package:rxdart/rxdart.dart';
class MathAddBloc implements BlocBase {
final Sink<int> x;
final Sink<int> y;
final Stream<int> addxy;
factory MathAddBloc() {
final x = PublishSubject<int>();
final y = PublishSubject<int>();
// This is the part that I am confused about - I can figure how to trigger when one Sink/Observable changes but not when either one is updated.
final addxy = x
.distinct()
.switchMap<int>((int x) => (x + y.last))
.startWith(0);
return MathAddBloc._(x,y, addxy);
}
MathAddBloc._(this.x, this.y, this.addxy);
void dispose() {
x.close();
y.close();
}
}