Skip to content

listenerApi.condition not working as expected #2698

Open
@skavan

Description

@skavan

Hi,

Forgive any poor terminology, I'm relatively new at redux.

I have created a test reducer (uselessReducer), a listenerMiddleware, and my general appMiddleware.
uselessReducer has an action, updateStatus. the listenerMiddleware has an actionCreator on updateStatus.

In my general appMiddleware, if the useless/init case is triggered, the updateStatus action is dispatched and my listener is triggered, its condition is met (connected: true) and it executes as expected. All good.

    return function (next) {
        return function (action) {
            switch (action.type) {
                case "useless/init":
                    store.dispatch(updateStatus({ connected: true, status: "connected" }));
                    return
                default:
                    break;
            }
            return next(action);
        };
    };

Here's the listener:

listenerMiddleware.startListening({
  actionCreator: updateStatus,
  effect: async (action, listenerApi) => {
        // Can cancel other running instances
        listenerApi.cancelActiveListeners()  
        console.log("LISTENER RUNNING")

        if (await listenerApi.condition((action, currentState) =>
          currentState.useless.connected === true
        )){
          console.log("LISTENER TRIGGERED")
        }
        
  }
})

However, if I change the appMiddleware and dispatch the action inside the .then(..), then the action is correctly dispatched, the listener is run, but the condition doesn't work and the following code is never executed. I must be missing something pretty fundamental, but can't figure out what! What am I missing here?

case "useless/init":
       axios.get("http://somewebsite.xxx/").then((res) => {
               store.dispatch(updateStatus({ connected: true, status: "connected" }));                    
       });
       return

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions