public async Task RefreshAsync() { var cancellationTokenSource = new CancellationTokenSource(); var disposables = new CompositeDisposable(new CancellationDisposable(cancellationTokenSource)); _currentExecution.Disposable = disposables; using (disposables) { _notificationsSubject.OnNext(new ObservableViewModelNotification() { Status = ObservableViewModelStatus.Updating, Value = null }); ObservableViewModelNotification notification; bool cancelledFromTimeout = false; try { if (TimeoutDelay > TimeSpan.Zero) { Observable.Timer(TimeoutDelay, _schedulers.ThreadPool.SchedulerFromPriority(SchedulerPriority.Low)) .Subscribe(_ => { cancelledFromTimeout = true; cancellationTokenSource.Cancel(); }) .DisposeWith(disposables); } var value = await Source(cancellationTokenSource.Token); cancellationTokenSource.Token.ThrowIfCancellationRequested(); notification = SelectValue(value); CurrentValue = notification.Status == ObservableViewModelStatus.Value ? value : default(T); } catch (OperationCanceledException) { if (!cancelledFromTimeout) { return; } notification = new ObservableViewModelNotification() { Status = ObservableViewModelStatus.Timeout, Value = null }; CurrentValue = default(T); } catch (Exception ex) { notification = new ObservableViewModelNotification() { Status = ObservableViewModelStatus.Error, Value = ex }; CurrentValue = default(T); } _notificationsSubject.OnNext(notification); } }
public void ViewModel_WhenInitialized_WithRefreshOnCollectionUpdateNotification_ShouldRefresh( IFixture fixture, TestSchedulers scheduler, int[] expectedValue) { //arrange var notifications = scheduler.CreateColdObservable(OnNext(200, 1)); var observer = scheduler.CreateObserver<ObservableViewModelNotification>(); var sut = new UpdatableObservableViewModelBuilderOptions<int, int[], int>( _ => { }, ct => Task.FromResult(expectedValue), () => notifications, scheduler, scheduler, scheduler) .UpdateAction((i, o) => () => { }) .RefreshOnCollectionUpdateNotification() .ToViewModel(); sut.Subscribe(observer); //act scheduler.Start(); //assert var expected = new ObservableViewModelNotification() { Status = ObservableViewModelStatus.Value, Value = expectedValue }; observer.Values().Last().Value.As<ObservableCollection<int>>().ShouldAllBeEquivalentTo(expectedValue); }
private DataTemplate SelectTemplate(object o, ObservableViewModelNotification rvvmValue) { #if WINDOWS_PHONE return (DataTemplate)GetValue(TemplatesMapping[rvvmValue.Status]); #else DataTemplate template; if (TemplateSelectorsMapping.ContainsKey(rvvmValue.Status) && GetValue(TemplateSelectorsMapping[rvvmValue.Status]) != null) { template = ((DataTemplateSelector)GetValue(TemplateSelectorsMapping[rvvmValue.Status])).SelectTemplate(o, this); } else { template = (DataTemplate)GetValue(TemplatesMapping[rvvmValue.Status]); } return template; #endif }