Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call unsubscribe many times will remove other listeners #938

Closed
jzlxiaohei opened this issue Oct 23, 2015 · 1 comment
Closed

Call unsubscribe many times will remove other listeners #938

jzlxiaohei opened this issue Oct 23, 2015 · 1 comment
Labels

Comments

@jzlxiaohei
Copy link
Contributor

I write a test

const store = createStore(reducers.todos);
const listenerA = expect.createSpy(() => {});
const listenerB = expect.createSpy(() => {});

let unsubscribeA = store.subscribe(listenerA);
store.subscribe(listenerB);

unsubscribeA();
unsubscribeA();//invoke two times

store.dispatch(unknownAction());
expect(listenerA.calls.length).toBe(0);
expect(listenerB.calls.length).toBe(1); // failed

reason:

arr.splice(-1,1) still remove the item of the arr.

//createStore.js

function subscribe(listener) {
    listeners.push(listener);

    return function unsubscribe() {
      var index = listeners.indexOf(listener);
      // if index ===1 ,should return
      listeners.splice(index, 1);
    };

}

jzlxiaohei added a commit to jzlxiaohei/redux that referenced this issue Oct 23, 2015
@jzlxiaohei jzlxiaohei changed the title Call unsubscribe many time will remove other listeners Call unsubscribe many times will remove other listeners Oct 23, 2015
gaearon added a commit that referenced this issue Oct 23, 2015
fixed unsubscribe bug for issue #938
@gaearon
Copy link
Contributor

gaearon commented Oct 23, 2015

Fixed in #939 and b7031ce.
Out in 3.0.4.
Thanks.

@gaearon gaearon closed this as completed Oct 23, 2015
@gaearon gaearon added the bug label Oct 23, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants