-
Notifications
You must be signed in to change notification settings - Fork 6
/
sagas.js
174 lines (159 loc) · 4.77 KB
/
sagas.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
import * as types from './types'
import { metaTypes, eventTypes } from './types'
import * as actions from './actions'
import firebase from './firebase'
import { eventChannel, buffers } from 'redux-saga'
import { put, take, call, fork, cancel, flush } from 'redux-saga/effects'
export function* watchUpdateRequested() {
while (true) {
const action = yield take(types.firebase.FIREBASE_UPDATE_REQUESTED)
let getUpdates = null
switch (action.meta.type) {
case metaTypes.userContacts:
getUpdates = getUserContactsUpdates
break
}
if (typeof getUpdates === 'function') {
const updates = yield call(getUpdates, action.payload)
yield fork(updateItems, updates, action.meta.type)
}
}
}
export function* watchRemoveRequested() {
while (true) {
const action = yield take(types.firebase.FIREBASE_REMOVE_REQUESTED)
let getPath = null
switch (action.meta.type) {
case metaTypes.userContacts:
getPath = getUserContactsPath
break
}
if (typeof getPath === 'function') {
const path = yield call(getPath, action.payload)
yield fork(removeItem, path, action.meta.type)
}
}
}
export function getUserContactsPath({ uid, contactId }) {
return `users/${uid}/contacts/${contactId}`
}
export function getUserContactsUpdates({ uid, contactId, name, phone }) {
return {
[`users/${uid}/contacts/${contactId}/name`]: name,
[`users/${uid}/contacts/${contactId}/phone`]: phone,
}
}
export function* updateItems(updates, metaType) {
try {
const ref = firebase.database().ref()
yield call([ref, ref.update], updates)
yield put(actions.firebaseUpdateFulfilled(metaType))
} catch (error) {
yield put(actions.firebaseUpdateRejected(error, metaType))
}
}
export function* removeItem(path, metaType) {
try {
const ref = firebase.database().ref(path)
yield call([ref, ref.remove])
yield put(actions.firebaseRemoveFulfilled(metaType))
} catch (error) {
yield put(actions.firebaseRemoveRejected(error, metaType))
}
}
export function createEventChannel(ref) {
const listener = eventChannel(emit => {
ref.on('child_added', snap => {
emit({
eventType: eventTypes.CHILD_ADDED,
key: snap.key,
value: snap.val(),
})
})
ref.on('child_changed', snap => {
const val = snap.val()
emit({
eventType: eventTypes.CHILD_CHANGED,
key: snap.key,
value: snap.val(),
})
})
ref.on('child_removed', snap => {
emit({ eventType: eventTypes.CHILD_REMOVED, key: snap.key })
})
return () => {
ref.off()
}
}, buffers.expanding(1))
return listener
}
export function* getDataAndListenToChannel(ref, metaType) {
const chan = yield call(createEventChannel, ref)
try {
try {
const snap = yield call([ref, ref.once], 'value')
yield flush(chan)
const val = snap.val()
const value = val ? val : {}
yield put(actions.firebaseListenFulfilled(value, metaType))
} catch (error) {
yield put(actions.firebaseListenRejected(error, metaType))
}
while (true) {
const data = yield take(chan)
yield put(getUpdateAction(data, metaType))
}
} finally {
chan.close()
}
}
export function getUpdateAction(data, metaType) {
switch (data.eventType) {
case eventTypes.CHILD_ADDED:
return actions.firebaseListenChildAdded(data.key, data.value, metaType)
case eventTypes.CHILD_CHANGED:
return actions.firebaseListenChildChanged(data.key, data.value, metaType)
case eventTypes.CHILD_REMOVED:
return actions.firebaseListenChildRemoved(data.key, metaType)
}
}
export function* watchListener(metaType) {
while (true) {
const listenRequestAction = yield take(
types.firebase.FIREBASE_LISTEN_REQUESTED
)
if (listenRequestAction.meta.type === metaType) {
let task = yield fork(
getDataAndListenToChannel,
listenRequestAction.payload.ref,
metaType
)
while (true) {
const action = yield take([
types.firebase.FIREBASE_REMOVE_LISTENER_REQUESTED,
types.firebase.FIREBASE_LISTEN_REQUESTED,
types.firebase.FIREBASE_REMOVE_ALL_LISTENERS_REQUESTED,
])
if (
action.type ===
types.firebase.FIREBASE_REMOVE_ALL_LISTENERS_REQUESTED ||
action.meta.type === metaType
) {
yield cancel(task)
yield put(
actions.firebaseListenRemoved(!!action.payload.clearItems, metaType)
)
if (action.type === types.firebase.FIREBASE_LISTEN_REQUESTED) {
task = yield fork(
getDataAndListenToChannel,
action.payload.ref,
metaType
)
} else {
break
}
}
}
}
}
}