I am trying to understand how to structure my program for using RX in performance.
My application has a vector of objects in a 3D world. each object occupied a field and had a “hit” stream, which is a mouse hover over it.
I was thinking about two options for structure:
Option 1
struct object_t
{
string name_;
box bounding_box_;
observable<bool> hit_;
};
struct scene_t
{
scene_t(observable<point> mouse) : hit_(hit(mouse))
{
add({"background", {}, {}};
}
object_t& add(object_t o)
{
int object_index = objects_.size();
o.hit_ = hit_
.map([=](int index){ return index == object_index; })
.distinct_until_changed();
objects_.push_back(o);
return objects_.back();
}
observable<int> hit(observable<point> mouse);
using objects_t = std::vector<object_t>;
objects_t objects_;
observable<int> hit_
};
Option 2
struct object_t
{
string name_;
box bounding_box_;
void signal_hit(boot is_hit) { hit_.get_observer().on_next(is_hit); }
observable<bool> hit() const { return hit_.get_observable(); }
private:
subject<bool> hit_;
};
struct scene_t
{
scene_t(observable<point> mouse) : hit_(hit(mouse))
{
add({"background", {}, {}};
hit_
.start_with(0)
.buffer(2, 1)
.subscribe([this](std::vector<int> indices) {
objects_[indices[1]].signal_hit(false);
objects_[indices[0]].signal_hit(true);
});
}
object_t& add(object_t o)
{
objects_.push_back(o);
return objects_.back();
}
};
Now the question is how to associate the result of the hit function with the stream object_t :: hit.
I see two ways:
- Option 1 is fully functional, but works very poorly, since for each mouse point the stream of all objects will have to calculate their value.
- 2. , . , , () () .
:
rxcpp, , RX, FRP, rxjs\rx.net\frp ..
: -)