-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
239 lines (222 loc) · 6.09 KB
/
index.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
const flatten = require('callbag-flatten')
const map = require('callbag-map')
const forEach = require('callbag-for-each')
const share = require('callbag-share')
const fromObservable = require('callbag-from-obs')
const fromIterable = require('callbag-from-iter')
const fromEvent = require('callbag-from-event')
const fromPromise = require('callbag-from-promise')
/**
* pipe-me
* --------------
*/
module.exports = {
/**
* fromObservable
* --------------
*
* Convert from Observables in Rx.js, xstream, or anything with a `subscribe` method.
*
* ```js
* import { Observable } from 'rxjs'
* import { fromObservable } from 'pipe-me/create'
*
* const observed = fromObservable(Observable.of([0, 1, 2])
* ```
*/
fromObservable: (...args) => share(fromObservable(...args)),
/**
* fromIterable
* --------------
*
* aka(ish): fromArray & fromGeneratorFunction
*
* Create from Iterable things like generators and arrays
* ```js
* import { Observable } from 'rxjs'
* import { fromIterable } from 'pipe-me/create'
*
* function* generate(i) {
* yield i*2;
* }
* const myArray = [0, 1, 2]
* const fromGenerator = fromIterable(generate(2)) // 4
*
* const fromArray = fromIterable(myArray) // 0
* // 1
* // 2
* ```
*/
fromIterable: (...args) => share(fromIterable(...args)),
/**
* fromEvent
* --------------
*
* Get data from any event listener.
*
* ```js
* import { fromEvent } from 'pipe-me/create'
* const buttonClicked = fromEvent(document, 'click')
* |> filter(event => event.target.tagName === 'BUTTON')
* ```
*/
fromEvent: (...args) => share(fromEvent(...args)),
/**
* fromPromise
* --------------
*
* Create stream from a Promise.
*
* ```js
* import { fromPromise } from 'pipe-me/create'
*
* const promiseResolved = fromPromise(Promise.resolve([0, 1, 2])
* ```
*/
fromPromise: (...args) => share(fromPromise(...args)),
/**
* sideEffect
* --------------
*
* Side effects are reactions to your data flow. Things like actual
* user interface code (`<div></div>`), the browser url bar, network requests,
* LED lights turning on, etc. Create any side effect here.
*
* ![Diagram for map](https://github.com/sartaj/pipe-me/blob/master/docs/assets/diagrams/sideEffect.png?raw=true)
*/
sideEffect: require('callbag-for-each'),
/**
* log
* --------------
*
* Shortcut for sideEffect((data) => { console.log(data) })
*/
log: (res) => forEach(((...args) => console.log(...args)))(res),
/**
* map
* --------------
*
* Whenever you want to transform/change the contents of your stream, you'll use map.
*
* ![Diagram for map](https://github.com/sartaj/pipe-me/blob/master/docs/assets/diagrams/map.png?raw=true)
*
* ***(aka transforming streams)***
*/
map: map,
/**
* accumulate
* --------------
*
* aka(ish): Rx: 'scan' | Arrays: 'reduce'
*
* This allows you to build up data over time.
* ![Diagram for map](https://github.com/sartaj/pipe-me/blob/master/docs/assets/diagrams/accumulate.png?raw=true)
*/
accumulate: require('callbag-scan'),
/**
* flatten
* --------------
*
* Sometimes, the contents of source may be another stream. As in, a stream within a stream.
*
* ![](https://github.com/sartaj/pipe-me/blob/master/docs/assets/memes/stream-in-stream.jpg?raw=true)
*
* `flatten` can make that data the main stream.
*
* ## Example
*
* ```javascript
* const fetchData = fromPromise(callAPI)
*
* // This has fetchData Stream itself, not it's contents.
* const dataStreamRetrieved = userClicked.map(fetchData)
*
* // This actually has the data.
* const dataRetrieved = flatten(dataStreamRetrieved)
* ```
*
* ## In Other Worlds
*
* ### Observables
* `|> map() |> flatten` is another way to do `flatMap`/`switchMap`.
*
* ### Arrays
* `Array.flatten` will flatten arrays within arrays.
*/
flatten: require('callbag-flatten'),
/**
* take
* --------------
*
* A filter to take only what you need.
*
* ```javascript
* // Will take only first 5 clicks
* const userClicked = listenToClick() |> take(5)
* ```
*/
take: require('callbag-take'),
/**
* skip
* --------------
*
* Ignores the first n of the source.
*
* ```javascript
* // Ignore first 5 clicks
* const userClicked = listenToClick() |> skip(5)
* ```
*/
skip: require('callbag-skip'),
/**
* filter
* --------------
*
* Filter data based on truthy/falsy condition
*
* ```javascript
* fromIterable([1,2,3,4,5])
* |> filter(x => x % 2);
* |> log // 1
* // 3
* // 5
* ```
*/
filter: require('callbag-filter'),
/**
* merge
* --------------
*
* Merges data from multiple callbag sources. Designed more for listening sources (like fromEvent)
*
* const userWantsToSubmit = merge(userClickedSubmit, userPressedEnter);
*
* userWantsToSubmit |> log // click event
* // keyboard enter event
* // keyboard enter event
* // click event
* // etc...
*/
merge: require('callbag-merge'),
/**
* concat
* --------------
*
* Combine sources by putting one source after the previous source ends.
*
* ![Diagram for concat](https://github.com/sartaj/pipe-me/blob/master/docs/assets/diagrams/concat.png?raw=true)
*/
concat: require('callbag-concat'),
/**
* combine
* ---------------
*
* Get the latest value from multiple streams as an array.
*
* ```javascript
* const state = combine(uiStream, serverStream, userStream);
* state |> log // [uiState, serverState, userStream]
* ```
*/
combine: require('callbag-combine')
}