Skip to content

Commit 54b3f81

Browse files
committed
chore: Refactor wasm detection
1 parent 789f288 commit 54b3f81

4 files changed

Lines changed: 45 additions & 30 deletions

File tree

specs/001-wasm-compat/spec.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,18 +363,24 @@ The following are explicitly **not** included in this specification:
363363
- Caches machine ID to avoid repeated JavaScript calls
364364
- No reflection, no eval, CSP-compliant
365365

366+
5. **`src/Uno.DevTools.Telemetry/PlatformDetection.cs`**
367+
- Shared utility class for platform detection
368+
- Centralizes WASM/Browser detection logic
369+
- Used by both Telemetry.cs and TelemetryCommonProperties.cs
370+
- Eliminates code duplication and provides single source of truth
371+
366372
### Files Modified
367373

368374
1. **`src/Uno.DevTools.Telemetry/Telemetry.cs`**
369-
- Added `IsWasmBrowser` static field for runtime detection
375+
- Uses `PlatformDetection.IsWasmBrowser` for runtime detection
370376
- Added `_wasmSender` field for WASM HTTP sender
371377
- Modified `InitializeTelemetry()` to branch on WASM vs non-WASM
372378
- Modified `TrackEventTask()` to use WasmHttpSender on WASM
373379
- Modified `TrackExceptionTask()` to use WasmHttpSender on WASM
374380
- Fixed `Thread.Yield()` calls to skip on WASM
375381

376382
2. **`src/Uno.DevTools.Telemetry/TelemetryCommonProperties.cs`**
377-
- Added `IsWasmBrowser` static field
383+
- Uses `PlatformDetection.IsWasmBrowser` for runtime detection
378384
- Modified `GetMachineId()` to use WasmMachineIdHelper for persistent machine ID on WASM
379385
- Bypasses file I/O and NetworkInterface enumeration on WASM
380386

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
namespace Uno.DevTools.Telemetry
8+
{
9+
/// <summary>
10+
/// Provides platform detection utilities for telemetry.
11+
/// </summary>
12+
internal static class PlatformDetection
13+
{
14+
/// <summary>
15+
/// Cached result of WASM/Browser platform detection.
16+
/// </summary>
17+
public static readonly bool IsWasmBrowser = DetectWasmBrowser();
18+
19+
/// <summary>
20+
/// Detects if the current platform is WebAssembly/Browser.
21+
/// </summary>
22+
private static bool DetectWasmBrowser()
23+
{
24+
#if NET5_0_OR_GREATER
25+
return OperatingSystem.IsBrowser() || OperatingSystem.IsWasi();
26+
#else
27+
// For netstandard2.0, check RuntimeInformation.OSDescription
28+
var osDescription = RuntimeInformation.OSDescription;
29+
return osDescription.Contains("Browser", StringComparison.OrdinalIgnoreCase) ||
30+
osDescription.Contains("WebAssembly", StringComparison.OrdinalIgnoreCase) ||
31+
osDescription.Contains("WASI", StringComparison.OrdinalIgnoreCase);
32+
#endif
33+
}
34+
}
35+
}

src/Uno.DevTools.Telemetry/Telemetry.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,7 @@ namespace Uno.DevTools.Telemetry
1818
{
1919
public sealed class Telemetry : ITelemetry
2020
{
21-
private static readonly bool IsWasmBrowser = DetectWasmBrowser();
22-
23-
private static bool DetectWasmBrowser()
24-
{
25-
#if NET5_0_OR_GREATER
26-
return OperatingSystem.IsBrowser() || OperatingSystem.IsWasi();
27-
#else
28-
// For netstandard2.0, check RuntimeInformation.OSDescription
29-
var osDescription = System.Runtime.InteropServices.RuntimeInformation.OSDescription;
30-
return osDescription.Contains("Browser", StringComparison.OrdinalIgnoreCase) ||
31-
osDescription.Contains("WebAssembly", StringComparison.OrdinalIgnoreCase) ||
32-
osDescription.Contains("WASI", StringComparison.OrdinalIgnoreCase);
33-
#endif
34-
}
21+
private static readonly bool IsWasmBrowser = PlatformDetection.IsWasmBrowser;
3522

3623
private readonly string? _currentSessionId;
3724
private TelemetryClient? _client;

src/Uno.DevTools.Telemetry/TelemetryCommonProperties.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,7 @@ namespace Uno.DevTools.Telemetry
2828
{
2929
internal sealed class TelemetryCommonProperties
3030
{
31-
private static readonly bool IsWasmBrowser = DetectWasmBrowser();
32-
33-
private static bool DetectWasmBrowser()
34-
{
35-
#if NET5_0_OR_GREATER
36-
return OperatingSystem.IsBrowser() || OperatingSystem.IsWasi();
37-
#else
38-
// For netstandard2.0, check RuntimeInformation.OSDescription
39-
var osDescription = RuntimeInformation.OSDescription;
40-
return osDescription.Contains("Browser", StringComparison.OrdinalIgnoreCase) ||
41-
osDescription.Contains("WebAssembly", StringComparison.OrdinalIgnoreCase) ||
42-
osDescription.Contains("WASI", StringComparison.OrdinalIgnoreCase);
43-
#endif
44-
}
31+
private static readonly bool IsWasmBrowser = PlatformDetection.IsWasmBrowser;
4532

4633
public TelemetryCommonProperties(
4734
string storageDirectoryPath,

0 commit comments

Comments
 (0)