servicetitan / Stl.Fusion

Build real-time apps (Blazor included) with less than 1% of extra code responsible for real-time updates. Host 10-1000x faster APIs relying on transparent and nearly 100% consistent caching. We call it DREAM, or Distributed REActive Memoization, and it's here to turn real-time on!
MIT License
1.82k stars 106 forks source link

Assistance Needed: List Method Not Retrieving Values and Not Subscribing to Updates #646

Closed neozhu closed 8 months ago

neozhu commented 8 months ago

Hi @alexyakunin 👋,

First off, a huge thanks for creating such a wonderful library! I’ve been navigating through Stl.Fusion and have stumbled upon a little hiccup that I’m hoping you could possibly assist me with.

Here’s a snippet of code where I’m experiencing the issue:

public interface ITripMonitor : IComputeService
{
    Task<int> AddOrUpdate(int tripId, CancellationToken cancellationToken = default);
    Task Remove(int tripId, CancellationToken cancellationToken = default);
    [ComputeMethod]
    Task<List<int>> List(CancellationToken cancellationToken = default);
    [ComputeMethod]
    Task<int> Count(CancellationToken cancellationToken = default);
}
public class TripMonitor : ITripMonitor
{
    private ImmutableList<int> _tripruning = ImmutableList<int>.Empty;
    private readonly IMutableState<int> _count;
    public TripMonitor(IStateFactory stateFactory)
        => _count = stateFactory.NewMutable<int>(0);
    public virtual Task<int> AddOrUpdate(int tripId, CancellationToken cancellationToken = default)
    {
        if (Computed.IsInvalidating())
            return null!;
        using var invalidating = Computed.Invalidate();
        _tripruning = _tripruning.RemoveAll(i => i == tripId).Add(tripId);
        _count.Value +=1;
        return Task.FromResult(tripId);
    }
    public virtual async Task Remove(int tripId, CancellationToken cancellationToken = default)
    {
        if (Computed.IsInvalidating())
            return;
        using var invalidating = Computed.Invalidate();
        _tripruning = _tripruning.RemoveAll(i => i == tripId);
        _count.Value -= 1;
    }
    public virtual Task<List<int>> List(CancellationToken cancellationToken = default)
    {
        return Task.FromResult(_tripruning.ToList());
    }
    public virtual async Task<int> Count(CancellationToken cancellationToken = default)
    {
        return await _count.Use(cancellationToken);
    }
}

//===============================//
@using Stl.CommandR.Commands;
@using Stl.Fusion.Blazor;
@using CleanArchitecture.Blazor.Application.Services;
@using Stl.Fusion.Extensions;
@using Stl.Fusion.UI;
@using Stl.Fusion;
@inherits ComputedStateComponent<string>
@inject ITripMonitor _tripMonitor
@inject UICommander UICommander
@inject IFusionTime _time
@{
    var state = State.ValueOrDefault;
    var error = State.Error;
}
@state
<br>
@error
<br>
<button class="btn btn-primary" @onclick="add">Add</button>
@code {
    protected override Task OnInitializedAsync() => State.Update().AsTask();
    protected override async Task<string> ComputeState(CancellationToken cancellationToken)
    {
        var result = await _tripMonitor.List(cancellationToken);
        var count = await _tripMonitor.Count(cancellationToken);
        return $"{count} / {string.Join(',',result.ToArray())} ";
    }
    async Task add()
    {
        var v = Random.Shared.Next(100);
        await UICommander.Run(LocalCommand.New(() => _tripMonitor.AddOrUpdate(v)));
    }
}
image

Currently, I’m scratching my head a bit 🤔 because the List method doesn’t seem to be fetching values, nor subscribing to updates as I initially anticipated. I’ve tried a few things, but seem to be spinning my wheels a bit.

If you could shed some light on what might be going awry and any pointers on how to rectify it, I’d be ever so grateful! 🙏

Thanks so much for your time and assistance!

alexyakunin commented 8 months ago

Hi, AddOrRemove and Update in your code don't have a logic that invalidates List.

