Open kornerr opened 7 years ago
this is very close to a behavior<vector<Item>>
the missing piece today is .value = [item1, item2]
. this can be emulated.
untested
auto make_set_items(const behavior<vector<Item>>& items) {
auto out = items.get_subscriber();
return [out](vector<Item>& i){
out.next(i);
};
}
behavior<vector<Item>> items;
auto set_items = make_set_items(items);
items.subscribe([](const vector<Item>& v){
cout << "Items changed. Here they are: ";
for(auto& i : v) {
cout << i << ", ";
}
cout << endl;
});
set_items({{item1, item2}});
So I can't just assign an array and get notifications. I always need to create my own function. However, creating more functions pretty much defeats the prettyness of RxCpp. Too bad.
set_items
is just there to simplify this -
items.get_subscriber().on_next({{item1, item2}});
A PR to add set_value
to behavior<>
would result in
behavior<vector<Item>> items;
items.subscribe([](const vector<Item>& v){
cout << "Items changed. Here they are: ";
for(auto& i : v) {
cout << i << ", ";
}
cout << endl;
});
items.set_value({{item1, item2}});
is this good enough?
Looks almost great. But the reporting should also happen when I update/remove/add one item to vector, too.
to do that you need to use an immutable C++ collection. a couple were discussed in C++Now talks this year.
https://www.youtube.com/watch?v=ZsryQp0UAC8 https://www.youtube.com/watch?v=WT9kmIE3Uis
So any news about this? Anything that has been tested to actually track changes to a variable?
I think it's best to sum up what I want to do in C++ by showing you what actually works in Swift with RxSwift.
class Collection { let items: Variable<[Item]> = Variable([]) }
var collection = Collection()
collection.asObservable().subscribe(onNext: { items in NSLog("Items changed. Here they are: (items)") }) .disposed(by: disposeBag)
collection.items.value = [item1, item2]