Discussion: Changing SchedulerDefaults in a 3rd party lib to support Unity Engine
- Dominant language
- C#
- Stars
- 7.2k
- Forks
- 798
- PR merge metrics
- No merged PRs in 30d
Description
### Motivation:
I'm currently attempting to create a [lightweight extension](https://github.com/Cosmic-Shores/Rx.Unity) on top of _System.Reactive_ for use with the latest version 2021.2 of the Unity Game engine. The existing adoption of _System.Reactive_ for Unity called [UniRx](https://github.com/neuecc/UniRx) has lacked maintenance in a big way for years hence a growing community interest in the ability to benefit from the improvements made to _System.Reactive_ in recent years.
Sources:
* [Discussion: UniRx vs .NET Rx(with Unity integrate port from UniRx](https://github.com/neuecc/UniRx/issues/324)
* [Discussion: commnutity driven development](https://github.com/neuecc/UniRx/issues/325)
* [Open PRs - old ones first](https://github.com/neuecc/UniRx/pulls?q=is%3Apr+is%3Aopen+sort%3Acreated-asc)
_UniRx_ is a _System.Reactive_ fork and that's why it's maintenance is quite a bit of work. Also it makes it so you can't just consume a library that depends on _System.Reactive_ when using _UniRx_ inside unity. (This alone has lead to some [creative solutions/ points of pain](https://ecsrx.gitbook.io/project/other/microrx) in the past)
To make _System.Reactive_ work well within Unity one key thing has to be changed though and that's unfortunately currently not possible as far I was able to tell from the research I did. It's also something that probably everyone switching over from _UniRx_ will rely on to make a switch without having to add unreasonable 'hacks' all over their project.
I'm talking about being able to change the deafult scheduler behaviour to accommodate for unities unique multithreading considerations.
Thus it's required for TimeBasedOperations to be scheduled on the main unity thread.
Also if a build is for WebGL multithreading support isn't a thing altogether hence why all AsyncConversions need to also be scheduled on the main unity thread.
From my reading it looks to me like in the past it was possible to make these behaviour changes from a third party library by implementing an _IPlatformEnlightenmentProvider_. However as of version 5 of System.Reactive it looks like it's no longer possible to influence the behaviour sufficiently when using that method. If feels like that concept has been partially retired.
As an alternative the easiest way that I'd at least have the option (though it wouldn't be entirely clean yet) would be to change SchedulerDefaults.cs to look like this:
```cs
namespace System.Reactive.Concurrency
{
internal static class SchedulerDefaults
{
private static IScheduler _timeBasedOperations;
private static IScheduler _asyncConversions;
internal static IScheduler ConstantTimeOperations => ImmediateScheduler.Instance;
internal static IScheduler TailRecursion => ImmediateScheduler.Instance;
internal static IScheduler Iteration => CurrentThreadScheduler.Instance;
internal static IScheduler TimeBasedOperations
{
get => _timeBasedOperations ??= DefaultScheduler.Instance;
set => _timeBasedOperations = value;
}
internal static IScheduler AsyncConversions
{
get => _asyncConversions ??= DefaultScheduler.Instance;
set => _asyncConversions = value;
}
}
}
```
Using reflection I could then make the following calls in my [3rd party library](https://github.com/Cosmic-Shores/Rx.Unity/blob/master/Assets/Src/Scripts/ReactiveUnity.cs). (Reflection omit for ease of readability)
```cs
SchedulerDefaults.TimeBasedOperations = UnityMainThreadScheduler.Instance;
#if UNITY_WEBGL
SchedulerDefaults.AsyncConversions = UnityMainThreadScheduler.Instance;
#else
SchedulerDefaults.AsyncConversions = ThreadPoolOnlyScheduler.Instance; // Possibly subject to change in 3rd party lib still
#endif
```
Of course it would be a lot cleaner be able to make those changes without using internal code and I would hands down prefer that. But this is where I feel like I'd hear an opinion from you as to a possible approach that'd fit well into the current philosophy about doing this. Would it be a good idea to make use of the _IPlatformEnlightenmentProvider_ again for this or would you rather just have that be gone?
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.