Skip to content

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

Notifications You must be signed in to change notification settings

g-un--/AsyncAgent

Repository files navigation

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.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published