-
Notifications
You must be signed in to change notification settings - Fork 20
/
actions.js
247 lines (214 loc) · 6.07 KB
/
actions.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//@flow
import firebase from './firebase'
import * as types from './types'
import type {
MetaType, UpdateItemsActions, RemoveItemActions, ListenToPathActions
} from './types'
import { metaTypes } from './types'
export function clearData() {
return {
type: types.firebase.FIREBASE_RESET_DATA
}
}
export function updateRequested(metaType: MetaType) {
return {
type: types.firebase.FIREBASE_UPDATE_REQUESTED,
metaType
}
}
export function updateRejected(metaType: MetaType, error: string) {
return {
type: types.firebase.FIREBASE_UPDATE_REJECTED,
metaType,
error
}
}
export function updateFulfilled(metaType: MetaType) {
return {
type: types.firebase.FIREBASE_UPDATE_FULFILLED,
metaType
}
}
export function updateItem(metaType: MetaType, update: Object, path: string) {
return updateItems(metaType, [update], [path])
}
export function updateItems(metaType: MetaType, updates: Array<Object>, paths: Array<string>) {
return (dispatch: UpdateItemsActions => void) => {
dispatch(updateRequested(metaType))
const promises = []
updates.forEach((update, index) => {
const path = paths[index]
const ref = firebase.database().ref(path)
const promise = ref.update(update)
promises.push(promise)
})
return Promise.all(promises)
.then(() => {
dispatch(updateFulfilled(metaType))
})
.catch(error => {
dispatch(updateRejected(metaType, error))
})
}
}
export function removeRequested(metaType: MetaType) {
return {
type: types.firebase.FIREBASE_REMOVE_REQUESTED,
metaType
}
}
export function removeRejected(metaType: MetaType, error: string) {
return {
type: types.firebase.FIREBASE_REMOVE_REJECTED,
metaType,
error
}
}
export function removeFulfilled(metaType: MetaType) {
return {
type: types.firebase.FIREBASE_REMOVE_FULFILLED,
metaType
}
}
export function removeItem(metaType: MetaType, path: string) {
return (dispatch: RemoveItemActions => void) => {
dispatch(removeRequested(metaType))
return firebase.database().ref(path).remove()
.then(() => {
dispatch(removeFulfilled(metaType))
})
.catch((error) => {
dispatch(removeRejected(metaType, error))
})
}
}
export function listenRequested(metaType: MetaType, ref: Object) {
return {
type: types.firebase.FIREBASE_LISTEN_REQUESTED,
metaType,
ref
}
}
export function listenRejected(metaType: MetaType, error: string) {
return {
type: types.firebase.FIREBASE_LISTEN_REJECTED,
metaType,
error
}
}
export function listenFulfilled(metaType: MetaType, items: Object) {
return {
type: types.firebase.FIREBASE_LISTEN_FULFILLED,
metaType,
items
}
}
export function listenChildAdded(metaType: MetaType, id: string, value: Object) {
return {
type: types.firebase.FIREBASE_LISTEN_CHILD_ADDED,
metaType,
id,
value,
}
}
export function listenChildChanged(metaType: MetaType, id: string, value: Object) {
return {
type: types.firebase.FIREBASE_LISTEN_CHILD_CHANGED,
metaType,
id,
value,
}
}
export function listenChildRemoved(metaType: MetaType, id: string) {
return {
type: types.firebase.FIREBASE_LISTEN_CHILD_REMOVED,
metaType,
id,
}
}
export function listenRemoved(metaType: MetaType, clearItems: boolean) {
return {
type: types.firebase.FIREBASE_LISTEN_REMOVED,
metaType,
clearItems
}
}
export function removeListenerRef(state: Object, metaType: MetaType) {
if (state && state[metaType] &&
state[metaType].ref) {
return state[metaType].ref.off()
}
//returns resolved promise
return Promise.resolve()
}
export function listenToPath(metaType: MetaType, path: string) {
return (dispatch: ListenToPathActions => void, getState: () => Object) => {
//just in case we remove any existing listener
return removeListenerRef(getState(), metaType).then(() => {
const ref = firebase.database().ref(path)
dispatch(listenRequested(metaType, ref))
ref.on('child_added', (snap) => {
if (getState()[metaType].inProgress) {
return
}
const val: Object = snap.val()
dispatch(listenChildAdded(metaType, snap.key, val))
})
ref.on('child_changed', (snap) => {
if (getState()[metaType].inProgress) {
return
}
const val: Object = snap.val()
dispatch(listenChildChanged(metaType, snap.key, val))
})
ref.on('child_removed', (snap) => {
if (getState()[metaType].inProgress) {
return
}
dispatch(listenChildRemoved(metaType, snap.key))
})
return ref.once('value').then(snap => {
//better to have an empty object then a null value if data does not
//exist
const val = snap.val()
const value = val ? val : { }
dispatch(listenFulfilled(metaType, value))
})
.catch(error => {
dispatch(listenRejected(metaType, error))
})
})
}
}
export function removeListener(metaType: MetaType, clearItems: boolean = false) {
return (dispatch: ListenToPathActions => void, getState: () => Object) => {
return removeListenerRef(getState(), metaType).then(() => {
dispatch(listenRemoved(metaType, clearItems))
})
}
}
export function removeAllListeners() {
return (dispatch: ListenToPathActions => void, getState: () => Object) => {
const promises = Object.keys(metaTypes).map((metaType) => {
return removeListenerRef(getState(), metaType).then(() => {
dispatch(listenRemoved(metaType, true))
})
})
return Promise.all(promises)
}
}
export function listenToMessages() {
return listenToPath(metaTypes.messages, 'messages')
}
export function listenToUserContacts(uid: string) {
return listenToPath(metaTypes.userContacts, `users/${uid}/contacts`)
}
export function removeMessage(messageId: string) {
return removeItem(metaTypes.removeMessage, `messages/${messageId}`)
}
export function updateMessage(messageId: string, text: string) {
const update = {
[`messages/${messageId}/text`]: text
}
return updateItem(metaTypes.updateMessage, update, '')
}