This repository has been archived by the owner on Apr 19, 2019. It is now read-only.
/
index.js
214 lines (194 loc) · 5.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import {
createRecord,
getRef,
indexForKey,
getKey,
isObject,
mutations,
} from './utils/index'
import * as types from './utils/types'
export const firebaseMutations = {}
Object.keys(types).forEach(key => {
// the { commit, state, type, ...payload } syntax is not supported by buble...
const type = types[key]
firebaseMutations[type] = (_, context) => {
mutations[type](context.state, context)
}
})
const commitOptions = { root: true }
function bindAsObject ({
key,
source,
cancelCallback,
commit,
state,
}) {
const cb = source.on('value', function (snapshot) {
commit(types.VUEXFIRE_OBJECT_VALUE, {
type: types.VUEXFIRE_OBJECT_VALUE,
key,
record: createRecord(snapshot),
state,
}, commitOptions)
}, cancelCallback)
// return the listeners that have been setup
return { value: cb }
}
function bindAsArray ({
key,
source,
cancelCallback,
wait,
commit,
state,
}) {
// Initialise the array to an empty one
const array = []
const initializeArray = () => {
commit(types.VUEXFIRE_ARRAY_INITIALIZE, {
type: types.VUEXFIRE_ARRAY_INITIALIZE,
state,
key,
value: array,
}, commitOptions)
}
if (!wait) {
initializeArray()
} else {
source.once('value', initializeArray)
}
const onAdd = source.on('child_added', function (snapshot, prevKey) {
const index = prevKey ? indexForKey(array, prevKey) + 1 : 0
commit(types.VUEXFIRE_ARRAY_ADD, {
type: types.VUEXFIRE_ARRAY_ADD,
state,
key,
index,
array: wait && array,
record: createRecord(snapshot),
}, commitOptions)
}, cancelCallback)
const onRemove = source.on('child_removed', function (snapshot) {
const index = indexForKey(array, getKey(snapshot))
commit(types.VUEXFIRE_ARRAY_REMOVE, {
type: types.VUEXFIRE_ARRAY_REMOVE,
state,
key,
index,
array: wait && array,
}, commitOptions)
}, cancelCallback)
const onChange = source.on('child_changed', function (snapshot) {
const index = indexForKey(array, getKey(snapshot))
commit(types.VUEXFIRE_ARRAY_CHANGE, {
type: types.VUEXFIRE_ARRAY_CHANGE,
state,
key,
index,
array: wait && array,
record: createRecord(snapshot),
}, commitOptions)
}, cancelCallback)
const onMove = source.on('child_moved', function (snapshot, prevKey) {
const index = indexForKey(array, getKey(snapshot))
var newIndex = prevKey ? indexForKey(array, prevKey) + 1 : 0
// TODO refactor + 1
newIndex += index < newIndex ? -1 : 0
commit(types.VUEXFIRE_ARRAY_MOVE, {
type: types.VUEXFIRE_ARRAY_MOVE,
state,
key,
index,
newIndex,
array: wait && array,
record: createRecord(snapshot),
}, commitOptions)
}, cancelCallback)
// return the listeners that have been setup
return {
child_added: onAdd,
child_changed: onChange,
child_removed: onRemove,
child_moved: onMove,
}
}
// Firebase binding
const bindings = new WeakMap()
function bind ({
state,
commit,
key,
source,
options: {
cancelCallback,
readyCallback,
errorCallback,
wait = true,
},
}) {
if (!isObject(source)) {
throw new Error('VuexFire: invalid Firebase binding source.')
}
if (!(key in state)) {
throw new Error(`VuexFire: cannot bind undefined property '${key}'. Define it on the state first.`)
}
// Unbind if it already exists
let binding = bindings.get(commit)
if (!binding) {
binding = {
sources: Object.create(null),
listeners: Object.create(null),
}
bindings.set(commit, binding)
}
if (key in binding.sources) {
unbind({ commit, key })
}
binding.sources[key] = getRef(source)
// Support for SSR
// We have to listen for the readyCallback first so it
// gets called after the initializeArray callback
if (readyCallback || errorCallback) {
source.once('value', readyCallback, errorCallback)
}
// Automatically detects if it should be bound as an array or as an object
let listener
if (state[key] && 'length' in state[key]) {
listener = bindAsArray({ key, source, cancelCallback, wait, commit, state })
} else {
listener = bindAsObject({ key, source, cancelCallback, commit, state })
}
binding.listeners[key] = listener
}
function unbind ({ commit, key }) {
let binding = bindings.get(commit)
if (!binding) {
binding = {
sources: Object.create(null),
listeners: Object.create(null),
}
bindings.set(commit, binding)
}
if (!(key in binding.sources)) {
throw new Error(`VuexFire: cannot unbind '${key}' because it wasn't bound.`)
}
const oldSource = binding.sources[key]
const oldListeners = binding.listeners[key]
for (let event in oldListeners) {
oldSource.off(event, oldListeners[event])
}
// clean up
delete binding.sources[key]
delete binding.listeners[key]
}
export function firebaseAction (action) {
return function firebaseEnhancedActionFn (context, payload) {
// get the local state and commit. These may be bound to a module
const { state, commit } = context
context.bindFirebaseRef = (key, source, options = {}) =>
bind({ state, commit, key, source, options })
context.unbindFirebaseRef = (key) =>
unbind({ commit, key })
return action(context, payload)
}
}