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

Prevent invalidating subscription iterator #1438

Merged
merged 5 commits into from
Dec 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class Store {
handler(payload)
})
})
this._subscribers.forEach(sub => sub(mutation, this.state))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you write some comment to share the context?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also need to do the same thing for _actionSubscribers in dispatch method.
https://github.com/vuejs/vuex/blob/dev/src/store.js#L132

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I've shared it through a new notifySubscribers function.

this._subscribers.slice().forEach(sub => sub(mutation, this.state))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm open to cleaner/faster ways to generate a shallow copy.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's ok to use slice for that.


if (
process.env.NODE_ENV !== 'production' &&
Expand Down
21 changes: 21 additions & 0 deletions test/unit/store.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,27 @@ describe('Store', () => {
expect(secondSubscribeSpy.calls.count()).toBe(2)
})

it('subscribe: should handle subscriptions with synchronous unsubscriptions', () => {
const subscribeSpy = jasmine.createSpy()
const testPayload = 2
const store = new Vuex.Store({
state: {},
mutations: {
[TEST]: () => {}
}
})

const unsubscribe = store.subscribe(() => unsubscribe())
store.subscribe(subscribeSpy)
store.commit(TEST, testPayload)

expect(subscribeSpy).toHaveBeenCalledWith(
{ type: TEST, payload: testPayload },
store.state
)
expect(subscribeSpy.calls.count()).toBe(1)
})

// store.watch should only be asserted in non-SSR environment
if (!isSSR) {
it('strict mode: warn mutations outside of handlers', () => {
Expand Down