static Func <IEvent[], Task> OnPublish(this IStateStore stateManager, Func <IEvent[], Task> publish) => events => Task.WhenAll(events .GroupBy(x => x.GetKey()) .Select(async x => { await publish(x.ToArray()); await stateManager.CompleteOutBoxAsync(x.ToArray()); }));