mikoskinen / blazor.eventaggregator Goto Github PK
View Code? Open in Web Editor NEWLightweight Event Aggregator for Blazor (Razor Components).
License: MIT License
Lightweight Event Aggregator for Blazor (Razor Components).
License: MIT License
If you open a second browser tab and navigate to the page. Clicking on count will fail and freeze the UI. Actually just navigating to the page will cause the first page to freeze up.
Hi @mikoskinen and thanks for your work !
However i see a huge problem in your library.
Indeed i faced a performance issue with my app, and i see some of my components are definitively render too much (dotnet/aspnetcore#14684)
i debug this and i see each time one of the event aggregator (whatever the parameters) publish, all of the components wich suscribe (whatever the parameters) are re-loaded (i see them with put some trace on the ShouldRender when the value is true, when i traced back the call stach i arrive on the publish of components).
I precise i see this behavior is client-side (web assembly)
so if you have a component C1 wich publish the a message of type T1 and and component C2 which suscribe of message of type T2, C2 is reloaded when C1 publish, which make no sense, normally the type is used to isolate the different case.
In my case, the components was reloaded 8 time instead of 1. I don't take time to debug your code so i don't know what is causing this issue. However, i see you try to called automatically the StateAsChanged in your code, i think it's a bad idea, In my case sometime i don't want to relad my components (some are very huge) and can be a performance problem event without this bug. at least provide an option to disable it (or better, as a parameter of the publish).
Maybe i can make some PR if you need help.
Good luck
Julien
Just a note for anyone else...
I experienced a few issues using this package on a Blazor WASM net6.0 solution.
In the end I found that https://www.nuget.org/packages/Caliburn.Micro.Core/ is current and contains an updated version of the EventAggregator which is working fine in my solution (Blazor.EventAggregator is based on an older version of Caliburn).
builder.Services.AddScoped<Caliburn.Micro.IEventAggregator, Caliburn.Micro.EventAggregator>();
_eventAggregator.SubscribeOnPublishedThread(this);
await _eventAggregator.PublishOnCurrentThreadAsync(new MyMessage())
Hi,
Are you sure to register as singleton is a good way ?
In this case all clients will receive notification on their pages.
Does the subscriber have to call Unsubscribe
in order to prevent memory leaks (as the EventAggregator
is registered as a singleton)?
Shouldn't this listener unsubscribe when it is disposed?
I would suggest you add an enum called ImplementationType having the following:
public enum ImplementationType { Singleton, Scoped, Transient }
And add a property to the ServiceCollectionExtensions:
public ImplementationType ImplementationType { get; set; }
Which means you now can tell the AddEventAggregator method which way to dependency inject the IEventAggregator:
`
public static IServiceCollection AddSpecialEventAggregator(this IServiceCollection services, Action configure = null)
{
if (configure != null)
{
SpecialEventAggregatorOptions options = new SpecialEventAggregatorOptions();
configure(options);
if (options.ImplementationType == ImplementationType.Transient)
{
services.AddTransient<IEventAggregator, EventAggregator.Blazor.EventAggregator>();
}
else if (options.ImplementationType == ImplementationType.Scoped)
{
services.AddScoped<IEventAggregator, EventAggregator.Blazor.EventAggregator>();
}
else
{
services.AddSingleton<IEventAggregator, EventAggregator.Blazor.EventAggregator>();
}
services.Configure(configure);
}
else
{
services.AddSingleton<IEventAggregator, EventAggregator.Blazor.EventAggregator>();
}
return services;
}`
2>Fatal error in IL Linker
2>
2>Unhandled Exception: Mono.Linker.MarkException: Error processing method: 'System.Threading.Tasks.Task Microsoft.AspNetCore.Components.Browser.RendererRegistryEventDispatcher::DispatchEvent(Microsoft.AspNetCore.Components.Browser.RendererRegistryEventDispatcher/BrowserEventDescriptor,System.String)' in assembly: 'Microsoft.AspNetCore.Components.Browser.dll' ---> Mono.Cecil.ResolutionException: Failed to resolve System.Threading.Tasks.Task Microsoft.AspNetCore.Components.Rendering.Renderer::DispatchEventAsync(System.Int32,Microsoft.AspNetCore.Components.UIEventArgs)
2> at Mono.Linker.Steps.MarkStep.HandleUnresolvedMethod(MethodReference reference)
2> at Mono.Linker.Steps.MarkStep.MarkMethod(MethodReference reference)
2> at Mono.Linker.Steps.MarkStep.MarkInstruction(Instruction instruction)
2> at Mono.Linker.Steps.MarkStep.MarkMethodBody(MethodBody body)
2> at Mono.Linker.Steps.MarkStep.ProcessMethod(MethodDefinition method)
2> at Mono.Linker.Steps.MarkStep.ProcessQueue()
2> --- End of inner exception stack trace ---
2> at Mono.Linker.Steps.MarkStep.ProcessQueue()
2> at Mono.Linker.Steps.MarkStep.ProcessPrimaryQueue()
2> at Mono.Linker.Steps.MarkStep.Process()
2> at Mono.Linker.Steps.MarkStep.Process(LinkContext context)
2> at Mono.Linker.Pipeline.ProcessStep(LinkContext context, IStep step)
2> at Mono.Linker.Pipeline.Process(LinkContext context)
2> at Mono.Linker.Driver.Run(ILogger customLogger)
2> at Mono.Linker.Driver.Execute(String[] args, ILogger customLogger)
2> at Mono.Linker.Driver.Main(String[] args)
Just add (reference) NuGet package EventAggregator.Blazor 1.1.0 to project and build the project.
.NET Core 3.0.100-preview8-013656
Microsoft.AspNetCore.Blazor 3.0.0-preview8
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.