-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
WasmPlatformEnlightenmentProvider.cs
58 lines (49 loc) · 2.16 KB
/
WasmPlatformEnlightenmentProvider.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) 2019 .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.
// WARNING: The full namespace-qualified type name should stay the same for the discovery in System.Reactive.Core to work!
using System.ComponentModel;
using System.Reactive.Concurrency;
using System.Threading;
using Splat;
namespace System.Reactive.PlatformServices
{
/// <summary>
/// (Infrastructure) Provider for platform-specific framework enlightenment.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public class WasmPlatformEnlightenmentProvider : CurrentPlatformEnlightenmentProvider
{
private static Lazy<bool> _isWasm = new Lazy<bool>(
() => ModeDetector.InUnitTestRunner() || MonoTest, LazyThreadSafetyMode.PublicationOnly);
/// <summary>Gets a value indicating whether the current executable is processing under WASM.</summary>
public static bool IsWasm => _isWasm.Value;
/// <summary> Gets a value indicating whether we're running on mono, hence wasm. </summary>
private static bool MonoTest =>
Type.GetType("Mono.Runtime") != null;
/// <summary>
/// (Infastructure) Tries to gets the specified service.
/// </summary>
/// <typeparam name="T">Service type.</typeparam>
/// <param name="args">Optional set of arguments.</param>
/// <returns>Service instance or <c>null</c> if not found.</returns>
public override T GetService<T>(object[] args)
{
if (!IsWasm)
{
return base.GetService<T>(args);
}
Type t = typeof(T);
if (t == typeof(IConcurrencyAbstractionLayer))
{
return (T)(object)new ConcurrencyAbstractionLayerWasmImpl();
}
if (t == typeof(IScheduler))
{
return (T)(object)WasmScheduler.Default;
}
return base.GetService<T>(args);
}
}
}