Ejemplo n.º 1
0
Archivo: App.cs Proyecto: vipwan/Fluxor
        public void Run()
        {
            Console.Clear();
            Console.WriteLine("Initializing store");
            Store.InitializeAsync().Wait();
            string input = "";

            do
            {
                Console.WriteLine("1: Increment counter");
                Console.WriteLine("2: Fetch data");
                Console.WriteLine("x: Exit");
                Console.Write("> ");
                input = Console.ReadLine();

                switch (input.ToLowerInvariant())
                {
                case "1":
                    var incrementCounterActionction = new IncrementCounterAction();
                    Dispatcher.Dispatch(incrementCounterActionction);
                    break;

                case "2":
                    var fetchDataAction = new FetchDataAction();
                    Dispatcher.Dispatch(fetchDataAction);
                    break;

                case "x":
                    Console.WriteLine("Program terminated");
                    return;
                }
            } while (true);
        }
Ejemplo n.º 2
0
        public async Task StartCounterTimer()
        {
            if (this.timer != null)
            {
                throw new Exception("Can't start: already started");
            }

            await this.Dispatch(new StartCounterAction());

            await this.actionsToClientStream.OnNextAsync(new CounterStartedAction());

            await this.WriteStateAsync();

            this.timer = this.RegisterTimer(async(state) => {
                var action = new IncrementCounterAction();
                // This sends the action to the clients for processing there
                await this.actionsToClientStream.OnNextAsync(action);

                // This processes the action here on the server, and also sends the syncstate to make sure the outcome is the same
                // The order of events is important here
                await this.Dispatch(action);
                await this.WriteStateAsync();
            }, null, TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(3));

            await this.actionsToClientStream.OnNextAsync(new CounterStartedAction());
        }
Ejemplo n.º 3
0
        private async void  IncrementCount()
        {
            await ijsRuntime.InvokeVoidAsync("console.log", "dispatch an 'IncrementationCounterAction' event");

            //Console.WriteLine("The app is running on WebAssembly");

            var action = new IncrementCounterAction();

            dispatcher.Dispatch(action);
        }
Ejemplo n.º 4
0
        private void IncrementCounterInThread()
        {
            Interlocked.Decrement(ref NumberOfThreadsWaitingToStart);
            StartEvent.WaitOne();
            var action = new IncrementCounterAction();

            for (int i = 0; i < NumberOfIncrementsPerThread; i++)
            {
                Store.Dispatch(action);
            }
        }
Ejemplo n.º 5
0
 public static CounterState ReduceIncrementCounterAction(CounterState state, IncrementCounterAction action)
 {
     if (state == null)
     {
         throw new ArgumentNullException(nameof(state));
     }
     if (action == null)
     {
         throw new ArgumentNullException(nameof(action));
     }
     return(new CounterState(clickCount: state.ClickCount + 1));
 }
Ejemplo n.º 6
0
        public async Task HandleIncrementCounterAction(IncrementCounterAction action, IDispatcher dispatcher)
        {
            // Just to show even though the render error is thrown, the IState gets updated on Dispose
            _logger.LogInformation("Handling IncrementCounterAction");
            _logger.LogInformation("State pre-delay -------------");
            _logger.LogInformation(JsonConvert.SerializeObject(CounterState.Value));
            _logger.LogInformation("-------------");
            await Task.Delay(5000);

            _logger.LogInformation("State post-delay -------------");
            _logger.LogInformation(JsonConvert.SerializeObject(CounterState.Value));
            _logger.LogInformation("-------------");
        }
Ejemplo n.º 7
0
        public async Task Should_Increment_Counter()
        {
            CounterState.Initialize(aCount: 22);

            var incrementCounterRequest = new IncrementCounterAction
            {
                Amount = 5
            };

            CounterState = await Mediator.Send(incrementCounterRequest);

            CounterState.Count.ShouldBe(27);
        }
        public async Task Should_Decrement_Counter()
        {
            //Arrange
            CounterState.Initialize(aCount: 15);

            var incrementCounterRequest = new IncrementCounterAction
            {
                Amount = -2
            };

            //Act
            _ = await Mediator.Send(incrementCounterRequest);

            //Assert
            CounterState.Count.ShouldBe(13);
        }
        public async Task Increment_Count()
        {
            //Arrange
            CounterState.Initialize(aCount: 22);

            var incrementCounterRequest = new IncrementCounterAction
            {
                Amount = 5
            };

            //Act
            await Send(incrementCounterRequest);

            //Assert
            CounterState.Count.ShouldBe(27);
        }
        public async Task Decrement_Count_Given_NegativeAmount()
        {
            //Arrange
            CounterState.Initialize(aCount: 15);

            var incrementCounterRequest = new IncrementCounterAction
            {
                Amount = -2
            };

            //Act
            await Send(incrementCounterRequest);

            //Assert
            CounterState.Count.ShouldBe(13);
        }
