Skip to content

Commit a39d076

Browse files
simplesmilerktsn
andauthored
fix: Prepend devtool handler (#1358)
* Add prepend option to subscribe * Prepend devtools subscribe handler to fix vuejs/devtools-v6#678 Co-authored-by: Katashin <[email protected]>
1 parent f698a26 commit a39d076

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

docs/api/README.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ const store = new Vuex.Store({ ...options })
174174
175175
### subscribe
176176
177-
- `subscribe(handler: Function): Function`
177+
- `subscribe(handler: Function, options?: Object): Function`
178178
179179
Subscribe to store mutations. The `handler` is called after every mutation and receives the mutation descriptor and post-mutation state as arguments:
180180
@@ -185,13 +185,19 @@ const store = new Vuex.Store({ ...options })
185185
})
186186
```
187187
188+
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.
189+
190+
``` js
191+
store.subscribe(handler, { prepend: true })
192+
```
193+
188194
To stop subscribing, call the returned unsubscribe function.
189195
190196
Most commonly used in plugins. [Details](../guide/plugins.md)
191197
192198
### subscribeAction
193199
194-
- `subscribeAction(handler: Function): Function`
200+
- `subscribeAction(handler: Function, options?: Object): Function`
195201
196202
> New in 2.5.0
197203
@@ -204,6 +210,12 @@ const store = new Vuex.Store({ ...options })
204210
})
205211
```
206212
213+
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.
214+
215+
``` js
216+
store.subscribe(handler, { prepend: true })
217+
```
218+
207219
To stop subscribing, call the returned unsubscribe function.
208220
209221
> New in 3.1.0

src/plugins/devtool.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ export default function devtoolPlugin (store) {
1818

1919
store.subscribe((mutation, state) => {
2020
devtoolHook.emit('vuex:mutation', mutation, state)
21-
})
21+
}, { prepend: true })
2222

2323
store.subscribeAction((action, state) => {
2424
devtoolHook.emit('vuex:action', action, state)
25-
})
25+
}, { prepend: true })
2626
}

src/store.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,13 @@ export class Store {
164164
})
165165
}
166166

167-
subscribe (fn) {
168-
return genericSubscribe(fn, this._subscribers)
167+
subscribe (fn, options) {
168+
return genericSubscribe(fn, this._subscribers, options)
169169
}
170170

171-
subscribeAction (fn) {
171+
subscribeAction (fn, options) {
172172
const subs = typeof fn === 'function' ? { before: fn } : fn
173-
return genericSubscribe(subs, this._actionSubscribers)
173+
return genericSubscribe(subs, this._actionSubscribers, options)
174174
}
175175

176176
watch (getter, cb, options) {
@@ -238,9 +238,11 @@ export class Store {
238238
}
239239
}
240240

241-
function genericSubscribe (fn, subs) {
241+
function genericSubscribe (fn, subs, options) {
242242
if (subs.indexOf(fn) < 0) {
243-
subs.push(fn)
243+
options && options.prepend
244+
? subs.unshift(fn)
245+
: subs.push(fn)
244246
}
245247
return () => {
246248
const i = subs.indexOf(fn)

0 commit comments

Comments
 (0)