Skip to content

Commit 81b19ab

Browse files
committed
fix: replace #if DEBUG with runtime checks for diagnostics (fixes #9)
Diagnostics types were wrapped in #if DEBUG, stripping them from the Release-compiled NuGet package. Consumers could not use diagnostics even when running their apps in Debug mode. Changes: - Remove #if DEBUG from all diagnostics models, interface, service, middleware, and extensions (10 files) — always compiled now - Replace #if DEBUG with runtime null-checks in StoreComponent and SelectorStoreComponent — DiagnosticsService?.Method() guards suffice - Remove redundant #if DEBUG from StoreBuilderExtensions and SecureStoreServiceExtensions — WithDevTools already has no-op stubs - Remove #if DEBUG from test files so diagnostics tests run in Release DevTools middleware retains its #if DEBUG/#else no-op pattern since it intentionally exposes state to browser extensions.
1 parent acb5d0a commit 81b19ab

17 files changed

Lines changed: 3 additions & 77 deletions

src/EasyAppDev.Blazor.Store/Blazor/SelectorStoreComponent.cs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
using Microsoft.AspNetCore.Components;
22
using EasyAppDev.Blazor.Store.Core;
3-
#if DEBUG
43
using EasyAppDev.Blazor.Store.Diagnostics;
54
using EasyAppDev.Blazor.Store.Diagnostics.Models;
65
using Microsoft.Extensions.DependencyInjection;
7-
#endif
86

97
namespace EasyAppDev.Blazor.Store.Blazor;
108

@@ -40,28 +38,24 @@ public abstract class SelectorStoreComponent<TState> : ComponentBase, IDisposabl
4038
private object? _lastRenderedValue;
4139
private int _isFirstRender = 1; // 1 = true, 0 = false (for thread-safe access)
4240
private readonly object _valueLock = new();
43-
#if DEBUG
4441
private Guid _subscriptionId;
45-
#endif
4642

4743
/// <summary>
4844
/// Gets the injected store instance.
4945
/// </summary>
5046
[Inject]
5147
protected IStore<TState> Store { get; set; } = default!;
5248

53-
#if DEBUG
5449
/// <summary>
55-
/// Gets the injected service provider (DEBUG only).
50+
/// Gets the injected service provider.
5651
/// </summary>
5752
[Inject]
5853
private IServiceProvider ServiceProvider { get; set; } = default!;
5954

6055
/// <summary>
61-
/// Gets the diagnostics service if available (DEBUG only).
56+
/// Gets the diagnostics service if available.
6257
/// </summary>
6358
protected IDiagnosticsService? DiagnosticsService { get; private set; }
64-
#endif
6559

6660
/// <summary>
6761
/// Gets the current state from the store.
@@ -131,10 +125,8 @@ protected override void OnInitialized()
131125
{
132126
base.OnInitialized();
133127

134-
#if DEBUG
135128
// Try to resolve diagnostics service if available
136129
DiagnosticsService = ServiceProvider.GetService(typeof(IDiagnosticsService)) as IDiagnosticsService;
137-
#endif
138130

139131
SubscribeToStore();
140132
}
@@ -181,7 +173,6 @@ protected override void OnAfterRender(bool firstRender)
181173
_lastRenderedValue = _selectedValue;
182174
}
183175

184-
#if DEBUG
185176
// Record render event for diagnostics
186177
// This is now safe with ShouldRender() optimization preventing cascade renders
187178
if (DiagnosticsService is not null)
@@ -195,7 +186,6 @@ protected override void OnAfterRender(bool firstRender)
195186
Reason = firstRender ? "Initial Render" : "Selector Value Changed"
196187
});
197188
}
198-
#endif
199189
}
200190

201191
/// <summary>
@@ -207,7 +197,6 @@ protected virtual void SubscribeToStore()
207197
// Get initial selected value
208198
_selectedValue = SelectState(Store.GetState());
209199

210-
#if DEBUG
211200
_subscriptionId = Guid.NewGuid();
212201

213202
if (DiagnosticsService is not null)
@@ -222,18 +211,15 @@ protected virtual void SubscribeToStore()
222211
NotificationCount = 0
223212
});
224213
}
225-
#endif
226214

