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

Prepend devtool handler #1358

Merged
merged 3 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
16 changes: 14 additions & 2 deletions docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ const store = new Vuex.Store({ ...options })

### subscribe

- `subscribe(handler: Function): Function`
- `subscribe(handler: Function, options?: Object): Function`

Subscribe to store mutations. The `handler` is called after every mutation and receives the mutation descriptor and post-mutation state as arguments:

Expand All @@ -173,13 +173,19 @@ const store = new Vuex.Store({ ...options })
})
```

By default, new handler is added to the end of the chain, so it will be executed after other handlers that were added before. This can be overriden by adding `prepend: true` to `options`, which will add the handler to the beginning of the chain.

``` js
store.subscribe(handler, { prepend: true })
```

To stop subscribing, call the returned unsubscribe function.

Most commonly used in plugins. [Details](../guide/plugins.md)

### subscribeAction

- `subscribeAction(handler: Function): Function`
- `subscribeAction(handler: Function, options?: Object): Function`

> New in 2.5.0

Expand All @@ -192,6 +198,12 @@ const store = new Vuex.Store({ ...options })
})
```

By default, new handler is added to the end of the chain, so it will be executed after other handlers that were added before. This can be overriden by adding `prepend: true` to `options`, which will add the handler to the beginning of the chain.

``` js
store.subscribe(handler, { prepend: true })
```

To stop subscribing, call the returned unsubscribe function.

Most commonly used in plugins. [Details](../guide/plugins.md)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/devtool.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export default function devtoolPlugin (store) {

store.subscribe((mutation, state) => {
devtoolHook.emit('vuex:mutation', mutation, state)
})
}, { prepend: true })
}
14 changes: 8 additions & 6 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ export class Store {
: entry[0](payload)
}

subscribe (fn) {
return genericSubscribe(fn, this._subscribers)
subscribe (fn, options) {
return genericSubscribe(fn, this._subscribers, options)
}

subscribeAction (fn) {
return genericSubscribe(fn, this._actionSubscribers)
subscribeAction (fn, options) {
return genericSubscribe(fn, this._actionSubscribers, options)
}

watch (getter, cb, options) {
Expand Down Expand Up @@ -198,9 +198,11 @@ export class Store {
}
}

function genericSubscribe (fn, subs) {
function genericSubscribe (fn, subs, options) {
if (subs.indexOf(fn) < 0) {
subs.push(fn)
options && options.prepend
? subs.unshift(fn)
: subs.push(fn)
}
return () => {
const i = subs.indexOf(fn)
Expand Down