Skip to content

Commit

Permalink
Fix Wrong UWP Scheduler (#2999)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPulman committed Nov 4, 2021
1 parent b32de3e commit 8c96b75
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 193 deletions.
2 changes: 1 addition & 1 deletion src/Directory.build.props
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<PackageReference Include="Microsoft.Reactive.Testing" Version="5.0.0" />
<PackageReference Include="PublicApiGenerator" Version="10.2.0" />
<PackageReference Include="coverlet.msbuild" Version="3.1.0" PrivateAssets="All" />
<PackageReference Include="Verify.Xunit" Version="14.0.1" />
<PackageReference Include="Verify.Xunit" Version="14.1.1" />
</ItemGroup>

<ItemGroup Condition="'$(IsTestProject)' != 'true'">
Expand Down

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions src/ReactiveUI.Uwp/ActivationForViewFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class ActivationForViewFetcher : IActivationForViewFetcher
/// <inheritdoc/>
public IObservable<bool> GetActivationForView(IActivatableView view)
{
if (!(view is FrameworkElement fe))
if (view is not FrameworkElement fe)
{
return Observable<bool>.Empty;
}
Expand Down Expand Up @@ -54,4 +54,4 @@ public IObservable<bool> GetActivationForView(IActivatableView view)
.Switch()
.DistinctUntilChanged();
}
}
}
2 changes: 1 addition & 1 deletion src/ReactiveUI.Uwp/Common/AutoDataTemplateBindingHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public bool ExecuteHook(object? source, object target, Func<IObservedChange<obje
var viewProperties = getCurrentViewProperties();
var lastViewProperty = viewProperties.LastOrDefault();

if (!(lastViewProperty?.Sender is ItemsControl itemsControl))
if (lastViewProperty?.Sender is not ItemsControl itemsControl)
{
return true;
}
Expand Down
40 changes: 0 additions & 40 deletions src/ReactiveUI.Uwp/PlatformRegistrations.cs

This file was deleted.

8 changes: 7 additions & 1 deletion src/ReactiveUI.Uwp/Registrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,11 @@ public void Register(Action<Func<object>, Type> registerFunction)
registerFunction(() => new AutoDataTemplateBindingHook(), typeof(IPropertyBindingHook));

registerFunction(() => new WinRTAppDataDriver(), typeof(ISuspensionDriver));

if (!ModeDetector.InUnitTestRunner())
{
RxApp.TaskpoolScheduler = TaskPoolScheduler.Default;
RxApp.MainThreadScheduler = new SingleWindowDispatcherScheduler();
}
}
}
}
5 changes: 1 addition & 4 deletions src/ReactiveUI.WinUI/DispatcherQueueScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ namespace System.Reactive.Concurrency
/// <summary>
/// Represents an object that schedules units of work on a <see cref="Microsoft.UI.Dispatching.DispatcherQueue"/>.
/// </summary>
/// <remarks>
/// This scheduler type is typically used indirectly through the <see cref="Linq.DispatcherObservable.ObserveOnDispatcher{TSource}(IObservable{TSource})"/> and <see cref="Linq.DispatcherObservable.SubscribeOnDispatcher{TSource}(IObservable{TSource})"/> methods that use the Dispatcher on the calling thread.
/// </remarks>
public class DispatcherQueueScheduler : LocalScheduler, ISchedulerPeriodic
{
/// <summary>
Expand Down Expand Up @@ -47,7 +44,7 @@ public DispatcherQueueScheduler(DispatcherQueue dispatcherQueue)
}

/// <summary>
/// Constructs a <see cref="DispatcherScheduler"/> that schedules units of work on the given <see cref="Microsoft.UI.Dispatching.DispatcherQueue"/> at the given priority.
/// Constructs a DispatcherScheduler that schedules units of work on the given <see cref="Microsoft.UI.Dispatching.DispatcherQueue"/> at the given priority.
/// </summary>
/// <param name="dispatcherQueue"><see cref="Microsoft.UI.Dispatching.DispatcherQueue"/> to schedule work on.</param>
/// <param name="priority">Priority at which units of work are scheduled.</param>
Expand Down
87 changes: 87 additions & 0 deletions src/ReactiveUI/Platforms/uap/ComponentModelTypeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) 2021 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
// <auto-generated />

using System;
using System.ComponentModel;
using Splat;

#nullable enable
namespace ReactiveUI;

