本文整理汇总了C#中Subject.OnError方法的典型用法代码示例。如果您正苦于以下问题:C# Subject.OnError方法的具体用法?C# Subject.OnError怎么用?C# Subject.OnError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Subject
的用法示例。
在下文中一共展示了Subject.OnError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DeterministicDisposal
private static void DeterministicDisposal()
{
Demo.DisplayHeader("The Using operator will make sure that the resource is disposed no matter what caused the observable to stop");
Subject<int> subject = new Subject<int>();
var observable =
Observable.Using(() => Disposable.Create(() => { Console.WriteLine("DISPOSED"); }),
_ => subject);
Console.WriteLine();
Console.WriteLine("Disposed when completed");
observable.SubscribeConsole();
subject.OnCompleted();
Console.WriteLine();
Console.WriteLine("Disposed when error occurs");
subject = new Subject<int>();
observable.SubscribeConsole();
subject.OnError(new Exception("error"));
Console.WriteLine();
Console.WriteLine("Disposed when subscription disposed");
subject = new Subject<int>();
var subscription =
observable.SubscribeConsole();
subscription.Dispose();
}
示例2: OAPHShouldRethrowErrors
public void OAPHShouldRethrowErrors()
{
var input = new Subject<int>();
var sched = new TestScheduler();
var fixture = new ObservableAsPropertyHelper<int>(input,
_ => { }, -5, sched);
Assert.AreEqual(-5, fixture.Value);
(new[] { 1, 2, 3, 4 }).Run(x => input.OnNext(x));
sched.Run();
Assert.AreEqual(4, fixture.Value);
input.OnError(new Exception("Die!"));
sched.Run();
try {
Assert.AreEqual(4, fixture.Value);
} catch {
return;
}
Assert.Fail("We should've threw there");
}
示例3: CanExecuteExceptionShouldntPermabreakCommands
public void CanExecuteExceptionShouldntPermabreakCommands()
{
var canExecute = new Subject<bool>();
var fixture = CreateCommand(canExecute);
var exceptions = new List<Exception>();
var canExecuteStates = new List<bool>();
fixture.CanExecuteObservable.Subscribe(canExecuteStates.Add);
fixture.ThrownExceptions.Subscribe(exceptions.Add);
canExecute.OnNext(false);
Assert.False(fixture.CanExecute(null));
canExecute.OnNext(true);
Assert.True(fixture.CanExecute(null));
canExecute.OnError(new Exception("Aieeeee!"));
// The command should latch to false forever
Assert.False(fixture.CanExecute(null));
Assert.AreEqual(1, exceptions.Count);
Assert.AreEqual("Aieeeee!", exceptions[0].Message);
Assert.AreEqual(false, canExecuteStates[canExecuteStates.Count - 1]);
Assert.AreEqual(true, canExecuteStates[canExecuteStates.Count - 2]);
}
示例4: Should_Complete_When_Update_Observable_Errors
public void Should_Complete_When_Update_Observable_Errors()
{
var update = new Subject<Unit>();
var target = new ExpressionObserver(() => 1, "Foo", update);
var completed = false;
target.Subscribe(_ => { }, () => completed = true);
update.OnError(new Exception());
Assert.True(completed);
}
示例5: TakeErrorTest
public void TakeErrorTest()
{
var subject = new Subject<string>();
var result = new List<IValueOrError<string>>();
subject
.TakeError()
.Subscribe(result.Add);
subject.OnNext("a");
subject.OnError(new Exception("error!"));
result.Count.Is(2);
result[0].IsError.IsFalse();
result[1].IsError.IsTrue();
}
示例6: ResolvedEventDispatcher
public ResolvedEventDispatcher(
IEventStoreConnection eventStore,
ISerializer serializer,
ICheckpointRepository checkpoints,
Func<ISerializer, ResolvedEvent, bool, CancellationToken, Task> dispatchResolvedEvent,
Action onCaughtUp = null,
string streamId = null,
UserCredentials userCredentials = null)
{
_isStarted = new InterlockedBoolean();
_isDisposed = new InterlockedBoolean();
_eventStore = eventStore;
_serializer = serializer;
_checkpoints = checkpoints;
_dispatchResolvedEvent = dispatchResolvedEvent;
_onCaughtUp = onCaughtUp ?? (() => { });
_streamId = streamId;
_userCredentials = userCredentials;
_projectedEvents = new Subject<ResolvedEvent>();
_queue = new SimpleQueue(async (resolvedEvent, token) =>
{
try
{
await _dispatchResolvedEvent(_serializer, resolvedEvent, _subscription.IsSubscribedToAll, _disposed.Token);
}
catch(Exception ex)
{
_projectedEvents.OnError(ex);
throw;
}
if(_isDisposed.Value)
{
return;
}
_projectedEvents.OnNext(resolvedEvent);
}, _disposed.Token);
}
示例7: SubjectExample
private static void SubjectExample()
{
Console.WriteLine("Subject:");
var location = new Subject<Location>(); // Observable
var reporter1 = CreateReporterWith("FixedGPS "); // Observer
var reporter2 = CreateReporterWith("MobileGPS"); // Observer
var subscription1 = location.Subscribe(reporter1);
location.Subscribe(reporter2);
location.OnNext(new Location(47.6456, -123.1312));
location.OnNext(new Location(31.6677, -11.1199));
subscription1.Dispose();
location.OnNext(new Location(84.6677, -21.1023));
location.OnError(new LocationUnknownException());
location.OnCompleted();
ConsoleUtils.WaitForEnter();
}
示例8: NoThrownExceptionsSubscriberEqualsOAPHDeath
public void NoThrownExceptionsSubscriberEqualsOAPHDeath()
{
(new TestScheduler()).With(sched => {
var input = new Subject<int>();
var fixture = new ObservableAsPropertyHelper<int>(input, _ => { }, -5);
Assert.Equal(-5, fixture.Value);
(new[] { 1, 2, 3, 4 }).Run(x => input.OnNext(x));
input.OnError(new Exception("Die!"));
bool failed = true;
try {
sched.Start();
} catch (Exception ex) {
failed = ex.InnerException.Message != "Die!";
}
Assert.False(failed);
Assert.Equal(4, fixture.Value);
});
}
示例9: SetToError
public void SetToError()
{
bool invoked = false;
var status = ConnectionStatus.Pending;
var subject = new Subject<int>();
Exception exception;
var subscription = subject.MonitorStatus().Subscribe(s =>
{
invoked = true;
status = s;
}, ex =>
{
exception = ex;
});
subject.OnError(new Exception("Test"));
subscription.Dispose();
Assert.IsTrue(invoked, "No update has been received");
Assert.AreEqual(ConnectionStatus.Errored, status, "Status should be ConnectionStatus.Faulted");
}
示例10: ObserveOnTest
public void ObserveOnTest()
{
var xs = Observable.Range(1, 10)
.ObserveOn(Scheduler.ThreadPool)
.ToArrayWait();
xs.OrderBy(x => x).IsCollection(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
var s = new Subject<int>();
var list = new List<Notification<int>>();
s.ObserveOn(Scheduler.Immediate).Materialize().Subscribe(list.Add);
s.OnError(new Exception());
list[0].Kind.Is(NotificationKind.OnError);
s = new Subject<int>();
s.ObserveOn(Scheduler.Immediate).Materialize().Subscribe(list.Add);
s.OnCompleted();
list[1].Kind.Is(NotificationKind.OnCompleted);
}
示例11: UseObserverTest2
public void UseObserverTest2()
{
var subject = new Subject<string>();
var result = new List<string>();
subject
.UseObserver((observer, value, i) =>
{
observer.OnNext(value);
},
(error, observer) =>
{
observer.OnNext(error.Message);
},
observer =>
{
observer.OnNext("end");
})
.Subscribe(result.Add);
subject.OnNext("a");
subject.OnError(new Exception("error!"));
subject.OnCompleted();
result.Is("a", "error!");
}
示例12: OAPHShouldRethrowErrors
public void OAPHShouldRethrowErrors()
{
var input = new Subject<int>();
var sched = new TestScheduler();
var fixture = new ObservableAsPropertyHelper<int>(input, _ => { }, -5, sched);
var errors = new List<Exception>();
Assert.Equal(-5, fixture.Value);
(new[] { 1, 2, 3, 4 }).Run(x => input.OnNext(x));
fixture.ThrownExceptions.Subscribe(errors.Add);
sched.Start();
Assert.Equal(4, fixture.Value);
input.OnError(new Exception("Die!"));
sched.Start();
Assert.Equal(4, fixture.Value);
Assert.Equal(1, errors.Count);
}
示例13: FinishedSourceToReadOnlyReactiveProperty
public void FinishedSourceToReadOnlyReactiveProperty()
{
SetScehdulerForImport();
// pattern of OnCompleted
{
var source = Observable.Return(9);
var rxProp = source.ToReadOnlyReactiveProperty();
var notifications = rxProp.Record().Notifications;
notifications.IsCollection(Notification.CreateOnNext(9), Notification.CreateOnCompleted<int>());
rxProp.Record().Notifications.IsCollection(
Notification.CreateOnNext(9),
Notification.CreateOnCompleted<int>());
}
// pattern of OnError
{
// after
{
var ex = new Exception();
var source = Observable.Throw<int>(ex);
var rxProp = source.ToReadOnlyReactiveProperty();
var notifications = rxProp.Record().Notifications;
notifications.IsCollection(Notification.CreateOnError<int>(ex));
rxProp.Record().Notifications.IsCollection(Notification.CreateOnError<int>(ex));
}
// immediate
{
var ex = new Exception();
var source = new Subject<int>();
var rxProp = source.ToReadOnlyReactiveProperty();
var record = rxProp.Record();
source.OnError(new Exception());
var notifications = record.Notifications;
notifications.Count.Is(1);
notifications[0].Kind.Is(NotificationKind.OnError);
rxProp.Record().Notifications[0].Kind.Is(NotificationKind.OnError);
}
}
UniRx.Scheduler.SetDefaultForUnity();
}
示例14: NoSubscriberOfThrownExceptionsEqualsDeath
public void NoSubscriberOfThrownExceptionsEqualsDeath()
{
(new TestScheduler()).With(sched => {
var canExecute = new Subject<bool>();
var fixture = createCommand(canExecute);
canExecute.OnNext(true);
canExecute.OnError(new Exception("Aieeeee!"));
bool failed = true;
try {
sched.Start();
Assert.True(fixture.CanExecute(null));
} catch (Exception ex) {
failed = (ex.InnerException.Message != "Aieeeee!");
}
Assert.False(failed);
});
}
示例15: SubjectSubscribeTest
public void SubjectSubscribeTest()
{
var subject = new Subject<int>();
var listA = new List<int>();
var listB = new List<int>();
var listC = new List<int>();
subject.HasObservers.IsFalse();
var listASubscription = subject.Subscribe(x => listA.Add(x));
subject.HasObservers.IsTrue();
subject.OnNext(1);
listA[0].Is(1);
var listBSubscription = subject.Subscribe(x => listB.Add(x));
subject.HasObservers.IsTrue();
subject.OnNext(2);
listA[1].Is(2);
listB[0].Is(2);
var listCSubscription = subject.Subscribe(x => listC.Add(x));
subject.HasObservers.IsTrue();
subject.OnNext(3);
listA[2].Is(3);
listB[1].Is(3);
listC[0].Is(3);
listASubscription.Dispose();
subject.HasObservers.IsTrue();
subject.OnNext(4);
listA.Count.Is(3);
listB[2].Is(4);
listC[1].Is(4);
listCSubscription.Dispose();
subject.HasObservers.IsTrue();
subject.OnNext(5);
listC.Count.Is(2);
listB[3].Is(5);
listBSubscription.Dispose();
subject.HasObservers.IsFalse();
subject.OnNext(6);
listB.Count.Is(4);
var listD = new List<int>();
var listE = new List<int>();
subject.Subscribe(x => listD.Add(x));
subject.HasObservers.IsTrue();
subject.OnNext(1);
listD[0].Is(1);
subject.Subscribe(x => listE.Add(x));
subject.HasObservers.IsTrue();
subject.OnNext(2);
listD[1].Is(2);
listE[0].Is(2);
subject.Dispose();
AssertEx.Throws<ObjectDisposedException>(() => subject.OnNext(0));
AssertEx.Throws<ObjectDisposedException>(() => subject.OnError(new Exception()));
AssertEx.Throws<ObjectDisposedException>(() => subject.OnCompleted());
}