Skip to content

Commit 65f1941

Browse files
committed
✨ Use more explicit mutations names
Closes #60 Signed-off-by: Eduardo San Martin Morote <[email protected]>
1 parent 48e8af9 commit 65f1941

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

packages/vuexfire/src/index.js

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,20 @@ import {
44
indexForKey,
55
getKey,
66
isObject,
7-
VUEXFIRE_OBJECT_VALUE,
8-
VUEXFIRE_ARRAY_INITIALIZE,
9-
VUEXFIRE_ARRAY_ADD,
10-
VUEXFIRE_ARRAY_CHANGE,
11-
VUEXFIRE_ARRAY_MOVE,
12-
VUEXFIRE_ARRAY_REMOVE,
13-
VUEXFIRE_MUTATION,
147
mutations,
158
} from './utils/index.js'
169

17-
export const firebaseMutations = {
10+
import * as types from './utils/types.js'
11+
12+
export const firebaseMutations = {}
13+
14+
Object.keys(types).forEach(key => {
1815
// the { commit, state, type, ...payload } syntax is not supported by buble...
19-
[VUEXFIRE_MUTATION] (_, context) {
20-
mutations[context.type](context.state, context)
21-
},
22-
}
16+
const type = types[key]
17+
firebaseMutations[type] = (_, context) => {
18+
mutations[type](context.state, context)
19+
}
20+
})
2321

2422
function bindAsObject ({
2523
key,
@@ -29,8 +27,8 @@ function bindAsObject ({
2927
state,
3028
}) {
3129
const cb = source.on('value', function (snapshot) {
32-
commit(VUEXFIRE_MUTATION, {
33-
type: VUEXFIRE_OBJECT_VALUE,
30+
commit(types.VUEXFIRE_OBJECT_VALUE, {
31+
type: types.VUEXFIRE_OBJECT_VALUE,
3432
key,
3533
record: createRecord(snapshot),
3634
state,
@@ -49,16 +47,16 @@ function bindAsArray ({
4947
state,
5048
}) {
5149
// Initialise the array to an empty one
52-
commit(VUEXFIRE_MUTATION, {
53-
type: VUEXFIRE_ARRAY_INITIALIZE,
50+
commit(types.VUEXFIRE_ARRAY_INITIALIZE, {
51+
type: types.VUEXFIRE_ARRAY_INITIALIZE,
5452
state,
5553
key,
5654
})
5755
const onAdd = source.on('child_added', function (snapshot, prevKey) {
5856
const array = state[key]
5957
const index = prevKey ? indexForKey(array, prevKey) + 1 : 0
60-
commit(VUEXFIRE_MUTATION, {
61-
type: VUEXFIRE_ARRAY_ADD,
58+
commit(types.VUEXFIRE_ARRAY_ADD, {
59+
type: types.VUEXFIRE_ARRAY_ADD,
6260
state,
6361
key,
6462
index,
@@ -69,8 +67,8 @@ function bindAsArray ({
6967
const onRemove = source.on('child_removed', function (snapshot) {
7068
const array = state[key]
7169
const index = indexForKey(array, getKey(snapshot))
72-
commit(VUEXFIRE_MUTATION, {
73-
type: VUEXFIRE_ARRAY_REMOVE,
70+
commit(types.VUEXFIRE_ARRAY_REMOVE, {
71+
type: types.VUEXFIRE_ARRAY_REMOVE,
7472
state,
7573
key,
7674
index,
@@ -80,8 +78,8 @@ function bindAsArray ({
8078
const onChange = source.on('child_changed', function (snapshot) {
8179
const array = state[key]
8280
const index = indexForKey(array, getKey(snapshot))
83-
commit(VUEXFIRE_MUTATION, {
84-
type: VUEXFIRE_ARRAY_CHANGE,
81+
commit(types.VUEXFIRE_ARRAY_CHANGE, {
82+
type: types.VUEXFIRE_ARRAY_CHANGE,
8583
state,
8684
key,
8785
index,
@@ -95,8 +93,8 @@ function bindAsArray ({
9593
var newIndex = prevKey ? indexForKey(array, prevKey) + 1 : 0
9694
// TODO refactor + 1
9795
newIndex += index < newIndex ? -1 : 0
98-
commit(VUEXFIRE_MUTATION, {
99-
type: VUEXFIRE_ARRAY_MOVE,
96+
commit(types.VUEXFIRE_ARRAY_MOVE, {
97+
type: types.VUEXFIRE_ARRAY_MOVE,
10098
state,
10199
key,
102100
index,

0 commit comments

Comments
 (0)