public async Task DispatchAsyncActionCreatorThatThrowsExcepitionHandleExceptionTest() { AppState Reducer(AppState state, IAction action) { return(state ?? new AppState()); } var asyncActionCreator = new AsyncActionCreator <AppState>(async(_, __, callback) => { await Task.Yield(); throw new NotSupportedException(); }); var store = new Store <AppState>(Reducer, new AppState()); Exception actual = null; var observer = new ActionObserver <AppState>() { Error = (error) => { actual = error; } }; store.Subscribe(observer); await store.Dispatch(asyncActionCreator); Assert.NotNull(actual); Assert.IsType <NotSupportedException>(actual); }
public async Task DispatchAsyncActionCreatorHandleExceptionTest() { AppState Reducer(AppState state, IAction action) { if (action is StandardAction std && std.Type == "test") { throw new NotSupportedException(); } return(state ?? new AppState()); } var asyncActionCreator = new AsyncActionCreator <AppState>(async(_, __, callback) => { await Task.FromResult(0); callback((x, y) => new StandardAction("test")); }); var store = new Store <AppState>(Reducer, new AppState()); Exception actual = null; var observer = new ActionObserver <AppState>() { Error = (error) => { actual = error; } }; store.Subscribe(observer); await store.Dispatch(asyncActionCreator); Assert.NotNull(actual); Assert.IsType <NotSupportedException>(actual); }
public async Task DispatchNullAsyncActionCreatorTest() { var store = new Store <AppState>(AppReducer.Invoke); AsyncActionCreator <AppState> asyncActionCreator = null; await Assert.ThrowsAsync <ArgumentNullException>(async() => { await store.Dispatch(asyncActionCreator); }); }
/// <summary> /// Dispatches an async action creator. /// </summary> /// <param name="asyncActionCreator"> /// A function that creates and dispatches actions asynchronously. /// </param> /// <returns>A task that represents the asynchronous dispatch actions.</returns> public async Task Dispatch(AsyncActionCreator <TState> asyncActionCreator) { if (asyncActionCreator == null) { throw new ArgumentNullException(nameof(asyncActionCreator)); } try { await asyncActionCreator(State, this, actionCreator => { var action = actionCreator(State, this); if (action != null) { dispatcher(action); } }).ConfigureAwait(false); } catch (Exception ex) { OnError(ex); } }
public static Task DispatchAsync <TState>(this IStore <TState> store, AsyncActionCreator <TState> actionsCreator) { return(actionsCreator(store.Dispatch, store.GetState)); }