Suppose I create an image editor using Rx.Net. The user can manipulate the canvas with the mouse. The manipulation used depends on the currently selected tool. For example, there might be a draw tool and an erase tool. Only one tool can be selected at a time.
I have three threads; one for mouse events; one for mouse click commands; and one more for tool selection:
IObservable<ITool> toolSelection;
IObservalbe<MouseState> mouse;
IObservable<ICommand> commands;
The flow commandsdepends on two others: commands are issued when the user clicks the mouse, and the generated command depends on the last selected tool. Please note that the command should not be issued when the user changes the tool, only when they click on the mouse.
Now I could save the last selected tool in a variable like this:
var selectedTool = defaultTool;
toolSelection.Subscribe(x => selectedTool = x);
I can use selectedToolto create a stream commands:
var commands = mouse.Select(x => selectedTool.CreateCommand(x));
However, this does not seem like a “reactive” way of doing things. Can I achieve the same logic using stream compositions?
I looked CombineLatest, but it causes unwanted events when the user switches the tool. I just want the commands to exit when the user clicks.
source
share