1- namespace BlazorState
2- {
3- using System ;
4- using System . Collections . Concurrent ;
5- using MediatR ;
6- using Microsoft . AspNetCore . Components ;
7-
8- /// <summary>
9- /// A non required Base Class that injects Mediator and Store.
10- /// And exposes StateHasChanged
11- /// </summary>
12- /// <remarks>Implements IBlazorStateComponent by Injecting</remarks>
13- public class BlazorStateComponent : ComponentBase , IDisposable ,
14- IBlazorStateComponent
15- {
16- static readonly ConcurrentDictionary < string , int > s_InstanceCounts = new ConcurrentDictionary < string , int > ( ) ;
17-
18- public BlazorStateComponent ( )
19- {
20- string name = GetType ( ) . Name ;
21- int count = s_InstanceCounts . AddOrUpdate ( name , 1 , ( aKey , aValue ) => aValue + 1 ) ;
22-
23- Id = $ "{ name } -{ count } ";
24- }
25-
26- /// <summary>
27- /// A generated unique Id based on the Class name and number of times they have been created
28- /// </summary>
29- public string Id { get ; }
30-
31- /// <summary>
32- /// Allows for the Assigning of a value one can use to select an element during automated testing.
33- /// </summary>
34- [ Parameter ] public string TestId { get ; set ; }
35-
36- [ Inject ] public IMediator Mediator { get ; set ; }
37- [ Inject ] public IStore Store { get ; set ; }
38-
39- /// <summary>
40- /// Maintains all components that subscribe to a State.
41- /// Is updated by using the GetState method
42- /// </summary>
43- [ Inject ] public Subscriptions Subscriptions { get ; set ; }
44-
45- /// <summary>
46- /// Exposes StateHasChanged
47- /// </summary>
48- public void ReRender ( ) => base . InvokeAsync ( StateHasChanged ) ;
49-
50- /// <summary>
51- /// Place a Subscription for the calling component
52- /// And returns the requested state
53- /// </summary>
54- /// <typeparam name="T"></typeparam>
55- /// <returns></returns>
56- protected T GetState < T > ( )
57- {
58- Type stateType = typeof ( T ) ;
59- Subscriptions . Add ( stateType , this ) ;
60- return Store . GetState < T > ( ) ;
61- }
62-
63- public void Dispose ( ) => Subscriptions . Remove ( this ) ;
64- }
1+ namespace BlazorState
2+ {
3+ using MediatR ;
4+ using Microsoft . AspNetCore . Components ;
5+ using System ;
6+ using System . Collections . Concurrent ;
7+
8+ /// <summary>
9+ /// A non required Base Class that injects Mediator and Store.
10+ /// And exposes StateHasChanged
11+ /// </summary>
12+ /// <remarks>Implements IBlazorStateComponent by Injecting</remarks>
13+ public class BlazorStateComponent : ComponentBase , IDisposable ,
14+ IBlazorStateComponent
15+ {
16+ static readonly ConcurrentDictionary < string , int > s_InstanceCounts = new ConcurrentDictionary < string , int > ( ) ;
17+
18+ public BlazorStateComponent ( )
19+ {
20+ string name = GetType ( ) . Name ;
21+ int count = s_InstanceCounts . AddOrUpdate ( name , 1 , ( aKey , aValue ) => aValue + 1 ) ;
22+
23+ Id = $ "{ name } -{ count } ";
24+ }
25+
26+ /// <summary>
27+ /// A generated unique Id based on the Class name and number of times they have been created
28+ /// </summary>
29+ public string Id { get ; }
30+
31+ /// <summary>
32+ /// Allows for the Assigning of a value one can use to select an element during automated testing.
33+ /// </summary>
34+ [ Parameter ] public string TestId { get ; set ; }
35+
36+ [ Inject ] public IMediator Mediator { get ; set ; }
37+ [ Inject ] public IStore Store { get ; set ; }
38+
39+ /// <summary>
40+ /// Maintains all components that subscribe to a State.
41+ /// Is updated by using the GetState method
42+ /// </summary>
43+ [ Inject ] public Subscriptions Subscriptions { get ; set ; }
44+
45+ /// <summary>
46+ /// Exposes StateHasChanged
47+ /// </summary>
48+ public void ReRender ( ) => base . InvokeAsync ( StateHasChanged ) ;
49+
50+ /// <summary>
51+ /// Place a Subscription for the calling component
52+ /// And returns the requested state
53+ /// </summary>
54+ /// <typeparam name="T"></typeparam>
55+ /// <returns></returns>
56+ protected T GetState < T > ( )
57+ {
58+ Type stateType = typeof ( T ) ;
59+ Subscriptions . Add ( stateType , this ) ;
60+ return Store . GetState < T > ( ) ;
61+ }
62+
63+ public void Dispose ( ) => Subscriptions . Remove ( this ) ;
64+ }
6565}
0 commit comments