在我的c++项目中,我需要创建具有初始值的主题,这个值可以更新。在每次订阅/更新时,订阅者可能会触发然后进行数据处理.在以前的RxJS项目中,这种行为是用ReplaySubject(1)处理的。
我不能用c++ rxcpp lib复制这个。
我已经查找过文档、片段、教程,但都没有成功。
预期伪码(打字本):
private dataSub: ReplaySubject<Data> = new ReplaySubject<Data>(1);
private init = false;
// public Observable, immediatly share last published value
get currentData$(): Observable<Data> {
if (!this.init) {
return this.initData().pipe(switchMap(
() => this.dataSub.asObservable()
));
}
return this.dataSub.asObservable();
}发布于 2019-07-30 08:54:36
我认为您正在寻找rxcpp::subjects::replay。请试试这个:
auto coordination = rxcpp::observe_on_new_thread();
rxcpp::subjects::replay<int, decltype(coordination)> test(1, coordination);
// to emit data
test.get_observer().on_next(1);
test.get_observer().on_next(2);
test.get_observer().on_next(3);
// to subscribe
test.get_observable().subscribe([](auto && v)
printf("%d\n", v); // this will print '3'
});https://stackoverflow.com/questions/57038812
复制相似问题