0

I have an object which looks something like the following:

public class Message {
   public string Topic { get; set; }
   public string ContextId { get; set; }
   ...
}

The observable in question is being used as a message bus and I want to allow "observers" to say when a message with topic a occurs and then topic b occurs with the same ContextId do x.

Any ideas how you would go about doing this? Also, if there are any negative effects of doing this (i.e. is memory going to get held up is one of the message pairs for a given topic doesn't happen) and if this can be defended against?

anthonyv
  • 2,435
  • 1
  • 13
  • 17
  • You should follow up this example: http://stackoverflow.com/questions/1249517/super-simple-example-of-c-sharp-observer-observable-with-delegates – Shmwel Jul 16 '15 at 06:20

1 Answers1

1

There are a bunch of ways of doing this based on what you really want. It's not clear from your question exactly what the condition is.

I'm going to take the view that you want to track multiple ContextId at the same time for the situation when an .Topic == "a" appears followed by the next value for that ContextId by a .Topic == "b". This means that any number of message for other ContextId values could appear between the two messages. If this is not the case please let me know.

var query =
    messages
        .GroupBy(x => x.ContextId)
        .Select(xs =>
            xs
                .Publish(ys =>
                    ys
                        .Where(y => y.Topic == "A")
                        .Select(y =>
                            ys
                                .Where(w => w.Topic == "B")
                                .TakeUntil(ys.Where(w => w.Topic != "B")))
                        .Switch()))
        .Merge();

My test data is:

messages.OnNext(new Message() { ContextId = "1", Topic = "A" });
messages.OnNext(new Message() { ContextId = "2", Topic = "B" });
messages.OnNext(new Message() { ContextId = "3", Topic = "C" });
messages.OnNext(new Message() { ContextId = "2", Topic = "A" });
messages.OnNext(new Message() { ContextId = "1", Topic = "B" });
messages.OnNext(new Message() { ContextId = "2", Topic = "C" });
messages.OnNext(new Message() { ContextId = "2", Topic = "B" });

From which I get one value only Message() { ContextId = "1", Topic = "B" }.

As far as memory is concerned then this will eventually use up all of the memory on the computer if enough ContextId values appear. Of course it must. But the only way to know how much is to measure it in your code.

Remember, once you call .Dispose() on the subscription you get your memory back.

Enigmativity
  • 97,521
  • 11
  • 78
  • 153