Using LanguageExt with Blazor to make a better busy indicator

I previously blogged about creating a busy indicator in Blazor. That works fine, but there is more we can do.

As my loyal reader knows, I have been trying to make my code more functional for some time, and have been using the rather fabulous LanguageExt nuget package to help.

One of the most basic, but extremely useful parts of this package is the `Option` monad (oh no, the m-word!). This allows you to handle the case where the data you want doesn’t exist, without having to allow those naughty null references creep in your code. I was wondering how we could handle this in Blazor.

GitHub user orthoxerox suggested creating a Blazor component with two render fragments, one for the `Some` case and one for the `None` case. This was simple, but very effective.

Whilst playing with this, it occurred to me that we could extend it to combine the busy indicator as well. This would allow us to show a “Loading…” message while the data was being loaded, and then either the `Some` or `None` content, depending on what was returned. The code is pretty simple, and is an obvious combination of the two components…

@using LanguageExt
@typeparam T

@if (!Data.HasValue) {
  <p><span class="fad fa-spin fa-spinner" aria-hidden="true"></span> @Message</p>
} else {
  @Data.Value.Match(x => Some(x), None);
}

@code {

  [Parameter]
  public string Message { get; set; } = "Loading...";

  [Parameter]
  public Option<T>? Data { get; set; }

  [Parameter]
  public RenderFragment<T> Some { get; set; }

  [Parameter]
  public RenderFragment None { get; set; }

}

Assuming you have this saved in a file called `BusyOption`, which isn’t a very imaginative name, but does describe what it does, then you can use it like this…

@page "/BusyOptionTest"
@using LanguageExt

<h3>Busy option</h3>

<div>
  <BusyOption Data="_customer" Context="customer">
    <Some>@customer.Name is @customer.Age years old</Some>
    <None>No customer</None>
  </BusyOption>
</div>

<button @onclick="SetCustomer">Set to Bob</button> <button @onclick="SetNoCustomer">Set to unknown</button>

@code {
  public Option<Customer>? _customer;
}

@functions {
  private void SetCustomer() =>
    _customer = Get(1, "Bob", 42);

  private void SetNoCustomer() =>
    _customer = Option<Customer>.None;

  public record Customer(int Id, string Name, int Age);

  private static Option<Customer> Get(int Id, string Name, int Age) =>
    new Customer(Id, Name, Age);
}

This is obviously a very simplistic sample, but it shows the idea. When you first load the page, you see the “Loading…” message, and then when you click one of the buttons, you see what would happen if the customer were found (`Some`) or not (`None`).

Another example of how Blazor can make life sooo much easier.

This got me thinking – no mean feat in itself! There are some obvious ways you could take this further. it wouldn’t be hard to write a ValidationBlazor component that took a Validation object and displayed the error messages (if there were any). You could write an EitherBlazor component to handle the two cases that Either produces. The possibilities are (almost) endless.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.