Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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