示例#1
0
        public async Task ExampleUsage2()
        {
            var t = Log.MethodEntered("DataStoreExample2.ExampleUsage2");

            // Add a thunk middleware to allow dispatching async actions:
            var thunkMiddleware = Middlewares.NewThunkMiddleware <MyAppState1>();

            // aDD A logging middleware to log all dispatched actions:
            var loggingMiddleware = Middlewares.NewLoggingMiddleware <MyAppState1>();

            // Add a recorder middleware to enable hot reload by replaying previously recorded actions:
            var recorder      = new ReplayRecorder <MyAppState1>();
            var recMiddleware = recorder.CreateMiddleware();

            var undoable = new UndoRedoReducer <MyAppState1>();
            // To allow undo redo on the full store wrap the main reducer with the undo reducer:
            var undoReducer = undoable.Wrap(MyReducers1.ReduceMyAppState1);

            var data  = new MyAppState1(null, null, 0); // the initial immutable state
            var store = new DataStore <MyAppState1>(undoReducer, data, loggingMiddleware, recMiddleware, thunkMiddleware);

            store.storeName = "Store 1";

            TestNormalDispatchOfActions(store);

            TestUndoAndRedo(store);

            await TestAsyncActions(store);

            await TestReplayRecorder(recorder, store);

            TestSkippableUndoActions(store);

            Log.MethodDone(t);
        }
示例#2
0
        private async Task TestReplayRecorderOnNewStore(ReplayRecorder <MyAppState1> recorder, MyAppState1 finalStateOfFirstStore)
        {
            var t = Log.MethodEntered("TestReplayRecorderOnNewStore");

            // Connect the recorder to the new store:
            var recMiddleware = recorder.CreateMiddleware();
            var undoable      = new UndoRedoReducer <MyAppState1>();
            var logging       = Middlewares.NewLoggingMiddleware <MyAppState1>();

            var data2  = new MyAppState1(null, null, 0);
            var store2 = new DataStore <MyAppState1>(undoable.Wrap(MyReducers1.ReduceMyAppState1), data2, logging, recMiddleware);

            store2.storeName = "Store 2";

            // Replaying the recorder will now fill the second store with the same actions:
            await recorder.ReplayStore();

            AssertEqualJson(finalStateOfFirstStore, store2.GetState());

            Log.MethodDone(t);
        }
示例#3
0
        public async Task TestReplayRecorder()
        {
            // Add a recorder middleware to enable hot reload by replaying previously recorded actions:
            var recorder      = new ReplayRecorder <string>();
            var recMiddleware = recorder.CreateMiddleware();
            // Test that exceptions are thrown if ReplayStore is not yet ready to use:
            await Assert.ThrowsAsync <NullReferenceException>(async() => { await recorder.ReplayStore(); });

            var store = new DataStore <string>(StateReducer, "", recMiddleware);
            await recorder.ReplayStore(); // Does nothing but does not throw an exception

            // Test that exceptions are thrown if an invalid nrOfActionsToReplay is passed into ReplayStore():
            await Assert.ThrowsAsync <ArgumentException>(async() => {
                await recorder.ReplayStore(delayBetweenStepsInMs: 0, nrOfActionsToReplay: 1);
            });

            // Test that Replay does not change the recorder.recordedActionsCount:
            Assert.Equal(0, recorder.recordedActionsCount);
            store.Dispatch("1");                            // Dispatch a first event
            Assert.Equal(1, recorder.recordedActionsCount); // Now the count must be 1
            await recorder.ReplayStore();

            Assert.Equal(1, recorder.recordedActionsCount); // Recorded actions still 1 after replay
        }