227215
// Subscribe using the selector
228216
_subscription = Store.Subscribe(
229217
selector: SelectState,
230218
callback: value =>
231219
{
232-
#if DEBUG
233220
// Record subscription notification for diagnostics
234221
// This is now safe with ShouldRender() optimization preventing cascade renders
235222
DiagnosticsService?.RecordSubscriptionNotification(_subscriptionId);
236-
#endif
237223
// Thread-safe write to _selectedValue
238224
lock (_valueLock)
239225
{
@@ -269,9 +255,7 @@ protected virtual void Dispose(bool disposing)
269255

270256
if (disposing)
271257
{
272-
#if DEBUG
273258
DiagnosticsService?.RecordSubscriptionDisposed(_subscriptionId);
274-
#endif
275259
_subscription?.Dispose();
276260
_subscription = null;
277261
}

src/EasyAppDev.Blazor.Store/Blazor/StoreBuilderExtensions.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System.Text.Json;
22
using EasyAppDev.Blazor.Store.Core;
3-
#if DEBUG
43
using EasyAppDev.Blazor.Store.Diagnostics;
5-
#endif
64
using EasyAppDev.Blazor.Store.Persistence;
75
using Microsoft.Extensions.DependencyInjection;
86
using Microsoft.JSInterop;
@@ -50,12 +48,10 @@ public static StoreBuilder<TState> WithDefaults<TState>(
5048
bool includeDevTools)
5149
where TState : notnull
5250
{
53-
#if DEBUG
5451
if (includeDevTools)
5552
{
5653
builder = builder.WithDevTools(serviceProvider, storeName ?? typeof(TState).Name);
5754
}
58-
#endif
5955
return builder.WithLogging();
6056
}
6157

@@ -184,13 +180,11 @@ public static StoreBuilder<TState> WithDiagnosticsIfAvailable<TState>(
184180
IServiceProvider serviceProvider)
185181
where TState : notnull
186182
{
187-
#if DEBUG
188183
var diagnosticsService = serviceProvider.GetService<IDiagnosticsService>();
189184
if (diagnosticsService != null)
190185
{
191186
return builder.WithDiagnostics(diagnosticsService);
192187
}
193-
#endif
194188
return builder;
195189
}
196190
}

src/EasyAppDev.Blazor.Store/Blazor/StoreComponent.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
using Microsoft.AspNetCore.Components;
22
using Microsoft.Extensions.Logging;
33
using EasyAppDev.Blazor.Store.Core;
4-
#if DEBUG
54
using EasyAppDev.Blazor.Store.Diagnostics;
65
using EasyAppDev.Blazor.Store.Diagnostics.Models;
76
using Microsoft.Extensions.DependencyInjection;
8-
#endif
97

108
namespace EasyAppDev.Blazor.Store.Blazor;
119

@@ -21,9 +19,7 @@ public abstract class StoreComponent<TState> : ComponentBase, IDisposable
2119
{
2220
private IDisposable? _subscription;
2321
private volatile bool _disposed;
24-
#if DEBUG
2522
private Guid _subscriptionId;
26-
#endif
2723

2824
/// <summary>
2925
/// Gets the injected store instance.
@@ -37,18 +33,16 @@ public abstract class StoreComponent<TState> : ComponentBase, IDisposable
3733
[Inject]
3834
protected ILogger<StoreComponent<TState>>? Logger { get; set; }
3935

40-
#if DEBUG
4136
/// <summary>
4237
/// Gets the injected service provider.
4338
/// </summary>
4439
[Inject]
4540
private IServiceProvider ServiceProvider { get; set; } = default!;
4641

4742
/// <summary>
48-
/// Gets the diagnostics service if available (DEBUG only).
43+
/// Gets the diagnostics service if available.
4944
/// </summary>
5045
protected IDiagnosticsService? DiagnosticsService { get; private set; }
51-
#endif
5246

5347
/// <summary>
5448
/// Gets the current state from the store.
@@ -85,14 +79,11 @@ protected override void OnInitialized()
8579
{
8680
base.OnInitialized();
8781

88-
#if DEBUG
8982
DiagnosticsService = ServiceProvider.GetService(typeof(IDiagnosticsService)) as IDiagnosticsService;
90-
#endif
9183

9284
SubscribeToStore();
9385
}
9486

95-
#if DEBUG
9687
/// <inheritdoc />
9788
protected override void OnAfterRender(bool firstRender)
9889
{
@@ -110,14 +101,12 @@ protected override void OnAfterRender(bool firstRender)
110101
});
111102
}
112103
}
113-
#endif
114104

