g-un-- / AsyncAgent

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

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


Languages

Language:C# 95.0%Language:F# 4.0%Language:Batchfile 1.0%