Repository navigation
Is there a recommended way to await async work in WhenActivated? #4469
Replies: 1 comment 2 replies
|
In 23.2.28, there is no awaitable activation-completion contract. If initialization must start on each activation, keep the registered callback synchronous and expose its task explicitly: public Task InitializationTask { get; private set; } = Task.CompletedTask;
this.WhenActivated(d =>
{
InitializationTask = InitAsync(d); // InitAsync returns Task
});A test can then do: using var activation = vm.Activator.Activate();
await vm.InitializationTask;The assignment happens while For your Blazor component, Sources: 23.2.28 activator, WhenActivated overloads, Blazor base component, and Blazor lifecycle ordering. I cannot infer from the code whether the synchronous-only API was a deliberate product decision, or speak for the maintainers on adding an async overload. AI-assisted — checked against the linked versioned source and framework documentation. |
Uh oh!
There was an error while loading. Please reload this page.
Hi! We're on ReactiveUI 23.2.28 / .NET 10 (Blazor) and ran into something I'd like to understand better — quite possibly we're just holding it wrong.
Our view model base does async setup on activation:
this.WhenActivated(async d => await InitAsync(d));
Since the WhenActivated overloads take Action<…> or Func<IEnumerable>, this becomes async void, so Activate() returns before InitAsync has finished. That's reasonable in itself, but we haven't found a way to observe when activation is actually done — either from production code that wants to react once the view model is ready, or from a unit test. (The Activate() docs mention calling it from unit tests, which is what got us looking.)
We tried composing off Activator.Activated, but as far as we can tell it's hot without replay, and a chain built on it runs alongside the activation handler rather than after it — so it doesn't quite give us "activation finished".
We also noticed ReactiveComponentBase overrides OnInitialized/OnAfterRender rather than Blazor's awaitable OnInitializedAsync/OnAfterRenderAsync. Is that a deliberate choice?
A few questions:
All reactions