115105
/// <summary>
116106
/// Subscribes to store changes. Can be overridden for custom subscription logic.
117107
/// </summary>
118108
protected virtual void SubscribeToStore()
119109
{
120-
#if DEBUG
121110
_subscriptionId = Guid.NewGuid();
122111

123112
if (DiagnosticsService is not null)
@@ -132,13 +121,10 @@ protected virtual void SubscribeToStore()
132121
NotificationCount = 0
133122
});
134123
}
135-
#endif
136124

137125
_subscription = Store.Subscribe(state =>
138126
{
139-
#if DEBUG
140127
DiagnosticsService?.RecordSubscriptionNotification(_subscriptionId);
141-
#endif
142128
// Use try-catch to handle component disposal during async invoke
143129
try
144130
{
@@ -201,9 +187,7 @@ protected virtual void Dispose(bool disposing)
201187

202188
if (disposing)
203189
{
204-
#if DEBUG
205190
DiagnosticsService?.RecordSubscriptionDisposed(_subscriptionId);
206-
#endif
207191
_subscription?.Dispose();
208192
_subscription = null;
209193
}

src/EasyAppDev.Blazor.Store/Diagnostics/Components/DiagnosticDisplayMode.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#if DEBUG
21
namespace EasyAppDev.Blazor.Store.Diagnostics.Components;
32

43
/// <summary>
@@ -16,4 +15,3 @@ public enum DiagnosticDisplayMode
1615
/// </summary>
1716
Inline
1817
}
19-
#endif

src/EasyAppDev.Blazor.Store/Diagnostics/DiagnosticsExtensions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#if DEBUG
21
using EasyAppDev.Blazor.Store.Core;
32
using Microsoft.Extensions.DependencyInjection;
43

@@ -49,4 +48,3 @@ public static StoreBuilder<TState> WithDiagnostics<TState>(
4948
return builder.WithMiddleware(middleware);
5049
}
5150
}
52-
#endif

src/EasyAppDev.Blazor.Store/Diagnostics/DiagnosticsMiddleware.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#if DEBUG
21
// Copyright (c) EasyAppDev. All rights reserved.
32
// Licensed under the MIT License.
43

@@ -206,4 +205,3 @@ public Task OnAfterUpdateAsync(TState previousState, TState currentState, string
206205
}
207206
}
208207
}
209-
#endif

src/EasyAppDev.Blazor.Store/Diagnostics/DiagnosticsService.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#if DEBUG
21
using System.Collections.Concurrent;
32
using EasyAppDev.Blazor.Store.Diagnostics.Models;
43

@@ -321,4 +320,3 @@ private PerformanceMetrics CalculateMetrics(Type stateType, List<ActionHistoryEn
321320
};
322321
}
323322
}
324-
#endif

src/EasyAppDev.Blazor.Store/Diagnostics/IDiagnosticsService.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#if DEBUG
21
using EasyAppDev.Blazor.Store.Diagnostics.Models;
32

43
namespace EasyAppDev.Blazor.Store.Diagnostics;
@@ -109,4 +108,3 @@ public interface IDiagnosticsService
109108
/// <param name="stateType">The state type to clear data for.</param>
110109
void Clear(Type stateType);
111110
}
112-
#endif

src/EasyAppDev.Blazor.Store/Diagnostics/Models/ActionHistoryEntry.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#if DEBUG
21
namespace EasyAppDev.Blazor.Store.Diagnostics.Models;
32

43
/// <summary>
@@ -51,4 +50,3 @@ public sealed class ActionHistoryEntry
5150
/// </summary>
5251
public int NewStateSize { get; init; }
5352
}
54-
#endif

src/EasyAppDev.Blazor.Store/Diagnostics/Models/PerformanceMetrics.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#if DEBUG
21
namespace EasyAppDev.Blazor.Store.Diagnostics.Models;
32

43
/// <summary>
@@ -66,4 +65,3 @@ public sealed class PerformanceMetrics
6665
/// </summary>
6766
public double AverageStateSize { get; init; }
6867
}
69-
#endif

0 commit comments

Comments
 (0)