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);
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);
git clone https://github.com/g-un--/AsyncAgent.git
.\AsyncAgent\build.cmd