Closed neozhu closed 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 :)
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.
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.
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);
}
}
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);
}
}
Ok, I guess you can close the issue now :)
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:
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!