-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathepic.js
217 lines (189 loc) · 7.03 KB
/
epic.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
const { combineEpics } = require('redux-observable')
const is = require('typeof-is')
const either = require('ramda/src/either')
const merge = require('ramda/src/merge')
const map = require('ramda/src/map')
const __ = require('ramda/src/__')
const mapObjIndexed = require('ramda/src/mapObjIndexed')
const values = require('ramda/src/values')
const pathEq = require('ramda/src/pathEq')
const propEq = require('ramda/src/propEq')
const find = require('ramda/src/find')
const not = require('ramda/src/not')
const filter = require('ramda/src/filter')
// TODO split into individual modules
const Rx = require('rxjs/Rx')
const createActionTypes = require('./action-types')
const createActionCreators = require('./actions')
module.exports = createEpic
function createEpic (options) {
const {
service
} = options
const actionTypes = createActionTypes(options)
const actionCreators = createActionCreators(options)
const epics = createEpics({ actionTypes, actionCreators, service })
return combineEpics(...values(epics))
}
const requestArgs = {
find: ({ params }) => [params],
get: ({ id, params }) => [id, params],
create: ({ data, params }) => [data, params],
update: ({ id, data, params }) => [id, data, params],
patch: ({ id, data, params }) => [id, data, params],
remove: ({ id, params }) => [id, params]
}
const createRequestHandlers = actions => {
return {
find: (response$, { cid }) => Rx.Observable
.concat(
// set all initial results
response$.first().concatMap(values => Rx.Observable.of(
actions.setAll(cid, values),
actions.ready(cid)
)),
// update the next results as a pairwise diff
response$.pairwise().concatMap(([prev, next]) => {
const removed = getRemoved(prev, next)
const diff = [
actions.unsetAll(cid, removed),
actions.setAll(cid, next)
]
return Rx.Observable.of(...diff)
})
),
get: (response$, { cid, args }) => Rx.Observable
.merge(
response$
.map(value => {
// `feathers-reactive` return null on 'removed' events
return (value === null)
? actions.unset(cid, args.id)
: actions.set(cid, args.id, value)
}),
response$.first().mapTo(actions.ready(cid))
),
create: (response$, { cid, args }) => {
const setOptimistic = actions.set(cid, cid, args.data)
const unsetOptimistic = actions.unset(cid, cid)
const responseAction$ = response$
.take(1)
.concatMap(value => Rx.Observable.of(
actions.set(cid, value.id, value),
actions.ready(cid)
))
.catch(err => Rx.Observable.of(actions.error(cid, err)))
return Rx.Observable.of(setOptimistic)
.concat(responseAction$.startWith(unsetOptimistic))
},
update: createUpdateOrPatchHandler({
method: 'update',
getOptimisticData: ({ args }) => {
return merge({ id: args.id }, args.data)
}
}),
patch: createUpdateOrPatchHandler({
method: 'patch',
getOptimisticData: ({ args, previousData }) => {
return merge(previousData, args.data)
}
}),
remove: (response$, { cid, args, service, store }) => {
const state = store.getState()
const previousData = state[service][args.id]
const setOptimistic = actions.unset(cid, args.id)
const resetOptimistic = actions.set(cid, args.id, previousData)
const responseAction$ = response$
.take(1)
.concatMap(value => Rx.Observable.of(
actions.unset(cid, value.id),
actions.ready(cid)
))
.catch(err => Rx.Observable.of(resetOptimistic, actions.error(cid, err)))
return Rx.Observable.of(setOptimistic)
.concat(responseAction$)
}
}
// TODO: only rollback when _all_ updates for that id have errored
// TODO: find a way to pass in actions for all updates of the same id
function createUpdateOrPatchHandler (options) {
const { method, getOptimisticData } = options
return (response$, { cid, args, service, store }) => {
const state = store.getState()
const previousData = state[service][args.id]
const optimisticData = getOptimisticData({ args, previousData })
const setOptimistic = actions.set(cid, args.id, optimisticData)
const resetOptimistic = actions.set(cid, args.id, previousData)
const responseAction$ = response$
.take(1)
.concatMap(value => Rx.Observable.of(
actions.set(cid, value.id, value),
actions.ready(cid)
))
.catch(err => Rx.Observable.of(resetOptimistic, actions.error(cid, err)))
return Rx.Observable.of(setOptimistic)
.concat(responseAction$)
}
}
}
const createEpics = ({ actionTypes, actionCreators, service }) => {
const isCompleteAction = isType(actionTypes.complete)
const isErrorAction = isType(actionTypes.error)
const isEndAction = either(isCompleteAction, isErrorAction)
const requestHandlers = createRequestHandlers(actionCreators)
const mapRequestHandlers = mapObjIndexed((requestHandler, method) => {
return (action$, store, deps) => {
assertFeathersDep(deps)
const { feathers } = deps
const requester = createRequester({ method, feathers, service })
return action$.ofType(actionTypes[method])
.mergeMap(action => {
const args = action.payload
const { cid } = action.meta
const response$ = requester(args)
const requestAction$ = requestHandler(response$, { cid, args, service, store })
const cidAction$ = action$.filter(isCid(cid))
const completeAction$ = cidAction$.filter(isCompleteAction)
const errorAction$ = cidAction$.filter(isErrorAction)
const cancelAction$ = completeAction$.merge(errorAction$)
return Rx.Observable.of(actionCreators.start(cid, { service, method, args }))
.concat(requestAction$)
.concat(Rx.Observable.of(actionCreators.complete(cid)))
.catch(err => Rx.Observable.of(actionCreators.error(cid, err)))
.takeUntil(cancelAction$)
.filter(endOnce(isEndAction))
})
}
})
return mapRequestHandlers(requestHandlers)
}
const createRequester = ({ feathers, method, service }) => {
const feathersService = feathers.service(service)
return payload => {
const args = requestArgs[method](payload)
return feathersService[method](...args)
}
}
function assertFeathersDep (deps = {}) {
if (is.undefined(deps.feathers)) {
throw new Error('feathers-action/epic: expected feathers app or client to be given as dependency in redux-observable middleware')
}
}
const isCid = pathEq(['meta', 'cid'])
const isType = propEq('type')
const endOnce = (isEndAction) => {
var isDone = false
return (value) => {
if (isEndAction(value)) {
if (isDone) return false
isDone = true
return true
}
return true
}
}
const getRemoved = (prev, next) => {
return filter(value => {
return not(find(propEq('id', value.id), next))
}, prev)
}