Skip to content

Commit 51a52f8

Browse files
committed
make code style more generic and appropriate for a common reader.
1 parent f027825 commit 51a52f8

1 file changed

Lines changed: 42 additions & 54 deletions

File tree

readme.md

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,18 @@ Bud is a main FRP unit.
8282

8383
```js
8484
/* empty Bud (Nothing) */
85-
var bud = Bud()
85+
const bud = Bud()
8686

8787
/* Bud with value */
88-
var bud = Bud('value')
88+
const bud = Bud('value')
8989

9090
/* emit new values */
9191
bud.emit('value 1').emit('value 2')
9292
```
9393

9494
### derivatives
9595

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)`.
9797
* `emit`'s are propagated through the dependents.
9898
* By using `join` we guarantee that every dependency changes at most once for single `emit`. See [atomic updates](#atomic-updates).
9999
* `join(bud, fn)` is a `map`.
@@ -106,22 +106,22 @@ it's better to do it as an effect (`on`).
106106
* `bud.map(fn)` is a shortcut for `join` deriving from a single Bud.
107107

108108
```js
109-
var a = Bud()
109+
const a = Bud()
110110

111111
/* derive from `a` (map) */
112-
var b = join(a, (a) => a + 'b')
112+
const b = join(a, (a) => a + 'b')
113113

114114
/* derive from both `a` and `b` */
115-
var c = join(a, b, (a, b) => a + b + 'c')
115+
const c = join(a, b, (a, b) => a + b + 'c')
116116

117117
/* skip (filter out, reject) values */
118-
var n = join(a, (a) => Nothing)
118+
const n = join(a, (a) => Nothing)
119119

120120
/* return two values for each input (like transducer) */
121-
var n = join(a, (a) => Many(a, a + 'x'))
121+
const n = join(a, (a) => Many(a, a + 'x'))
122122

123123
/* derive from single Bud `a` */
124-
var b = a.map((a) => a + 'b')
124+
const b = a.map((a) => a + 'b')
125125
```
126126

127127
### merging
@@ -132,17 +132,17 @@ var b = a.map((a) => a + 'b')
132132
* 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.
133133

134134
```js
135-
var a = Bud()
136-
var b = Bud()
135+
const a = Bud()
136+
const b = Bud()
137137

138138
/* merge all from `a` and `b` */
139-
var c = merge(a, b)
139+
const c = merge(a, b)
140140

141141
/* 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+
const a = Bud()
143+
const b = join(a, (a) => a + 'b')
144+
const c = join(a, (a) => a + 'c')
145+
const d = merge(b, c)
146146
```
147147

148148
### high-order
@@ -151,10 +151,10 @@ var d = merge(b, c)
151151
* high-order `thru` transformers good when you can't express transformation in terms of `map`.
152152

153153
```js
154-
var a = Bud()
154+
const a = Bud()
155155

156156
/* delay must return function from Bud to Bud */
157-
var b = a.thru(delay(50))
157+
const b = a.thru(delay(50))
158158
```
159159

160160
### effects
@@ -167,13 +167,13 @@ var b = a.thru(delay(50))
167167
* `bud.on(fn)` returns disposer function.
168168

169169
```js
170-
var a = Bud()
170+
const a = Bud()
171171

172172
/* subscribe to changes */
173-
var ds = a.on((value) => console.log('a:', value))
173+
const disposer = a.on((value) => console.log('a:', value))
174174

175175
/* disposing of the effect */
176-
ds()
176+
disposer()
177177
```
178178

179179
### resources
@@ -184,14 +184,11 @@ ds()
184184

185185
```js
186186
/* create Bud from DOM Event */
187-
function dom_event (element, eventname)
188-
{
189-
return resource(emit =>
190-
{
187+
function dom_event (element, eventname) {
188+
return resource((emit) => {
191189
element.addEventListener(eventname, emit)
192190

193-
return function disposer ()
194-
{
191+
return function disposer () {
195192
if (! element) return
196193

197194
element.removeEventListener(eventname, emit)
@@ -204,14 +201,11 @@ function dom_event (element, eventname)
204201
}
205202

206203
/* create Bud from interval timer */
207-
function interval (ms)
208-
{
209-
return resource(emit =>
210-
{
211-
var t = setInterval(emit, ms)
212-
213-
return function disposer ()
214-
{
204+
function interval (ms) {
205+
return resource((emit) => {
206+
let t = setInterval(emit, ms)
207+
208+
return function disposer () {
215209
if (! t) return
216210

217211
clearInterval(t)
@@ -244,21 +238,18 @@ fluh's `map` works in three ways:
244238
So `map` covers all cases for `map`, `filter` and `flatMap` in a common manner.
245239

246240
### 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)`.
248242

249243
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.
250244

251245
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):
252246

253247
```js
254-
function defer (bud)
255-
{
256-
var deferred = bud.constructor()
257-
258-
bud.on(value =>
259-
{
260-
setTimeout(() =>
261-
{
248+
function defer (bud) {
249+
const deferred = bud.constructor()
250+
251+
bud.on((value) => {
252+
setTimeout(() => {
262253
deferred.emit(value)
263254
}
264255
, 0)
@@ -270,17 +261,15 @@ function defer (bud)
270261

271262
Then use it via `thru`:
272263
```js
273-
var a = Bud(1)
274-
var b = a.thru(defer)
264+
const a = Bud(1)
265+
const b = a.thru(defer)
275266
a.emit(2)
276267
```
277268

278269
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:
279270
```js
280-
function defer (bud)
281-
{
282-
return transfer(bud, (value, emit) =>
283-
{
271+
function defer (bud) {
272+
return transfer(bud, (value, emit) => {
284273
setTimeout(() => emit(value), 0)
285274
})
286275
}
@@ -289,12 +278,11 @@ function defer (bud)
289278
### handling errors
290279
fluh does not capture throws by default, but you can make any function to do that, by decorating it with `capture`:
291280
```js
292-
var a = Bud()
281+
const a = Bud()
293282

294283
import { capture } from 'fluh'
295284

296-
var b = a.map(capture(x =>
297-
{
285+
const b = a.map(capture((x) => {
298286
/* throws in this function will be captured: */
299287
/* … throw e … */
300288

@@ -311,7 +299,7 @@ import { when_data } from './map/when'
311299
/* `when_data` allows to work with data in pure manner, */
312300
/* passing past any `Error` instances and `End` */
313301
/* only real data passed to target function */
314-
var c = b.map(when_data(b => b + 1))
302+
const c = b.map(when_data((b) => b + 1))
315303
```
316304

317305
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

Comments
 (0)