-
April 2nd, 2017, 10:31 PM
#1
Rx framework observable and callbacks
Hi all,
I am having somewhat of a hard time diving into the Rx framework. I have a specific, simple, problem that I would like to solve, but getting a clear example is proving difficult. I can only assume I am going about things wrong and missing something.
I have a class (call is 'A') that processes data based on an event. I want to provide a mechanism whereby class 'A' can inform an observer when it has done some processing. In the following example an observer would implement IMyCallback and be notified.
Code:
public class A
{
private readonly IMyCallback _myCallback;
public A(IMyCallback callback)
{
_myCallback = null;
}
private OnSomeExternallyTriggeredEvent()
{
DoSomeProcessing();
}
private DoSomeProcessing()
{
byte[] bytes = new bytes[128];
//..
_myCallback?.OnDataRecv(bytes);
}
}
As an alternative class 'A' could have an event and the observer could subscribe to it. If I was to run with an event based approach, from my understanding so far class 'A' could almost directly replace this by using a subject.
Code:
public class A
{
private readonly Subject<byte[]> _subject = new Subject<byte[]>();
public IObservable<byte[]> Values{ get { return _doValues.AsObservable(); } }
//... etc
}
Following on from this, there is a lot of talk about not using subjects but what further confused me was this from another question
If "The system raises an event or a call back when a message arrives... we just wrap it with the appropriate FromEvent method and we are done. To the Pub!"
Once again, if I was using events I could see how to use FromEvent. What is confusing me if the "or a call back". In the class 'A' example above I am using a callback, but I cannot determine how I would use FromEvent..
Am I complicating things, should I simply use Subjects?
Note: All code is under my control and can be changed if needed.
Best,
Dan
Learning somthing new every day!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|