jspuij / Cortex.Net

State management like MobX for .NET and Blazor
https://jspuij.github.io/Cortex.Net.Docs
MIT License
81 stars 11 forks source link

Having shared models across multiple blazor server pages. #57

Open pauldotknopf opened 3 years ago

pauldotknopf commented 3 years ago

I need to have some state shared across all users/sessions.

I've registered my model:

services.AddSingleton<CustomState>(context =>
{
    return context.GetRequiredService<ISharedState>().Observable(() => new CustomState());
});

This is in my blazor page:

@inject CustomState _customState
@attribute [Observer]

<button class="btn btn-primary" @onclick="TestFunction">Click me</button>
<p>Shared state: @_customState.Title</p>

[Cortex.Net.Api.Action]
private void TestFunction()
{
    _customState.Title = Guid.NewGuid().ToString();
}

If I open a single page, the UI updates with the new Guid.

If I open another page, the initial render shows the right Guid. However, if I click the button, I would expect (hope) that both pages get re-rendered.

Is this a supported use-case? How would I achieve this effect?

wihrl commented 2 years ago

Have you figured this out? When I try to share models across several users in Blazor server, I end up getting: image