From 32a57f9ec01c33cf809556d3563ec12d7772f5cd Mon Sep 17 00:00:00 2001 From: Denis Karabaza Date: Tue, 31 Jul 2018 13:59:42 +0200 Subject: [PATCH 1/2] Add prepend option to subscribe --- docs/api/README.md | 16 ++++++++++++++-- src/store.js | 14 ++++++++------ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/docs/api/README.md b/docs/api/README.md index 906167686..a17f7899a 100644 --- a/docs/api/README.md +++ b/docs/api/README.md @@ -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: @@ -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 @@ -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) diff --git a/src/store.js b/src/store.js index b5b083c92..c18095e91 100644 --- a/src/store.js +++ b/src/store.js @@ -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) { @@ -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) From 986a6d1e8618f30ce594d2f2b6104c4c34708393 Mon Sep 17 00:00:00 2001 From: Denis Karabaza Date: Tue, 31 Jul 2018 14:00:46 +0200 Subject: [PATCH 2/2] Prepend devtools subscribe handler to fix vuejs/vue-devtools#678 --- src/plugins/devtool.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/devtool.js b/src/plugins/devtool.js index a6df685f0..c3488f469 100644 --- a/src/plugins/devtool.js +++ b/src/plugins/devtool.js @@ -15,5 +15,5 @@ export default function devtoolPlugin (store) { store.subscribe((mutation, state) => { devtoolHook.emit('vuex:mutation', mutation, state) - }) + }, { prepend: true }) }