You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+42-54Lines changed: 42 additions & 54 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -82,18 +82,18 @@ Bud is a main FRP unit.
82
82
83
83
```js
84
84
/* empty Bud (Nothing) */
85
-
var bud =Bud()
85
+
constbud=Bud()
86
86
87
87
/* Bud with value */
88
-
var bud =Bud('value')
88
+
constbud=Bud('value')
89
89
90
90
/* emit new values */
91
91
bud.emit('value 1').emit('value 2')
92
92
```
93
93
94
94
### derivatives
95
95
96
-
* Create derived streams by using `var new_bud = join(...buds, (...bud_values) => new_value)`.
96
+
* Create derived streams by using `const new_bud = join(...buds, (...bud_values) => new_value)`.
97
97
*`emit`'s are propagated through the dependents.
98
98
* By using `join` we guarantee that every dependency changes at most once for single `emit`. See [atomic updates](#atomic-updates).
99
99
*`join(bud, fn)` is a `map`.
@@ -106,22 +106,22 @@ it's better to do it as an effect (`on`).
106
106
*`bud.map(fn)` is a shortcut for `join` deriving from a single Bud.
107
107
108
108
```js
109
-
var a =Bud()
109
+
consta=Bud()
110
110
111
111
/* derive from `a` (map) */
112
-
var b =join(a, (a) => a +'b')
112
+
constb=join(a, (a) => a +'b')
113
113
114
114
/* derive from both `a` and `b` */
115
-
var c =join(a, b, (a, b) => a + b +'c')
115
+
constc=join(a, b, (a, b) => a + b +'c')
116
116
117
117
/* skip (filter out, reject) values */
118
-
var n =join(a, (a) => Nothing)
118
+
constn=join(a, (a) => Nothing)
119
119
120
120
/* return two values for each input (like transducer) */
121
-
var n =join(a, (a) =>Many(a, a +'x'))
121
+
constn=join(a, (a) =>Many(a, a +'x'))
122
122
123
123
/* derive from single Bud `a` */
124
-
var b =a.map((a) => a +'b')
124
+
constb=a.map((a) => a +'b')
125
125
```
126
126
127
127
### merging
@@ -132,17 +132,17 @@ var b = a.map((a) => a + 'b')
132
132
* In case of multiple simultaneous inputs all of them would be passed down merged stream in the order of inputs from left to right. The resulting value would be the most right one.
133
133
134
134
```js
135
-
var a =Bud()
136
-
var b =Bud()
135
+
consta=Bud()
136
+
constb=Bud()
137
137
138
138
/* merge all from `a` and `b` */
139
-
var c =merge(a, b)
139
+
constc=merge(a, b)
140
140
141
141
/* diamond is also possible */
142
-
var a =Bud()
143
-
var b =join(a, (a) => a +'b')
144
-
var c =join(a, (a) => a +'c')
145
-
var d =merge(b, c)
142
+
consta=Bud()
143
+
constb=join(a, (a) => a +'b')
144
+
constc=join(a, (a) => a +'c')
145
+
constd=merge(b, c)
146
146
```
147
147
148
148
### high-order
@@ -151,10 +151,10 @@ var d = merge(b, c)
151
151
* high-order `thru` transformers good when you can't express transformation in terms of `map`.
@@ -204,14 +201,11 @@ function dom_event (element, eventname)
204
201
}
205
202
206
203
/* create Bud from interval timer */
207
-
functioninterval (ms)
208
-
{
209
-
returnresource(emit=>
210
-
{
211
-
var t =setInterval(emit, ms)
212
-
213
-
returnfunctiondisposer ()
214
-
{
204
+
functioninterval (ms) {
205
+
returnresource((emit) => {
206
+
let t =setInterval(emit, ms)
207
+
208
+
returnfunctiondisposer () {
215
209
if (! t) return
216
210
217
211
clearInterval(t)
@@ -244,21 +238,18 @@ fluh's `map` works in three ways:
244
238
So `map` covers all cases for `map`, `filter` and `flatMap` in a common manner.
245
239
246
240
### high-order transformations
247
-
In practice, `map` covers most of the cases, but there're may be advanced tasks when you need to take a Bud, transform it (for instance, involving state) and return modified Bud: `var new_bud = transform(bud)`.
241
+
In practice, `map` covers most of the cases, but there're may be advanced tasks when you need to take a Bud, transform it (for instance, involving state) and return modified Bud: `const new_bud = transform(bud)`.
248
242
249
243
In order to do this, fluh has `bud.thru(transform)` which accepts function from one Bud to another and returns result of invocation that function on this particular Bud.
250
244
251
245
Here's the example of how it can be used to make Bud async by default (by creating new dependent Bud which receives updates asynchronously):
252
246
253
247
```js
254
-
functiondefer (bud)
255
-
{
256
-
var deferred =bud.constructor()
257
-
258
-
bud.on(value=>
259
-
{
260
-
setTimeout(() =>
261
-
{
248
+
functiondefer (bud) {
249
+
constdeferred=bud.constructor()
250
+
251
+
bud.on((value) => {
252
+
setTimeout(() => {
262
253
deferred.emit(value)
263
254
}
264
255
, 0)
@@ -270,17 +261,15 @@ function defer (bud)
270
261
271
262
Then use it via `thru`:
272
263
```js
273
-
var a =Bud(1)
274
-
var b =a.thru(defer)
264
+
consta=Bud(1)
265
+
constb=a.thru(defer)
275
266
a.emit(2)
276
267
```
277
268
278
269
fluh exposes special helper for easier implementation of high-order transformations, called `lib/trasfer`. In terms of `transfer` previous `defer` example may be implemented in such manner:
279
270
```js
280
-
functiondefer (bud)
281
-
{
282
-
returntransfer(bud, (value, emit) =>
283
-
{
271
+
functiondefer (bud) {
272
+
returntransfer(bud, (value, emit) => {
284
273
setTimeout(() =>emit(value), 0)
285
274
})
286
275
}
@@ -289,12 +278,11 @@ function defer (bud)
289
278
### handling errors
290
279
fluh does not capture throws by default, but you can make any function to do that, by decorating it with `capture`:
291
280
```js
292
-
var a =Bud()
281
+
consta=Bud()
293
282
294
283
import { capture } from'fluh'
295
284
296
-
var b =a.map(capture(x=>
297
-
{
285
+
constb=a.map(capture((x) => {
298
286
/* throws in this function will be captured: */
299
287
/* … throw e … */
300
288
@@ -311,7 +299,7 @@ import { when_data } from './map/when'
311
299
/* `when_data` allows to work with data in pure manner, */
312
300
/* passing past any `Error` instances and `End` */
313
301
/* only real data passed to target function */
314
-
var c =b.map(when_data(b=> b +1))
302
+
constc=b.map(when_data((b)=> b +1))
315
303
```
316
304
317
305
There's no special error channel, use mixed content in combine with helper above if you need to handle errors. If you want a more pure approach, bring your own `Either`/`Result` container.
0 commit comments