blazor-state icon indicating copy to clipboard operation
blazor-state copied to clipboard

Added virtual function OnStateSet

Open chris5287 opened this issue 5 years ago • 5 comments

Added virtual function OnStateSet to allow a subscriber to perform work before StateHasChanged is called (a bit like OnParametersSet).

chris5287 avatar Oct 22 '19 23:10 chris5287

This addition sounds interesting, please merge.

Would simplify a couple of edge cases I have found.

pablopioli avatar Dec 15 '19 12:12 pablopioli

@pablopioli could you please give me example of the edge cases where this would be of benefit?

See Pete's Docs for nice overview of Blazor component lifecycle. https://blazor-university.com/components/component-lifecycles/

StevenTCramer avatar Dec 15 '19 14:12 StevenTCramer

I had a table and a chart (JS based) of the table data.

In a handler I can update the state and trigger a table refresh. But the chart needs to be manually redrawn in the handler. This leads to a lot of coupling.

If I can be notified of the state update I can trigger updates on components that don´t work natively in Blazor.

pablopioli avatar Dec 18 '19 02:12 pablopioli

Pablo, have you looked at how the pipeline works. One can fire an event and handle this event from anywhere. This would be more in line with my objectives of the repo.

Particularly look at INotification.

StevenTCramer avatar Jan 01 '20 20:01 StevenTCramer

I have looked at INotification and the hooks provided by Mediatr, the problem is that they assume a non-UI object. It is difficult to update the UI directly from there.

As you suggested I used a Domain Events style approach to solve my problem. I send events from the handler, which Blazor components subscribe to. It works, but is more error prone and you end having two pipelines of processing (BlazorState and the Domain Events). My app was small and worked fine, but in a bigger app I think it will be confusing.

Don't worry, I understand your point of view, I just have to warn you that the code will be harder to think of when you need to do non trivial JS interop (at least from my point of view). If someday I need to do that I would probably do a fork of your code and incorporate this changes.

pablopioli avatar Jan 02 '20 19:01 pablopioli