Created
April 26, 2015 15:24
-
-
Save wordijp/0aaa979b83a4d9d1d578 to your computer and use it in GitHub Desktop.
期限内に指定回数値が通過した時、その時の最新の値を出す
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ObserverExtensions | |
{ | |
/// <summary> | |
/// periodの期間内にcount回数の値が通過した時、その時の最新の値を出す | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="self"></param> | |
/// <param name="period"></param> | |
/// <param name="count"></param> | |
/// <param name="removeArrivedCache"></param> | |
/// <returns></returns> | |
public static IObservable<T> OfPeriodCount<T>(this IObservable<T> self, TimeSpan period, int count, bool removeArrivedCache = true) | |
{ | |
return Observable.Create<T>(observer => | |
{ | |
var calles = new List<System.Reactive.Timestamped<T>>(); | |
self.Timestamp().Subscribe((x) => | |
{ | |
calles.Add(x); | |
calles.RemoveAll((y) => x.Timestamp - y.Timestamp > period); // 期限切れ削除 | |
if (calles.Count >= count) | |
{ | |
observer.OnNext(x.Value); | |
if (removeArrivedCache) | |
{ | |
calles.Clear(); | |
} | |
} | |
}, | |
(err) => | |
observer.OnError(err), | |
() => | |
observer.OnCompleted() | |
); | |
return () => { }; | |
}); | |
} | |
} | |
public static class FormExtensions | |
{ | |
public static IObservable<MouseEventArgs> MouseDownAsObservable(this Form self) | |
{ | |
return Observable.FromEvent<MouseEventHandler, MouseEventArgs>( | |
h => (_, args) => h(args), | |
h => self.MouseDown += h, | |
h => self.MouseDown -= h); | |
} | |
} | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private void Form1_Load(object sender, EventArgs e) | |
{ | |
this.MouseDownAsObservable() | |
.OfPeriodCount(TimeSpan.FromMilliseconds(250), 2) | |
.Subscribe((x) => | |
{ | |
this.label1.Text = "x:" + x.X + " y:" + x.Y; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment