C
C#8mo ago
CrosRoad95

Blazor, auto unsubscribe pattern

I have created website using blazor, and if some component subscribe to something then it implement IDisposable interface to cleanup subscriptions. Because i have also a little experience with angular, rx.js and simillar, i wonder if it is good idea to introduce simillar pattern to backend:
@inherits ComponentWithSubscriptions
@inject MyService _myService

@code{
protected ovveride void OnInitializeAsync(){
Subscribe(_myService.IncomingMessage, (message) => { /* here i render message */ InvokeAsync(StateHasChanged) }
}
}
@inherits ComponentWithSubscriptions
@inject MyService _myService

@code{
protected ovveride void OnInitializeAsync(){
Subscribe(_myService.IncomingMessage, (message) => { /* here i render message */ InvokeAsync(StateHasChanged) }
}
}
and base class will automatically cleanup all subscrptions, what do you think? do you have any other examples like this? I ask because i'm warry that in more complex scenario where for example events are subscribed conditionally i may leave some not cleanuped event handlers
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server