Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
 public async Task DispatchNullAsyncActionCreatorTest()
 {
     var store = new Store <AppState>(AppReducer.Invoke);
     AsyncActionCreator <AppState> asyncActionCreator = null;
     await Assert.ThrowsAsync <ArgumentNullException>(async() =>
     {
         await store.Dispatch(asyncActionCreator);
     });
 }
Ejemplo n.º 4
0
        /// <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));
 }