/// <summary> /// 生成Observable的测试代码 /// </summary> private static async void CreateDemo() { // ByCreate Console.WriteLine($"ByCreate Start... {DateTime.Now.ToLongTimeString()}"); var byCreate = CreateOperate.GetObservableByCreate().Timestamp(); await Task.Delay(TimeSpan.FromSeconds(2)); byCreate.SubscribeConsole("ByCreate"); // ByDefer var byDefer = CreateOperate.GetObservableByDefer(); byDefer.SubscribeConsole("ByDefer"); // 事件生成 var eventMock = new EventMock(); CreateOperate.GetObservableForEventPattern(eventMock).SubscribeConsole("ForEventPattern"); CreateOperate.GetObservableForEventPatternSimplest(eventMock).SubscribeConsole("ForEventPatternSimplest"); CreateOperate.GetObservableForNotFollowEventPattern(eventMock).SubscribeConsole("ForNotFollowEventPattern"); CreateOperate.GetObservableForMultipleParameters(eventMock).SubscribeConsole("ForMultipleParameters"); CreateOperate.GetObservableForNotArgument(eventMock).SubscribeConsole("ForNotArgument"); eventMock.RaiseEvent(); eventMock.RaiseEvent(); eventMock.RaiseEvent(); // Enumerable 转 Observable CreateOperate.EnumerableToObservable().SubscribeConsole("Enumerable 转 Observable"); CreateOperate.EnumerableToObservableWithException().SubscribeConsole("抛异常的Enumerable 转 Observable"); CreateOperate.EnumerableToObservableWithConcat() .SubscribeConsole("Enumerable 转 Observable 后使用 Concat() 拼接多个 Observable"); // Observable 转 Enumerable var enumerable = CreateOperate.ObservableToEnumerable(); foreach (var item in enumerable) { Console.WriteLine(item); } // ObservableToDictionary CreateOperate.ObservableToDictionary() .Select(x => string.Join(",", x)) .SubscribeConsole("Enumerable 转 Dictionary"); // ObservableToLookup CreateOperate.ObservableToLookup() .Select(lookup => { var groups = new StringBuilder(); foreach (var grouping in lookup) { groups.Append($"[Key => {grouping.Count()}]"); } return(groups.ToString()); }) .SubscribeConsole("Enumerable 转 Lookup"); // 循环生成Observable CreateOperate.GetObservableByLoopWithGenerate().SubscribeConsole("Generate 循环"); CreateOperate.GetObservableByLoopWithRange().SubscribeConsole("Range 循环"); // Using CreateOperate.GetObservableByResource().SubscribeConsole("Using 读取流"); // 生成简单的Observable CreateOperate.GetObservableByReturn().SubscribeConsole("Return"); CreateOperate.GetObservableByThrow().SubscribeConsole("Throw"); CreateOperate.GetObservableByNever().SubscribeConsole("Never"); CreateOperate.GetObservableByEmpty().SubscribeConsole("Empty"); }
/// <summary> /// 没有参数的事件生成 Observable /// </summary> /// <param name="eventMock"></param> /// <returns></returns> public static IObservable <Unit> GetObservableForNotArgument(EventMock eventMock) { return(Observable.FromEvent(h => eventMock.NotArgumentEvent += h, h => eventMock.NotArgumentEvent -= h)); }
/// <summary> /// 多个参数的事件生成 Observable。 /// </summary> /// <param name="eventMock"></param> /// <returns></returns> public static IObservable <Tuple <int, string> > GetObservableForMultipleParameters(EventMock eventMock) { return(Observable.FromEvent <Action <int, string>, Tuple <int, string> >( handler => (value1, value2) => handler(Tuple.Create(value1, value2)), // 传一个转换参数的委托 h => eventMock.MultipleParameterEvent += h, h => eventMock.MultipleParameterEvent -= h)); }
/// <summary> /// 从非标准事件模式生成 Observable。 /// </summary> /// <param name="eventMock"></param> /// <returns></returns> public static IObservable <string> GetObservableForNotFollowEventPattern(EventMock eventMock) => Observable.FromEvent <Action <string>, string>(h => eventMock.NotFollowEventPatternEvent += h, h => eventMock.NotFollowEventPatternEvent -= h);
/// <summary> /// 从标准事件模式生成 Observable 的最简化版本,但是这个方法需要传一个魔法数,所以一定要用 nameof 减少Bug。 /// </summary> /// <param name="eventMock"></param> /// <returns></returns> public static IObservable <string> GetObservableForEventPatternSimplest(EventMock eventMock) => Observable.FromEventPattern(eventMock, nameof(eventMock.MessageEvent)).Select(change => "eventRaiseSimplest");
/// <summary> /// Observable.FromEventPattern<TDelegate, TEventArgs>() 是从标准的事件模式生成 Observable。 /// 1. 标准事件模式是指 public event EventHandler MessageEvent 或者 public event EventHandler<T> MessageEvent 签名的。 /// </summary> /// <param name="eventMock"></param> /// <returns></returns> public static IObservable <string> GetObservableForEventPattern(EventMock eventMock) { return(Observable.FromEventPattern <EventHandler, EventArgs>(h => eventMock.MessageEvent += h, h => eventMock.MessageEvent -= h).Select(change => "eventRaise")); }