-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateSagas.html
304 lines (215 loc) · 12.1 KB
/
createSagas.html
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<!DOCTYPE html>
<html>
<head>
<title>createSagas.js</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" media="all" href="public/stylesheets/normalize.css" />
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div class="container">
<div class="page">
<div class="header">
<h1>createSagas.js</h1>
<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li>
<a class="source" href="appStateFactory.html">
appStateFactory.js
</a>
</li>
<li>
<a class="source" href="createAppDispatcher.html">
createAppDispatcher.js
</a>
</li>
<li>
<a class="source" href="StateWithSideEffects.html">
StateWithSideEffects.js
</a>
</li>
<li>
<a class="source" href="createChannels.html">
createChannels.js
</a>
</li>
<li>
<a class="source" href="createSagas.html">
createSagas.js
</a>
</li>
<li>
<a class="source" href="sagaInterfaceFactory.html">
sagaInterfaceFactory.js
</a>
</li>
<li>
<a class="source" href="Container.html">
Container.js
</a>
</li>
<li>
<a class="source" href="createContainer.html">
createContainer.js
</a>
</li>
<li>
<a class="source" href="createDangerouslySlowContainer.html">
createDangerouslySlowContainer.js
</a>
</li>
<li>
<a class="source" href="index.html">
index.js
</a>
</li>
<li>
<a class="source" href="ContainerHelpers.html">
ContainerHelpers.js
</a>
</li>
<li>
<a class="source" href="assert.html">
assert.js
</a>
</li>
<li>
<a class="source" href="cast.html">
cast.js
</a>
</li>
<li>
<a class="source" href="checkUnique.html">
checkUnique.js
</a>
</li>
<li>
<a class="source" href="isObservable.html">
isObservable.js
</a>
</li>
<li>
<a class="source" href="uuid.html">
uuid.js
</a>
</li>
<li>
<a class="source" href="ReduxSaga.html">
ReduxSaga.js
</a>
</li>
<li>
<a class="source" href="createReduxReducers.html">
createReduxReducers.js
</a>
</li>
<li>
<a class="source" href="reduxChannelName.html">
reduxChannelName.js
</a>
</li>
<li>
<a class="source" href="reduxMiddlewareFactory.html">
reduxMiddlewareFactory.js
</a>
</li>
<li>
<a class="source" href="rfluxMessage.html">
rfluxMessage.js
</a>
</li>
<li>
<a class="source" href="kefirEmitter.html">
kefirEmitter.js
</a>
</li>
</ol>
</div>
</div>
<div class='highlight'><pre><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'../internal/assert'</span>
<span class="hljs-keyword">import</span> checkUnique <span class="hljs-keyword">from</span> <span class="hljs-string">'../internal/checkUnique'</span>
<span class="hljs-keyword">import</span> {bindActionFunctions} <span class="hljs-keyword">from</span> <span class="hljs-string">'./createChannels'</span>
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">bindSagaHandler</span>(<span class="hljs-params">channel, sagaName, sagaHandler</span>) </span>{
<span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-params">AppDispatcher</span> =></span>
AppDispatcher
.filter(<span class="hljs-function"><span class="hljs-params">x</span> =></span> x.channel === channel && x.actionType === sagaName)
.flatMap(<span class="hljs-function"><span class="hljs-params">x</span> =></span> sagaHandler(x.payload))
.map(<span class="hljs-function"><span class="hljs-params">result</span> =></span> {</pre></div>
<p>emit the result back to the app dispatcher for time travel.</p>
<div class='highlight'><pre> setTimeout(<span class="hljs-function"><span class="hljs-params">()</span> =></span> AppDispatcher.emit({channel, <span class="hljs-attr">actionType</span>: <span class="hljs-string">`<span class="hljs-subst">${sagaName}</span>Result`</span>, <span class="hljs-attr">payload</span>: result}), <span class="hljs-number">0</span>)
<span class="hljs-keyword">return</span> result
})
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_bindSagaHandlers</span>(<span class="hljs-params">channel, ActionTypes, SagaHandlers</span>) </span>{
<span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-params">AppDispatcher</span> =></span>
<span class="hljs-built_in">Object</span>.keys(ActionTypes).reduce(
<span class="hljs-function">(<span class="hljs-params">observables, saga</span>) =></span> {
<span class="hljs-keyword">const</span> handler = SagaHandlers[saga]
<span class="hljs-keyword">const</span> observable = bindSagaHandler(channel, saga, handler)(AppDispatcher)
<span class="hljs-keyword">return</span> <span class="hljs-built_in">Object</span>.assign(observables, {[saga]: observable})
},
{}
)
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_bindSagaResultObservables</span>(<span class="hljs-params">sagas</span>) </span>{
<span class="hljs-keyword">return</span> <span class="hljs-built_in">Object</span>.keys(sagas).reduce(
<span class="hljs-function">(<span class="hljs-params">observables, saga</span>) =></span> <span class="hljs-built_in">Object</span>.assign(observables, {[<span class="hljs-string">`<span class="hljs-subst">${saga}</span>ResultObservable`</span>]: sagas[saga]}),
{}
)
}
<span class="hljs-comment">/**
* Sagas are primarily used for handling ajax workflows.
*
* @param {Object} opts
* @param {string} opts.channel - the name of the saga collection
* @param {Map<string,*>} opts.ActionTypes - the names of the action types (aka side
* effects) these sagas handle
* @param {Map<ActionType,Function>} opts.SagaActionFunctions - (optional) map of action
* functions indexed by ActionType. Like a channel's action functions, these functions
* can be used to initiate a saga. Note that if you include one action function, then
* every ActionType must have a corresponding action function.
* @param {Function} opts.SagaHandlersFn - higher order function with signature
* `({...sagaInterface})=>SagaHandlers` that accepts the `sagas` interface object and
* returns the SagaHandlers. The SagaHandlers are a map of functions indexed by
* ActionType i.e, Map<ActionType,Function>.
* @return {Saga} higher order function that creates the saga.
* @private
*/</span>
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_createSagas</span>(<span class="hljs-params">{channel, ActionTypes, SagaActionFunctions = {}, SagaHandlersFn}</span>) </span>{
assert(<span class="hljs-keyword">typeof</span> channel === <span class="hljs-string">'string'</span>, <span class="hljs-string">'Needs a channel and it needs to be a string'</span>)
assert(ActionTypes, <span class="hljs-string">'Need ActionTypes'</span>)
assert(SagaHandlersFn, <span class="hljs-string">'Need SagaHandlersFn'</span>)
assert(<span class="hljs-keyword">typeof</span> SagaHandlersFn === <span class="hljs-string">'function'</span>, <span class="hljs-string">'SagaHandlersFn should be a higher order function'</span>)
<span class="hljs-keyword">return</span> <span class="hljs-function">(<span class="hljs-params">{AppDispatcher, sagaInterface}</span>) =></span> {
<span class="hljs-comment">/* eslint-disable new-cap */</span>
<span class="hljs-keyword">const</span> SagaHandlers = SagaHandlersFn({...sagaInterface})
<span class="hljs-comment">/* eslint-enable */</span></pre></div>
<p>every side effect must map to an action function and handler</p>
<div class='highlight'><pre> <span class="hljs-built_in">Object</span>.keys(ActionTypes).forEach(<span class="hljs-function"><span class="hljs-params">action</span> =></span> {
<span class="hljs-keyword">if</span> (SagaActionFunctions) {
assert(
SagaActionFunctions[action],
<span class="hljs-string">`Channel <span class="hljs-subst">${channel}</span> is missing side effect action function "<span class="hljs-subst">${action}</span>"`</span>
)
}
assert(SagaHandlers[action], <span class="hljs-string">`Channel <span class="hljs-subst">${channel}</span> is missing side effect handler "<span class="hljs-subst">${action}</span>"`</span>)
})
<span class="hljs-keyword">const</span> observables = _bindSagaHandlers(channel, ActionTypes, SagaHandlers)(AppDispatcher)
<span class="hljs-keyword">return</span> {
<span class="hljs-attr">name</span>: channel,
observables,
<span class="hljs-attr">actionFunctions</span>: bindActionFunctions(ActionTypes, SagaActionFunctions)(AppDispatcher),
<span class="hljs-attr">resultObservables</span>: _bindSagaResultObservables(observables)
}
}
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createSagas</span>(<span class="hljs-params">{rawSagas, ...args}</span>) </span>{
checkUnique(rawSagas, <span class="hljs-string">'channel'</span>, <span class="hljs-string">'Cannot have two sagas with the same name'</span>)
<span class="hljs-keyword">return</span> rawSagas.map(<span class="hljs-function"><span class="hljs-params">s</span> =></span> _createSagas(s)({...args}))
}</pre></div>
<div class="fleur">h</div>
</div>
</div>
</body>
</html>