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: _chapters/asteroids.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -100,7 +100,7 @@ Let’s say we want a left- or right-arrow keydown event to start the ship rotat
100
100
```
101
101
102
102
So as promised, this function is setting the `transform` property on the ship, using the position and angle information stored in our local `state` object. We compute the new position by deducting or removing 1 (degree) from the angle (for a left or right rotation respectively) and simultaneously update the state object with the new angle.
103
-
Since we specify 10-millisecond delay, the ship will rotate 100 times per second.
103
+
Since we specify 10-millisecond delay, the ship will rotate 100 degrees per second.
104
104
105
105
We’re not done yet. We have to stop the rotation on keyup by calling `clearInterval`, for the specific interval we just created on keydown (using the `handle` we stored). To do this, we’ll use `document.addEventListener` to specify a separate keyup handler for each keydown event, and since we will be creating a new keyup listener for each keydown event, we will also have to cleanup after ourselves or we’ll have a memory (event) leak:
Copy file name to clipboardExpand all lines: _chapters/functionalreactiveprogramming.md
+40-36Lines changed: 40 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ Functional Reactive Programming describes an approach to modelling complex, asyn
20
20
21
21
We will explore FRP through an implementation of the [Observable](#observable-streams) data structure in [the Reactive Extensions for JavaScript (RxJS) library](https://www.learnrxjs.io/). We will then see it applied in application to a straightforward [browser-based user interface problem](#a-user-interface-example).
22
22
23
-
To support the code examples, the streams are visualised using [rxviz](https://rxviz.com/)
23
+
To support the code examples, the streams are visualised using [rxviz](https://rx-viz.vercel.app/).
24
24
25
25
## Observable Streams
26
26
@@ -53,12 +53,13 @@ You could think of our lazy sequences as being “pull-based” data structures,
53
53
Just as we have done for various data structures (arrays and so on) in previous chapters, we can define a transform over an Observable to create a new Observable. This transformation may have multiple steps the same way that we chained `filter` and `map` operations over arrays previously. In RxJS’s Observable implementation, however, they’ve gone a little bit more functional, by insisting that such operations are composed (rather than chained) inside a `pipe`. For example, here’s the squares of even numbers in the range [0,10):
54
54
55
55
```javascript
56
-
constisEven=x=>x%2===0,
57
-
square=x=>x*x
56
+
constisEven=x=>x%2===0,
57
+
square=x=>x*x
58
58
range(10)
59
59
.pipe(
60
60
filter(isEven),
61
-
map(square))
61
+
map(square)
62
+
)
62
63
.subscribe(console.log)
63
64
```
64
65
@@ -87,9 +88,10 @@ To solve the first Project Euler problem using RxJS, we generate a sequence of n
87
88
```javascript
88
89
range(1000)
89
90
.pipe(
90
-
filter(x=> x%3===0|| x%5===0),
91
-
scan((a,v)=>a+v),
92
-
last())
91
+
filter(x=> x%3===0|| x%5===0),
92
+
scan((a,v) => a+v),
93
+
last()
94
+
)
93
95
.subscribe(console.log);
94
96
```
95
97
@@ -125,8 +127,8 @@ By contrast, the `mergeMap` operator gives the *Cartesian product* of two stream
125
127
126
128
```javascript
127
129
columns.pipe(
128
-
mergeMap(column=>rows.pipe(
129
-
map(row=>[column, row])
130
+
mergeMap(column=>rows.pipe(
131
+
map(row=>[column, row])
130
132
))
131
133
).subscribe(console.log)
132
134
```
@@ -147,8 +149,8 @@ If we contrast `mergeMap` and `map`, `map` will produce an Observable of Observa
147
149
148
150
```javascript
149
151
columns.pipe(
150
-
map(column=>rows.pipe(
151
-
map(row=>[column, row])
152
+
map(column=>rows.pipe(
153
+
map(row=>[column, row])
152
154
))
153
155
).subscribe(console.log)
154
156
```
@@ -191,7 +193,7 @@ The following lets us see in the console the keys pressed as they come in, it wi
191
193
192
194
```javascript
193
195
key$.pipe(
194
-
map(e=>e.key)
196
+
map(e=>e.key)
195
197
).subscribe(console.log)
196
198
```
197
199
@@ -203,7 +205,7 @@ The following prints “!!” on every mousedown:
203
205
204
206
```javascript
205
207
mouse$.pipe(
206
-
map(_=>"!!")
208
+
map(_=>"!!")
207
209
).subscribe(console.log)
208
210
```
209
211
@@ -216,8 +218,8 @@ Once again this will keep producing the message for every mouse click for as lon
216
218
The following achieves the same thing with a single subscription using `merge`:
217
219
218
220
```javascript
219
-
merge(key$.pipe(map(e=>e.key)),
220
-
mouse$.pipe(map(_=>"!!"))
221
+
merge(key$.pipe(map(e=>e.key)),
222
+
mouse$.pipe(map(_=>"!!"))
221
223
).subscribe(console.log)
222
224
```
223
225
@@ -266,7 +268,7 @@ Creating new Observable streams from existing streams
@@ -427,38 +429,38 @@ the position of the top-left corner of the rectangle, and (optionally, since it
427
429
428
430
```typescript
429
431
typeState=Readonly<{
430
-
pos:Point,
431
-
offset?:Point
432
+
pos:Point,
433
+
offset?:Point
432
434
}>
433
435
```
434
436
435
437
We’ll introduce some types to model the objects coming through the stream and the effects they have when applied to a `State` object in the `scan`. First, all the events we care about have a position on the SVG canvas associated with them, so we’ll have a simple immutable `Point` interface with `x` and `y` positions and a couple of handy vector math methods (note that these create a new `Point` rather than mutating any existing state within the `Point`):
Now we create a subclass of `Point` with a constructor letting us instantiate it for a given (DOM) `MouseEvent` and an `abstract` (placeholder) definition for a function to apply the correct update action to the `State`:
0 commit comments