Chris Webb
Chris Webb

Reputation: 796

How could I go about getting historic records from an IObservable?

I'm monitoring a stream of stock quotes via an Observable which I watch to match a certain condition, e.g -

Observable
.Empty<Quote>
.Where(q => q.Price > watchPrice)
.Subscribe(q => { // do stuff } );

Now, at the point of "do stuff", what I'd ideally like is to get the last 3 "q"s that come out the where clause, so sort of like BufferWithCount(), but each entry to Subscribe() contains the last 3 entries. This is so I can save a snapshot of the quote changes that led to the condition evaluating.

Pseudo Marble Diagram -

in  -  a     b     c     d     e     f
out -  a     ba    cba   dcb   edc   fde

Any ideas appreciated

Upvotes: 1

Views: 185

Answers (1)

Sergey Aldoukhov
Sergey Aldoukhov

Reputation: 22744

Like this?

    static void Main()
    {
        new[] {"a", "b", "c", "d", "e", "f"}
            .ToObservable()
            .Scan(Enumerable.Empty<string>(), (x, y) => x.StartWith(y).Take(3))
            .Subscribe(x =>
                           {
                               x.Run(Console.Write);
                               Console.WriteLine();
                           });
        Console.ReadLine();
    }

Upvotes: 5

Related Questions