public static void Go()
        {
            var dispatcher = new AppDispatcher();
            var store      = new AppUIStore(dispatcher, new MessageApi(dispatcher));

            var container = Document.GetElementById("main");

            container.ClassName = string.Join(" ", container.ClassName.Split().Where(c => c != "loading"));
            React.Render(
                new AppContainer(store, dispatcher),
                container
                );

            // After the Dispatcher and the Store and the Container Component are all associated with each other, the Store needs to be told that
            // it's time to set its initial state, so that the Component can receive an OnChange event and draw itself accordingly. In a more
            // complicated app, this would probably be an event fired by the router - initialising the Store appropriate to the current URL,
            // but in this case there's only a single Store to initialise.
            dispatcher.Dispatch(new StoreInitialised(store));
        }
Beispiel #2
0
        public static void RunTests()
        {
            Module("AppDispatcher");

            Test("Broadcast action to single receiver", assert =>
            {
                var dispatcher = new AppDispatcher();
                dispatcher.Receive(action =>
                {
                    assert.Equal(action.GetType(), typeof(SomethingHappened));
                });
                dispatcher.Dispatch(new SomethingHappened());
            });

            Test("Broadcast action to two receivers, where the second to subscribe waits for the first to complete", assert =>
            {
                var dispatcher             = new AppDispatcher();
                var firstReceiverProcessed = false;
                var token = dispatcher.Receive(action =>
                {
                    firstReceiverProcessed = true;
                });
                dispatcher.Receive(action =>
                {
                    dispatcher.WaitFor(token);
                    assert.Equal(firstReceiverProcessed, true);
                });
                dispatcher.Dispatch(new SomethingHappened());
            });

            Test("Broadcast action to two receivers, where the first to subscribe waits for the second to complete", assert =>
            {
                var dispatcher              = new AppDispatcher();
                DispatchToken token         = null;
                var secondReceiverProcessed = false;
                dispatcher.Receive(action =>
                {
                    dispatcher.WaitFor(token);
                    assert.Equal(secondReceiverProcessed, true);
                });
                token = dispatcher.Receive(action =>
                {
                    secondReceiverProcessed = true;
                });
                dispatcher.Dispatch(new SomethingHappened());
            });

            Test("Broadcast action to two receivers that WaitFor each other - expect circular dependency failure", assert =>
            {
                var dispatcher       = new AppDispatcher();
                DispatchToken token2 = null;
                var token1           = dispatcher.Receive(action =>
                {
                    dispatcher.WaitFor(token2);
                });
                token2 = dispatcher.Receive(action =>
                {
                    dispatcher.WaitFor(token1);
                });
                assert.Throws(() => dispatcher.Dispatch(new SomethingHappened()), "Dispatch should throw in this case");
            });

            Test("Broadcast action to three receivers where the first waits on the third, the second waits on the first and the third waits on the second - expect circular dependency failure", assert =>
            {
                var dispatcher       = new AppDispatcher();
                DispatchToken token3 = null;
                var token1           = dispatcher.Receive(action =>
                {
                    dispatcher.WaitFor(token3);
                });
                var token2 = dispatcher.Receive(action =>
                {
                    dispatcher.WaitFor(token1);
                });
                token3 = dispatcher.Receive(action =>
                {
                    dispatcher.WaitFor(token2);
                });
                assert.Throws(() => dispatcher.Dispatch(new SomethingHappened()), "Dispatch should throw in this case");
            });
        }