Skip to content

Latest commit

 

History

History
45 lines (41 loc) · 1.2 KB

README.md

File metadata and controls

45 lines (41 loc) · 1.2 KB

AsyncAgent

Actor which executes a Func<TState, T, CancellationToken, Task<TState>> for each message from a ConcurrentQueue<T>, accumulating an internal state.

var agent = new AsyncAgent<int, int>(
    initialState: 0,
    messageHandler: async (state, msg, ct) =>
    {
        //do stuff
        await Task.Delay(0, ct);
        return state + msg;
    },
    errorHandler: (ex, ct) => Task.FromResult(true));

//it is safe to send messages from multiple threads
agent.Send(1);

ReactiveAsyncAgent

AsyncAgent with an IObservable<State>

var agent = new ReactiveAsyncAgent<int, int>(
    initialState: 0,
    messageHandler: async (state, msg, ct) =>
    {
        //do stuff
        await Task.Delay(0, ct);
        return state + msg;
    },
    errorHandler: (ex, ct) => Task.FromResult(true));

agent.State.Subscribe(state =>
{
    //observe state
});

//it is safe to send messages from multiple threads
agent.Send(1);

Build solution

git clone https://github.com/g-un--/AsyncAgent.git
.\AsyncAgent\build.cmd

Build status