Skip to content

Call unsubscribe many times will remove other listeners #938

@jzlxiaohei

Description

@jzlxiaohei

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);
    };

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions