goatslacker / alt

Isomorphic flux implementation
http://alt.js.org/
3.45k stars 323 forks source link

Cannot push while pushing #616

Closed pdolega closed 8 years ago

pdolega commented 8 years ago

I just migrated to version 0.18.2 (same code worked perfectly on 0.17.9). I got following code:

UserService.fetchIdentity().then(response => {
            let result = response.data;
            let state = this.getState();
            state.user = result.baseInfo;
            state.managerInfo = result.managerInfo;
            this.setState(state);

            let roles = state.user.roles.map(role => role.name);

            console.info(`User initialized as: ${state.user.name}, id: ${state.user.id}, roles: [` + roles + "] !");

            if(onSuccess != null) {
                onSuccess();
            }
        }).catch(response => {
            if(response.status === 404) {
                // just refreshes user context - nothing to
                this.setState(this.getState());
                console.info(`User initialized as anonymous`);
            } else {
                AxiosUtils.baseErrorHandling(response);
            }
        });

It worked fine before but now in this line: this.setState(state); I am getting exception from following method (below, taken from Alt sources):

var push = function push(value) {
        if (pushing) throw new Error('Cannot push while pushing');
        pushing = true;
        try {
          subscriptions.forEach(function (subscription) {
            return subscription(value);
          });
        } finally {
          pushing = false;
          toUnsubscribe = toUnsubscribe.filter(unsubscribe);
        }
      };

Message saying: Cannot push while pushing

Any ideas what I am doing wrong ?

bradbyte commented 8 years ago

I had this issue too. I'm not sure why, but when I had multiple setStates within a given store handler for an action, and then another action for that store is called also invoking setState, it would error. The last action didn't need to emit a change so I made it directly mutate state returning false. It is now working for me. I put a breakpoint at line 50 in AltStore.js to watch each action invoking a push to find the culprit.

49           this.emitChange = function () {
50              return _this.transmitter.push(output(_this.state));
                // watch _this.displayName and look back in the stack to the Dispatcher to find the action
51           };

Hope this helps.

goatslacker commented 8 years ago

Removed this error in 0.18.3