Search code examples
javascriptreactjs-fluxflux

Basic Flux architecture - duplicate constants values?


I am trying to learn how Flux works and am confused about one specific thing - what happens when action constants for two different views have identical values?

To illustrate the source of my confusion - in a Store in the Flux architecture we are supposed to register a callback with one dispatcher in our app. (So, one assumption in this question is that our front-end applications should only have one dispatcher - this seems to be the recommended approach).

So here, inside a Store, we register a callback with the Flux dispatcher:

   AppDispatcher.register(function (payload) {
                var action = payload.action;
                var text;

                switch (action.actionType) {

                    // Respond to CART_ADD action
                    case FluxCartConstants.CART_ADD:
                        add(action.sku, action.update);
                        break;

                    // Respond to CART_VISIBLE action
                    case FluxCartConstants.CART_VISIBLE:
                        setCartVisible(action.cartVisible);
                        break;

                    // Respond to CART_REMOVE action
                    case FluxCartConstants.CART_REMOVE:
                        removeItem(action.sku);
                        break;

                    default:
                        return true;
                }

                // If action was responded to, emit change event
                thisStore.emitChange();

                return true;

            });

here are the constants for my Flux app:

 //FluxCartConstants.js

     var constants = {
        CHOLO:'CHOLO',
        ROLO:'ROLO',
        YOLO:'YOLO',
        CART_ADD:'CART_ADD'
     }


    //OtherConstants.js

     var constants = {
       CART_FOO:'CART_FOO',
       CART_VISIBLE:'CART_VISIBLE',
       CART_ADD:'CART_ADD'  //uh-oh, this constant has the same value as FluxCartConstants.CART_ADD
     }

So my question is - how is the basic Flux architecture supposed to handle duplicate constant values? It seems crazy to think that you might not accidentally have overlapping constants? Are you supposed to ensure that they have a unique value or am I missing something about flux?


Solution

  • yes, it is necessary that all action constants are unique.

    I do not see a problem here, there are many cases in programming where you have to ensure that certain keys are unique...

    Also it is true that you should have only one dispatcher in your app.