Ejemplo n.º 11
0
        public async Task ShouldCloneState()
        {
            //Arrange
            CounterState.Initialize(aCount: 15);
            Guid preActionGuid = CounterState.Guid;

            // Create request
            var incrementCounterRequest = new IncrementCounterAction
            {
                Amount = -2
            };

            //Act
            CounterState = await Mediator.Send(incrementCounterRequest);

            //Assert
            CounterState.Guid.ShouldNotBe(preActionGuid);
        }
Ejemplo n.º 12
0
        public void Run()
        {
            Console.Clear();
            Console.WriteLine("Initializing store");

            Store.InitializeAsync().Wait();
            SubscribeToResultAction();

            string input = "";

            do
            {
                Console.WriteLine("1: Increment counter");
                Console.WriteLine("2: Fetch data");
                Console.WriteLine("3: Get mutable object from API server");
                Console.WriteLine("x: Exit");
                Console.Write("> ");

                input = Console.ReadLine();

                switch (input.ToLowerInvariant())
                {
                case "1":
                    var action = new IncrementCounterAction();
                    Dispatcher.Dispatch(action);
                    break;

                case "2":
                    var fetchDataAction = new FetchDataAction();
                    Dispatcher.Dispatch(fetchDataAction);
                    break;

                case "3":
                    var getCustomerAction = new GetCustomerForEditAction(42);
                    Dispatcher.Dispatch(getCustomerAction);
                    break;

                case "x":
                    Console.WriteLine("Program terminated");
                    return;
                }
            } while (true);
        }
Ejemplo n.º 13
0
        public void Handle_Action()
        {
            //Arrange

            string requestTypeAssemblyQualifiedName = typeof(IncrementCounterAction).AssemblyQualifiedName;
            var    incrementCounterAction           = new IncrementCounterAction
            {
                Amount = 5
            };

            string requestAsJson  = JsonSerializer.Serialize(incrementCounterAction, JsonSerializerOptions);
            int    preActionCount = CounterState.Count;

            //Act
            JsonRequestHandler.Handle(requestTypeAssemblyQualifiedName, requestAsJson);

            //Assert
            CounterState.Count.ShouldBe(preActionCount + 5);
        }
        public async Task Should_Increment_Counter()
        {
            //Arrange

            // Setup know state.
            CounterState.Initialize(aCount: 22);

            // Create request
            var incrementCounterRequest = new IncrementCounterAction
            {
                Amount = 5
            };

            //Act
            _ = await Mediator.Send(incrementCounterRequest);

            //Assert
            CounterState.Count.ShouldBe(27);
        }
        //public async Task ShouldPerformAction()
        public void ShouldPerformAction()
        {
            //Arrange

            string requestTypeAssemblyQualifiedName = typeof(IncrementCounterAction).AssemblyQualifiedName;
            var    incrementCounterAction           = new IncrementCounterAction
            {
                Amount = 5
            };

            string requestAsJson  = Json.Serialize(incrementCounterAction);
            int    preActionCount = CounterState.Count;

            //Act
            JsonRequestHandler.Handle(requestTypeAssemblyQualifiedName, requestAsJson);

            //Assert
            CounterState = Store.GetState <CounterState>();
            CounterState.Count.ShouldBe(preActionCount + 5);
        }
Ejemplo n.º 16
0
 public static CounterState ReduceIncrementCounterAction(CounterState state, IncrementCounterAction action) =>
 new CounterState(clickCount: state.ClickCount + 1);
Ejemplo n.º 17
0
 public static CounterState2 ReduceIncrementCounterAction2(CounterState2 state, IncrementCounterAction action) =>
 new CounterState2(state.ClickCount + 2);
Ejemplo n.º 18
0
        private void IncrementCount()
        {
            var action = new IncrementCounterAction();

            Dispatcher.Dispatch(action);
        }
Ejemplo n.º 19
0
 public static CounterState Reduce(CounterState state, IncrementCounterAction action)
 {
     return(new CounterState(currentCount: state.CurrentCount + 1));
 }
Ejemplo n.º 20
0
 public static CounterState ReduceIncrementCounterAction(CounterState state, IncrementCounterAction action) =>
 state with
Ejemplo n.º 21
0
 public static CounterState ReduceIncrementCounterAction(
     CounterState state, IncrementCounterAction action) =>
 CounterState.MakeStateWithNumberOfClicks(state.NumberOfClicks + 1);