-
Notifications
You must be signed in to change notification settings - Fork 56
/
IEnumerableToObservable.cs
43 lines (41 loc) · 1.21 KB
/
IEnumerableToObservable.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using System.Collections.Generic;
using System.Reactive.Linq;
namespace Adaptation
{
public static class IEnumerableToObservable
{
public static void ShowAll(IEnumerable<string> source)
{
IObservable<string> observableSource = source.ToObservable();
observableSource.Subscribe(Console.WriteLine);
}
public static IObservable<T> MyToObservable<T>(this IEnumerable<T> input)
{
return Observable.Create((IObserver<T> observer) =>
{
bool inObserver = false;
try
{
foreach (T item in input)
{
inObserver = true;
observer.OnNext(item);
inObserver = false;
}
inObserver = true;
observer.OnCompleted();
}
catch (Exception x)
{
if (inObserver)
{
throw;
}
observer.OnError(x);
}
return () => { };
});
}
}
}