Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can anyone say how to track changes of a variable? #402

Open
kornerr opened this issue Sep 16, 2017 · 6 comments
Open

Can anyone say how to track changes of a variable? #402

kornerr opened this issue Sep 16, 2017 · 6 comments

Comments

@kornerr
Copy link

kornerr commented Sep 16, 2017

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.

  1. Declare collection and its items:
struct Item {
  var name = ""
}

class Collection {
  let items: Variable<[Item]> = Variable([])
}
  1. Define collection:
var collection = Collection()
  1. Subscribe to collection items' changes:
collection.asObservable().subscribe(onNext: { items in
  NSLog("Items changed. Here they are: \(items)")
})
.disposed(by: disposeBag)
  1. Change items from anywhere on the collection instance:
collection.items.value = [item1, item2]

Whenever I do change collection items like that, the subscription runs and prints all items.

I want to do the same in C++. Is it possible?

@kornerr kornerr changed the title Can anyone say how to track variable changes? Can anyone say how to track changes of a variable? Sep 16, 2017
@kirkshoop
Copy link
Member

kirkshoop commented Sep 16, 2017

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}});

@kornerr
Copy link
Author

kornerr commented Sep 18, 2017

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.

@kirkshoop
Copy link
Member

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?

@kornerr
Copy link
Author

kornerr commented Sep 19, 2017

Looks almost great. But the reporting should also happen when I update/remove/add one item to vector, too.

@kirkshoop
Copy link
Member

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

@abc123098123
Copy link

So any news about this? Anything that has been tested to actually track changes to a variable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants