Skip to content

Commit d3593a2

Browse files
committed
Merge branch 'dev' into 4.0
2 parents 5b44e8e + 38c11dc commit d3593a2

33 files changed

+323
-134
lines changed

dist/logger.d.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
/**
2-
* Types for the logger plugin.
3-
* This file must be put alongside the JavaScript file of the logger.
4-
*/
1+
// Types for the logger plugin. This file must be put alongside the bundled
2+
// JavaScript file of the logger.
53

64
import { Payload, Plugin } from "../types/index";
75

@@ -10,6 +8,10 @@ export interface LoggerOption<S> {
108
filter?: <P extends Payload>(mutation: P, stateBefore: S, stateAfter: S) => boolean;
119
transformer?: (state: S) => any;
1210
mutationTransformer?: <P extends Payload>(mutation: P) => any;
11+
actionFilter?: <P extends Payload>(action: P, state: S) => boolean;
12+
actionTransformer?: <P extends Payload>(action: P) => any;
13+
logMutations?: boolean;
14+
logActions?: boolean;
1315
}
1416

1517
export default function createLogger<S>(option?: LoggerOption<S>): Plugin<S>;

docs/api/README.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ const store = new Vuex.Store({ ...options })
109109
110110
### strict
111111
112-
- type: `Boolean`
112+
- type: `boolean`
113113
- default: `false`
114114
115115
Force the Vuex store into strict mode. In strict mode any mutations to Vuex state outside of mutation handlers will throw an Error.
@@ -118,7 +118,7 @@ const store = new Vuex.Store({ ...options })
118118
119119
### devtools
120120
121-
- type: `Boolean`
121+
- type: `boolean`
122122
123123
Turn the devtools on or off for a particular vuex instance. For instance passing false tells the Vuex store to not subscribe to devtools plugin. Useful for if you have multiple stores on a single page.
124124
@@ -128,7 +128,6 @@ const store = new Vuex.Store({ ...options })
128128
}
129129
```
130130
131-
132131
## Vuex.Store Instance Properties
133132
134133
### state
@@ -154,8 +153,8 @@ const store = new Vuex.Store({ ...options })
154153
155154
### dispatch
156155
157-
- `dispatch(type: string, payload?: any, options?: Object)`
158-
- `dispatch(action: Object, options?: Object)`
156+
- `dispatch(type: string, payload?: any, options?: Object): Promise<any>`
157+
- `dispatch(action: Object, options?: Object): Promise<any>`
159158
160159
Dispatch an action. `options` can have `root: true` that allows to dispatch root actions in [namespaced modules](../guide/modules.md#namespacing). Returns a Promise that resolves all triggered action handlers. [Details](../guide/actions.md)
161160
@@ -175,7 +174,7 @@ const store = new Vuex.Store({ ...options })
175174
176175
### subscribe
177176
178-
- `subscribe(handler: Function): Function`
177+
- `subscribe(handler: Function, options?: Object): Function`
179178
180179
Subscribe to store mutations. The `handler` is called after every mutation and receives the mutation descriptor and post-mutation state as arguments:
181180
@@ -186,13 +185,19 @@ const store = new Vuex.Store({ ...options })
186185
})
187186
```
188187
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+
189194
To stop subscribing, call the returned unsubscribe function.
190195
191196
Most commonly used in plugins. [Details](../guide/plugins.md)
192197
193198
### subscribeAction
194199
195-
- `subscribeAction(handler: Function): Function`
200+
- `subscribeAction(handler: Function, options?: Object): Function`
196201
197202
> New in 2.5.0
198203
@@ -205,6 +210,12 @@ const store = new Vuex.Store({ ...options })
205210
})
206211
```
207212
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+
208219
To stop subscribing, call the returned unsubscribe function.
209220
210221
> New in 3.1.0
@@ -240,7 +251,7 @@ const store = new Vuex.Store({ ...options })
240251
241252
### hasModule
242253
243-
- `hasModule(path: string | Array<string>)`
254+
- `hasModule(path: string | Array<string>): boolean`
244255
245256
Check if the module with the given name is already registered. [Details](../guide/modules.md#dynamic-module-registration)
246257

docs/fr/api/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ const store = new Vuex.Store({ ...options })
107107
108108
### strict
109109
110-
- type : `Boolean`
110+
- type : `boolean`
111111
- default: `false`
112112
113113
Force le store Vuex en mode strict. En mode strict, toute mutation de l'état en dehors des gestionnaires de mutation lancera une erreur.
@@ -139,8 +139,8 @@ const store = new Vuex.Store({ ...options })
139139

140140
### dispatch
141141

142-
- `dispatch(type: string, payload?: any, options?: Object)`
143-
- `dispatch(action: Object, options?: Object)`
142+
- `dispatch(type: string, payload?: any, options?: Object): Promise<any>`
143+
- `dispatch(action: Object, options?: Object): Promise<any>`
144144

145145
Propager une action. Retourne la valeur renvoyée par le gestionnaire d'action déclenché, ou une Promesse si plusieurs gestionnaires ont été déclenchés. [Plus de détails](../guide/actions.md)
146146

docs/fr/guide/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ Au cœur de chaque application Vuex, il y a la **zone de stockage (« store »)*
1010

1111
### Le store le plus simple
1212

13-
> **NOTE:** Nous allons utiliser la syntaxe ES2015 dans les exemples de code pour le reste de la documentation. Si vous ne vous êtes pas encore penché dessus, [vous devriez](https://babeljs.io/docs/learn-es2015/) !
13+
:::tip NOTE
14+
Nous allons utiliser la syntaxe ES2015 dans les exemples de code pour le reste de la documentation. Si vous ne vous êtes pas encore penché dessus, [vous devriez](https://babeljs.io/docs/learn-es2015/) !
15+
:::
1416

1517
Après [avoir installé](../installation.md) Vuex, nous allons créer un store. C'est assez simple ; définissez juste un objet d'état initial et quelques mutations :
1618

docs/fr/guide/mutations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Puisqu'un état de store de Vuex est rendu réactif par Vue, lorsque nous mutons
8888

8989
- Utiliser `Vue.set(obj, 'newProp', 123)`, ou
9090

91-
- Remplacer cet objet par un nouvel objet. Par exemple, en utilisant [opérateur de décomposition](https://github.com/tc39/proposal-object-rest-spread), il est possible d'écrire :
91+
- Remplacer cet objet par un nouvel objet. Par exemple, en utilisant l'[opérateur de décomposition](https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Op%C3%A9rateurs/Syntaxe_d%C3%A9composition#Utiliser_la_d%C3%A9composition_avec_les_litt%C3%A9raux_objet) (stage-4), il est possible d'écrire :
9292

9393
``` js
9494
state.obj = { ...state.obj, newProp: 123 }

