Coder Social home page Coder Social logo

Comments (5)

neozhu avatar neozhu commented on May 2, 2024 1

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);
    }
}

from stl.fusion.

alexyakunin avatar alexyakunin commented on May 2, 2024

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 :)

from stl.fusion.

alexyakunin avatar alexyakunin commented on May 2, 2024

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.

from stl.fusion.

neozhu avatar neozhu commented on May 2, 2024

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);
    }
}

from stl.fusion.

alexyakunin avatar alexyakunin commented on May 2, 2024

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

from stl.fusion.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.