Computed.Invalidate() call doesn't do anything alone - it just opens a scope that forces every call to [ComputeMethod] made from this scope into invalidating call (of a method you call - with the args you pass).

So all you need is to add List() call to the end of both of these methods.

You also don't need if (Computed.IsInvalidating()) piece inside regular methods. The logic that runs the same method twice, where the second "pass" is running inside invalidating block, is applicable only for command handlers.

Hopefully this helps. Free to ask more questions :)

alexyakunin commented 8 months ago

One other change I recommend to do: move "use invalidating = ..." to the very end. You should run invalidation logic once the changes are already applied, but never earlier, coz otherwise you leave a chance for evaluation of whatever is invalidated before the change is actually made, which makes invalidation to produce zero effect.

neozhu commented 8 months ago

Thank you very much for your reply. I have already removed the Invalidate(), but the problem still persists. Could you please advise on how I should modify it.

image

public interface ITripMonitor : IComputeService
{
    Task<int> AddOrUpdate(int tripId, CancellationToken cancellationToken = default);
    Task Remove(int tripId, CancellationToken cancellationToken = default);
    [ComputeMethod]
    Task<List<int>> List(CancellationToken cancellationToken = default);
    [ComputeMethod]
    Task<int> Count(CancellationToken cancellationToken = default);
}
public class TripMonitor : ITripMonitor
{
    private ImmutableList<int> _tripruning = ImmutableList<int>.Empty;
    private readonly IMutableState<int> _count;
    public TripMonitor(IStateFactory stateFactory)
        => _count = stateFactory.NewMutable<int>(0);
    public virtual Task<int> AddOrUpdate(int tripId, CancellationToken cancellationToken = default)
    {
        _tripruning = _tripruning.RemoveAll(i => i == tripId).Add(tripId);
        _count.Value +=1;
        _ = List();
        return Task.FromResult(tripId);
    }
    public virtual async Task Remove(int tripId, CancellationToken cancellationToken = default)
    {
        _tripruning = _tripruning.RemoveAll(i => i == tripId);
        _count.Value -= 1;
        _ = List();
    }
    public virtual Task<List<int>> List(CancellationToken cancellationToken = default)
    {
        return Task.FromResult(_tripruning.ToList());
    }
    public virtual async Task<int> Count(CancellationToken cancellationToken = default)
    {
        return await _count.Use(cancellationToken);
    }
}
neozhu commented 8 months ago

it's working in this version! Thank you!😊


public interface ITripMonitor : IComputeService
{
    Task<int> AddOrUpdate(int tripId, CancellationToken cancellationToken = default);
    Task Remove(int tripId, CancellationToken cancellationToken = default);
    [ComputeMethod]
    Task<List<int>> List(CancellationToken cancellationToken = default);
    [ComputeMethod]
    Task<int> Count(CancellationToken cancellationToken = default);
}
public class TripMonitor : ITripMonitor
{
    private ImmutableList<int> _tripruning = ImmutableList<int>.Empty;
    private readonly IMutableState<int> _count;
    public TripMonitor(IStateFactory stateFactory)
        => _count = stateFactory.NewMutable<int>(0);
    public virtual async Task<int> AddOrUpdate(int tripId, CancellationToken cancellationToken = default)
    {
        _tripruning = _tripruning.RemoveAll(i => i == tripId).Add(tripId);
        _count.Value +=1;
        using var invalidating = Computed.Invalidate();
        await List();

        return tripId;
    }
    public virtual async Task Remove(int tripId, CancellationToken cancellationToken = default)
    {
        _tripruning = _tripruning.RemoveAll(i => i == tripId);
        _count.Value -= 1;
        using var invalidating = Computed.Invalidate();
        await List();
    }
    public virtual Task<List<int>> List(CancellationToken cancellationToken = default)
    {
        return Task.FromResult(_tripruning.ToList());
    }
    public virtual async Task<int> Count(CancellationToken cancellationToken = default)
    {
        return await _count.Use(cancellationToken);
    }
}
alexyakunin commented 8 months ago

Ok, I guess you can close the issue now :)