docs/fr/guide/state.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ L'arbre d'état unique n'entre pas en conflit avec la modularité. Dans les proc
1010

1111
### Récupération d'état Vuex dans des composants Vue
1212

13-
Alors, comment affichons-nous l'état du store dans nos composants Vue ? Puisque les stores Vuex sont réactifs, la façon la plus simple d'y « récupérer » l'état est tout simplement de retourner une partie de l'état depuis une [une propriété calculée](https://fr.vuejs.org/guide/computed.html) :
13+
Alors, comment affichons-nous l'état du store dans nos composants Vue ? Puisque les stores Vuex sont réactifs, la façon la plus simple d'y « récupérer » l'état est tout simplement de retourner une partie de l'état depuis une [propriété calculée](https://fr.vuejs.org/guide/computed.html) :
1414

1515
``` js
1616
// créons un composant Counter

docs/fr/guide/testing.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@ const testAction = (action, args, state, expectedMutations, done) => {
9797

9898
try {
9999
expect(mutation.type).to.equal(type)
100-
if (payload) {
101-
expect(payload).to.deep.equal(mutation.payload)
102-
}
100+
expect(payload).to.deep.equal(mutation.payload)
103101
} catch (error) {
104102
done(error)
105103
}

docs/guide/README.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@ At the center of every Vuex application is the **store**. A "store" is basically
1010

1111
### The Simplest Store
1212

13-
> **NOTE:** We will be using ES2015 syntax for code examples for the rest of the docs. If you haven't picked it up, [you should](https://babeljs.io/docs/learn-es2015/)!
13+
:::tip NOTE
14+
We will be using ES2015 syntax for code examples for the rest of the docs. If you haven't picked it up, [you should](https://babeljs.io/docs/learn-es2015/)!
15+
:::
1416

1517
After [installing](../installation.md) Vuex, let's create a store. It is pretty straightforward - just provide an initial state object, and some mutations:
1618

1719
``` js
18-
// Make sure to call Vue.use(Vuex) first if using a module system
20+
import Vue from 'vue'
21+
import Vuex from 'vuex'
22+
23+
Vue.use(Vuex)
1924

2025
const store = new Vuex.Store({
2126
state: {
@@ -37,6 +42,37 @@ store.commit('increment')
3742
console.log(store.state.count) // -> 1
3843
```
3944

45+
In order to have an access to `this.$store` property in your Vue components, you need to provide the created store to Vue instance. Vuex has a mechanism to "inject" the store into all child components from the root component with the `store` option:
46+
47+
``` js
48+
new Vue({
49+
el: '#app',
50+
store: store,
51+
})
52+
```
53+
54+
:::tip
55+
If you're using ES6, you can also go for ES6 object property shorthand notation (it's used when object key has the same name as the variable passed-in as a property):
56+
57+
```js
58+
new Vue({
59+
el: '#app',
60+
store
61+
})
62+
```
63+
:::
64+
65+
Now we can commit a mutation from component's method:
66+
67+
``` js
68+
methods: {
69+
increment() {
70+
this.$store.commit('increment')
71+
console.log(this.$store.state.count)
72+
}
73+
}
74+
```
75+
4076
Again, the reason we are committing a mutation instead of changing `store.state.count` directly, is because we want to explicitly track it. This simple convention makes your intention more explicit, so that you can reason about state changes in your app better when reading the code. In addition, this gives us the opportunity to implement tools that can log every mutation, take state snapshots, or even perform time travel debugging.
4177

4278
Using store state in a component simply involves returning the state within a computed property, because the store state is reactive. Triggering changes simply means committing mutations in component methods.

docs/guide/plugins.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ const logger = createLogger({
109109
// `mutation` is a `{ type, payload }`
110110
return mutation.type !== "aBlacklistedMutation"
111111
},
112+
actionFilter (action, state) {
113+
// same as `filter` but for actions
114+
// `action` is a `{ type, payload }`
115+
return action.type !== "aBlacklistedAction"
116+
},
112117
transformer (state) {
113118
// transform the state before logging it.
114119
// for example return only a specific sub-tree
@@ -119,6 +124,12 @@ const logger = createLogger({
119124
// we can format it any way we want.
120125
return mutation.type
121126
},
127+
actionTransformer (action) {
128+
// Same as mutationTransformer but for actions
129+
return action.type
130+
},
131+
logActions: true, // Log Actions
132+
logMutations: true, // Log mutations
122133
logger: console, // implementation of the `console` API, default `console`
123134
})
124135
```

docs/guide/testing.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@ const testAction = (action, payload, state, expectedMutations, done) => {
9797

9898
try {
9999
expect(type).to.equal(mutation.type)
100-
if (payload) {
101-
expect(payload).to.deep.equal(mutation.payload)
102-
}
100+
expect(payload).to.deep.equal(mutation.payload)
103101
} catch (error) {
104102
done(error)
105103
}

0 commit comments

Comments
 (0)