简体   繁体   中英

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, eg -

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

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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM