Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            // Initializes dispatcher with initial state, reducer instance and
            // the TaskScheduler where StateChanged event runs.
            var dispatcher = new ReduxDispatcher <RootState, Reducer>(
                // root state with counter set to 0
                initialState: new RootState(0),
                reducer: new Reducer(),
                // really doesn't matter since no UI thread is present
                notificationScheduler: TaskScheduler.Current
                );

            // handles all state changes with displaying the counter value
            dispatcher.StateChanged += (s, e) =>
                                       Console.WriteLine($"\nCounter is {e.State.Counter}");
            // dispatcher is all set, start it
            // it will wait for actions in a background thread
            dispatcher.Start();
            bool loop = true;

            Console.WriteLine("Type i to increment, d to decrement or any other key to exit.");
            do
            {
                char c = (char)Console.ReadKey().KeyChar;
                switch (c)
                {
                case 'i':
                    // dispatches increment action
                    dispatcher.Dispatch(new IncrementAction());
                    break;

                case 'd':
                    // dispatcher decrement action
                    dispatcher.Dispatch(new DecrementAction());
                    break;

                default:
                    // exit the loop
                    loop = false;
                    break;
                }
            } while (loop);
            dispatcher.Dispose();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to SharRedux in Blazor");
            var appState = new AppState(
                new CounterState(0),
                new FetchDataState(new WeatherForecast[0], isLoading: false, error: null));

            dispatcher = new ReduxDispatcher <AppState, Reducer>(
                initialState: appState,
                reducer: new Reducer(),
                notificationScheduler: TaskScheduler.Current);
            bool areDevToolsAvailable = ReduxDevToolsExtension.IsAvailable();

            if (areDevToolsAvailable)
            {
                ReduxDevToolsExtension.Connect();
                ReduxDevToolsExtension.Init(appState);
                ReduxDevToolsExtension.Subscribe();
                ReduxDevToolsExtension.MessageReceived += ReduxDevToolsExtension_MessageReceived;
                dispatcher.StateChanged += (s, e) =>
                {
                    if (!(e.Action is InitAction))
                    {
                        ReduxDevToolsExtension.Send(e.Action, e.State);
                    }
                };
            }

            var serviceProvider = new BrowserServiceProvider(configure =>
            {
                configure.AddSingleton <IReduxDispatcher>(dispatcher);
                configure.AddSingleton <IReduxDispatcher <AppState> >(dispatcher);
                configure.AddSingleton <Communicator>();
            });

            dispatcher.Start();

            new BrowserRenderer(serviceProvider).AddComponent <Main>("app");
        }