-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateChannels.html
475 lines (363 loc) · 19.1 KB
/
createChannels.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<!DOCTYPE html>
<html>
<head>
<title>createChannels.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>createChannels.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> cast <span class="hljs-keyword">from</span> <span class="hljs-string">'../internal/cast'</span>
<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> StateWithSideEffects <span class="hljs-keyword">from</span> <span class="hljs-string">'./StateWithSideEffects'</span>
<span class="hljs-keyword">import</span> {state} <span class="hljs-keyword">from</span> <span class="hljs-string">'./StateWithSideEffects'</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_bindActionFunctionToAppDispatcher</span>(<span class="hljs-params">actionFunction</span>) </span>{
<span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-params">AppDispatcher</span> =></span>
(...args) => AppDispatcher.emit({...actionFunction(...args)})
}
<span class="hljs-comment">/**
* Takes a map of ActionFunctions indexed by ActionType and binds each to the
* AppDispatcher. That is, when a bound function is called, it automatically
* dispatches its message to the channel.
*
* **Note:**
* 1. This method is actually a higher order function. It returns a function
* that accepts the AppDispatcher object as a parameter. This way, the
* AppDispatcher is not hard-coded dependency.
*
* @param {Map<string,boolean>} ActionTypes
* @param {Map<ActionType,Function>} ActionFunctions
* @returns {Function} a function that binds the action functions to the app dispatcher
* @private
*/</span>
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">bindActionFunctions</span>(<span class="hljs-params">ActionTypes, ActionFunctions</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">channelActions, action</span>) =></span> ({
...channelActions,
[action]: _bindActionFunctionToAppDispatcher(ActionFunctions[action])(AppDispatcher)
}),
{}
)
}
<span class="hljs-comment">/**
* @deprecated You rarely (really) need a *pre-bound* selector (emphasis:
* "pre-bound"). For this reason, these are deprecated.
*
* Takes a map of ActionObservables *not necessarily indexed by ActionType* and binds each
* to the ChannelStateObservable. The ChannelStateObservable is the channel's state,
* wrapped in a Kefir stream (otherwise known as an *observable*).
*
* Since the ChannelStateObservable represents the state, an ActionObservable is a
* way of observing (aka "selecting") arbitrary parts of the state tree.
*
* **Note:**
* 1. This method is actually a higher order function. It returns a function
* that accepts a ChannelStateObservable object as a parameter. This way, the
* ChannelStateObservable is not hard-coded dependency.
*
* TODO global rename ActionObservable => SelectionObservable
*
* @param {Map<string,Observable>} ActionObservables
* @returns {Function} a function that binds the action observables to the channel observable
* @private
*/</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_bindActionObservables</span>(<span class="hljs-params">ActionObservables</span>) </span>{
<span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-params">channelObservable</span> =></span>
<span class="hljs-built_in">Object</span>.keys(ActionObservables).reduce(
<span class="hljs-function">(<span class="hljs-params">total, observable</span>) =></span> <span class="hljs-built_in">Object</span>.assign(
total,
{[observable]: ActionObservables[observable](channelObservable)}
),
{}
)
}
<span class="hljs-comment">/**
* Creates the channel's state observable using the given channel name.
*
* When an action comes in, it will call the corresponding reducer with the payload,
* and pass the new state to the observable.
*
* Every reducer is called with these parameters:
* 1. the current state
* 2. the action payload
* 3. a `endOfSideEffects` function that can be used to report the end of all the
* side effects.
*
* In addition to updating the state, every reducer can also dispatch side
* effects---which are just messages that are handled by other reducers or sagas. By
* using the result of the `endOfSideEffects` function as the last side effect, it is
* possible to tell when the entire reducer workflow completes... or so that's the
* idea.
*
* Each reducer has this signature:
*
* ```
* (state:ChannelState, payload:Payload, endOfSideEffects:Payload => Message)
* => StateWithSideEffects
* ```
*
* TODO sideEffectResult may not actually fire correctly, specially when side effects
* are handled by async sagas.
*
* **Notes:**
* 1. Every ActionType must have a corresponding Reducer.
* 2. This method is actually a higher order function. It returns a function
* that accepts an AppDispatcher object as a parameter. This way, the
* AppDispatcher is not hard-coded dependency.
*
* @param {string} channel
* @param {Map<ActionType,Function>} Reducers
* @returns {Function} a function that creates the channel's state observable.
* @private
*/</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_createChannelStateObservable</span>(<span class="hljs-params">channel, Reducers</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 && x.channel === channel)
.scan(
<span class="hljs-function">(<span class="hljs-params">stateWithSideEffects, action</span>) =></span> {
<span class="hljs-keyword">const</span> reducer = Reducers[action.actionType]
<span class="hljs-keyword">if</span> (!reducer) {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">`Channel <span class="hljs-subst">${channel}</span> does not support <span class="hljs-subst">${action.actionType}</span>`</span>)
}
<span class="hljs-keyword">const</span> endOfSideEffects = <span class="hljs-function"><span class="hljs-params">payload</span> =></span> ({
<span class="hljs-attr">channel</span>: <span class="hljs-string">`<span class="hljs-subst">${channel}</span>Result`</span>,
<span class="hljs-attr">actionType</span>: <span class="hljs-string">`<span class="hljs-subst">${action.actionType}</span>Result`</span>,
payload
})</pre></div>
<p>always return a StateWithSideEffects (code hardening)</p>
<div class='highlight'><pre> <span class="hljs-keyword">return</span> cast(
reducer(stateWithSideEffects.state, action.payload, endOfSideEffects),
StateWithSideEffects
)
},
state(Reducers.initialState || {})
)
}
<span class="hljs-comment">/**
* The idea is that you can use these observables to observe the end of a reducer +
* side effects.
* @param {string} channel
* @param {Map<string,*>} ActionTypes
* @returns {Function} function that binds AppDispatcher to the observables
* @private
*/</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_createEndOfActionsObservables</span>(<span class="hljs-params">channel, ActionTypes</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, action</span>) =></span> <span class="hljs-built_in">Object</span>.assign(
observables,
{
[<span class="hljs-string">`<span class="hljs-subst">${action}</span>ResultObservable`</span>]:
AppDispatcher
.filter(<span class="hljs-function"><span class="hljs-params">x</span> =></span>
x.channel === <span class="hljs-string">`<span class="hljs-subst">${channel}</span>Result`</span> && x.actionType === <span class="hljs-string">`<span class="hljs-subst">${action}</span>Result`</span>)
.map(<span class="hljs-function"><span class="hljs-params">x</span> =></span> x.payload)
}
),
{}
)
}
<span class="hljs-comment">/* eslint-disable no-console */</span>
<span class="hljs-comment">/**
* The channel consists of
* - the channel name
* - the state observable
* - bound (aka "live") action functions
* - bound state selectors (which will probably be deprecated in a future release)
*
* It is created from a map of the ActionTypes. Each ActionType has a corresponding
* reducer, which handles incoming messages. Each ActionType also has a corresponding
* ActionFunction that's used to dispatch messages.
*
* One catch is that the *names* of the ActionFunctions and the ActionObservables must
* be globally unique. This isn't hard to achieve as long as you:
*
* 1. use the channel name in the action/observable. Ex: createDoc
* 2. use the word "observable" in the observables. Ex: docObservable
*
* @param {Object} opts
* @param {string} opts.channel
* @param {Map<string,*>} opts.ActionTypes - map of action type constants
* @param {Map<ActionType,Function>} opts.Reducers - map of reducers, indexed by
* ActionType. Additionally, reducers have an `initialState` property.
* @param {Map<ActionType,Function>} opts.ActionFunctions - map of action functions,
* indexed by ActionType
* @param {Map<string,Function>} opts.ActionObservables (optional) - higher order
* functions that take the ChannelStateObservable as input and return an observable that
* selects parts of the state tree. **This will probably be deprecated.**
* @returns {Function} that binds the channel to the app dispatcher
* @private
*/</span>
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_createChannel</span>(<span class="hljs-params">
{channel, ActionTypes, Reducers, ActionFunctions, ActionObservables}</span>) </span>{
ActionObservables = ActionObservables || {}
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(Reducers, <span class="hljs-string">'Need Reducers'</span>)
assert(ActionFunctions, <span class="hljs-string">'Need action functions'</span>)</pre></div>
<p>every action must have an action function and a reducer</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> {
assert(
ActionFunctions[action],
<span class="hljs-string">`Channel <span class="hljs-subst">${channel}</span> is missing action function "<span class="hljs-subst">${action}</span>"`</span>
)
assert(Reducers[action], <span class="hljs-string">`Channel <span class="hljs-subst">${channel}</span> is missing reducer "<span class="hljs-subst">${action}</span>"`</span>)
})</pre></div>
<p>need an initial state; otherwise defaults to {}</p>
<div class='highlight'><pre> <span class="hljs-keyword">if</span> (!Reducers.initialState) {
<span class="hljs-built_in">console</span>.warn(<span class="hljs-string">`Channel <span class="hljs-subst">${channel}</span> doesn't have initialState`</span>)
}
<span class="hljs-keyword">return</span> <span class="hljs-function">(<span class="hljs-params">{AppDispatcher}</span>) =></span> {
<span class="hljs-keyword">const</span> stateWithSideEffectsObservable =
_createChannelStateObservable(channel, Reducers)(AppDispatcher)
<span class="hljs-keyword">const</span> stateObservable = stateWithSideEffectsObservable.map(<span class="hljs-function"><span class="hljs-params">x</span> =></span> x.state)
<span class="hljs-keyword">return</span> {
<span class="hljs-attr">name</span>: channel,
stateWithSideEffectsObservable,
<span class="hljs-attr">actions</span>: {
...bindActionFunctions(ActionTypes, ActionFunctions)(AppDispatcher),
},
<span class="hljs-attr">observable</span>: {
[channel]: stateObservable,
},
<span class="hljs-attr">channel</span>: {</pre></div>
<p>@deprecated</p>
<div class='highlight'><pre> ..._bindActionObservables(ActionObservables)(stateObservable),</pre></div>
<p>@deprecated</p>
<div class='highlight'><pre> ..._createEndOfActionsObservables(channel, ActionTypes)(AppDispatcher),
}
}
}
}
<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">createChannels</span>(<span class="hljs-params">{rawChannels, ...args}</span>) </span>{
checkUnique(rawChannels, <span class="hljs-string">'channel'</span>, <span class="hljs-string">'Cannot have two channels with the same name'</span>);
<span class="hljs-keyword">return</span> rawChannels.map(<span class="hljs-function"><span class="hljs-params">s</span> =></span> _createChannel(s)({...args}))
}</pre></div>
<div class="fleur">h</div>
</div>
</div>
</body>
</html>