简体   繁体   中英

pairwise operator using .buffer in rx.net

I need to automatically back up my files from external drivers (such as usb keys) When the driver is plugged in. to know when a new usb or hard drive is inserted I used an observable, the problem is that the only way I know about getting connected drivers is by using the function:

DriveInfo.GetDrives();

returning the list containing each linked driver: and I implement this function to know when a new mass memory is connected:

       public static IObservable<DriveInfo> ObserveNewDrivers() =>
        Observable.Interval(TimeSpan.FromSeconds(5))
            .Select(_ => DriveInfo
                .GetDrives())
            .DistinctUntilChanged()
            .Buffer(2)
            .SelectMany(buff => buff.Aggregate(
                (a, b) => b.Except(a).ToArray()));

it does not work because I only compare the pair of two-to-two drivers (using the "Except" function) numerical example: 1,2; 3.4; 5.6; . In this way if I re-insert the same driver I will double backup it twice because i don't compare 3th driver with the first (for example).

I think the behavior I need is the pairwise operator in rxjs but not in .net ie: 1.2; 2.3; 3.4; 4.5; 5.6; etc

ps. sorry if I was not too clear and for my poor english :^)

thanks for any answers

Buffer in .NET has a constructor with 2 arguments which allows you to do an 'overlapping' buffer.

If my understanding of the problem you face is correct, you would want to use:

.Buffer(2, 1)

which means that two values will be emitted on every 1 new value. More info here:

http://www.introtorx.com/Content/v1.0.10621.0/13_TimeShiftedSequences.html#Buffer

See section 'overlapping buffers by count'.

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