/// <summary>
/// Binding Type Converter for component model.
/// </summary>
public class ComponentModelTypeConverter : IBindingTypeConverter
{
private readonly MemoizingMRUCache<(Type fromType, Type toType), TypeConverter?> _typeConverterCache = new (
(types, _) =>
{
// NB: String is a Magical Type(tm) to TypeConverters. If we are
// converting from string => int, we need the Int converter, not
// the string converter :-/
if (types.fromType == typeof(string))
{
types = (types.toType, types.fromType);
}
var converter = TypeDescriptor.GetConverter(types.fromType);
return converter.CanConvertTo(types.toType) ? converter : null;
}, RxApp.SmallCacheLimit);

/// <inheritdoc/>
public int GetAffinityForObjects(Type fromType, Type toType)
{
var converter = _typeConverterCache.Get((fromType, toType));
return converter is not null ? 10 : 0;
}

/// <inheritdoc/>
public bool TryConvert(object? from, Type toType, object? conversionHint, out object? result)
{
if (from is null)
{
result = null;
return true;
}

var fromType = from.GetType();
var converter = _typeConverterCache.Get((fromType, toType));

if (converter is null)
{
throw new ArgumentException($"Can't convert {fromType} to {toType}. To fix this, register a IBindingTypeConverter");
}

try
{
// TODO: This should use conversionHint to determine whether this is locale-aware or not
result = (fromType == typeof(string)) ?
converter.ConvertFrom(from) : converter.ConvertTo(from, toType);

return true;
}
catch (FormatException)
{
result = null;
return false;
}
catch (Exception e)
{
// Errors from ConvertFrom end up here but wrapped in
// outer exception. Add more types here as required.
// IndexOutOfRangeException is given when trying to
// convert empty strings with some/all? converters
if (e.InnerException is IndexOutOfRangeException ||
e.InnerException is FormatException)
{
result = null;
return false;
}

throw new Exception($"Can't convert from {@from.GetType()} to {toType}.", e);
}
}
}
28 changes: 28 additions & 0 deletions src/ReactiveUI/Platforms/uap/PlatformRegistrations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2021 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
// <auto-generated />

using System;
using System.Reactive.Concurrency;
using Splat;

namespace ReactiveUI;

/// <summary>
/// .NET Framework platform registrations.
/// </summary>
/// <seealso cref="ReactiveUI.IWantsToRegisterStuff" />
public class PlatformRegistrations : IWantsToRegisterStuff
{
/// <inheritdoc/>
public void Register(Action<Func<object>, Type> registerFunction)
{
if (registerFunction is null)
{
throw new ArgumentNullException(nameof(registerFunction));
}
// Registration is done in Registrations of ReactiveUI.Uap
}
}
8 changes: 6 additions & 2 deletions src/ReactiveUI/ReactiveUI.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="MSBuild.Sdk.Extras">
<Project Sdk="MSBuild.Sdk.Extras">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net5.0;Xamarin.iOS10;Xamarin.Mac20;Xamarin.TVOS10;MonoAndroid11.0;tizen40;net6.0;net6.0-android;net6.0-ios;net6.0-tvos;net6.0-macos;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(TargetFrameworks);net461;net472;uap10.0.16299</TargetFrameworks>
Expand Down Expand Up @@ -91,10 +91,14 @@
<Compile Include="Platforms\xamarin-common\**\*.cs" />
</ItemGroup>

<ItemGroup Condition=" $(TargetFramework.StartsWith('net4')) or $(TargetFramework.StartsWith('net5')) or '$(TargetFramework)' == 'net6.0' or $(TargetFramework.StartsWith('netcoreapp3')) or $(TargetFramework.StartsWith('uap')) ">
<ItemGroup Condition=" $(TargetFramework.StartsWith('net4')) or $(TargetFramework.StartsWith('net5')) or '$(TargetFramework)' == 'net6.0' or $(TargetFramework.StartsWith('netcoreapp3')) ">
<Compile Include="Platforms\net\**\*.cs" />
</ItemGroup>

<ItemGroup Condition=" $(TargetFramework.StartsWith('uap')) ">
<Compile Include="Platforms\uap\**\*.cs" />
</ItemGroup>

<ItemGroup Condition=" $(TargetFramework.StartsWith('tizen')) ">
<Compile Include="Platforms\tizen\**\*.cs" />
<Compile Include="Platforms\xamarin-common\**\*.cs" />
Expand Down

0 comments on commit 8c96b75

Please sign in to comment.