Skip to content

Commit 41888ef

Browse files
BioPhotonniklas-wortmann
authored andcommitted
docs(examples): change js examples to ts (ReactiveX#4635)
* (docs): Fix wrong format for examples changed to right code format and fixed wrong imports. related ReactiveX#4621 * (docs): Fix wrong format for examples changed to right code format. related ReactiveX#4621 * (docs): Fix wrong format for examples and refactored to ts changed to right code format and replaced some `var` declarations with `const` as well as removes references to `Rx` and added new imports related ReactiveX#4621 * fix typos * fix typo * fix typo * fix typo * fix typo
1 parent c27c5e2 commit 41888ef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+264
-237
lines changed

doc/installation.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ npm install rxjs@beta
66

77
Import just the parts you need and use them
88

9-
```js
9+
```ts
1010
import { of, fromEvent } from 'rxjs';
1111
import { map, filter } from 'rxjs/operators';
1212

@@ -29,7 +29,7 @@ npm install rxjs@beta
2929

3030
Usage is pretty much the same thing, only with require:
3131

32-
```js
32+
```ts
3333
const { of, fromEvent } = require('rxjs');
3434
const { map, filter } = require('rxjs/operators');
3535

@@ -48,7 +48,7 @@ fromEvent(input, 'input').pipe(
4848
You can use a CDN (shown below), if you like. In this case, everything is in the same location as it would be in the ESM or CJS versions, but they're namespaced like `rxjs` or `rxjs.operators` instead of `rxjs` and `rxjs/operators`.
4949

5050

51-
```js
51+
```ts
5252
const { of, fromEvent } = rxjs;
5353
const { map, filter } = rxjs.operators;
5454

doc/introduction.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ The essential concepts in RxJS which solve async event management are:
1919

2020
Normally you register event listeners.
2121

22-
```js
22+
```ts
2323
const button = document.querySelector('button');
2424

2525
button.addEventListener('click', () => console.log('Clicked!'));
2626
```
2727

2828
Using RxJS you create an observable instead.
2929

30-
```js
30+
```ts
3131
import { fromEvent } from 'rxjs';
3232

3333
const button = document.querySelector('button');
@@ -42,7 +42,7 @@ What makes RxJS powerful is its ability to produce values using pure functions.
4242

4343
Normally you would create an impure function, where other pieces of your code can mess up your state.
4444

45-
```js
45+
```ts
4646
const button = document.querySelector('button');
4747
let count = 0;
4848

@@ -72,7 +72,7 @@ RxJS has a whole range of operators that helps you control how the events flow t
7272

7373
This is how you would allow at most one click per second, with plain JavaScript:
7474

75-
```js
75+
```ts
7676
const button = document.querySelector('button');
7777
const rate = 1000;
7878
let count = 0;
@@ -88,7 +88,7 @@ button.addEventListener('click', () => {
8888

8989
With RxJS:
9090

91-
```js
91+
```ts
9292
import { fromEvent } from 'rxjs';
9393
import { throttleTime, scan } from 'rxjs/operators';
9494

@@ -110,7 +110,7 @@ You can transform the values passed through your observables.
110110

111111
Here's how you can add the current mouse x position for every click, in plain JavaScript:
112112

113-
```js
113+
```ts
114114
const button = document.querySelector('button');
115115
const rate = 1000;
116116
let count = 0;
@@ -127,7 +127,7 @@ button.addEventListener('click', (event) => {
127127

128128
With RxJS:
129129

130-
```js
130+
```ts
131131
import { fromEvent } from 'rxjs';
132132
import { throttleTime, map, scan } from 'rxjs/operators';
133133

doc/observable.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ This happens because both functions and Observables are lazy computations. If yo
148148

149149
Some people claim that Observables are asynchronous. That is not true. If you surround a function call with logs, like this:
150150

151-
```js
151+
```ts
152152
console.log('before');
153153
console.log(foo.call());
154154
console.log('after');
@@ -165,7 +165,7 @@ You will see the output:
165165

166166
And this is the same behavior with Observables:
167167

168-
```js
168+
```ts
169169
console.log('before');
170170
foo.subscribe(x => {
171171
console.log(x);
@@ -188,7 +188,7 @@ Which proves the subscription of `foo` was entirely synchronous, just like a fun
188188

189189
What is the difference between an Observable and a function? **Observables can "return" multiple values over time**, something which functions cannot. You can't do this:
190190

191-
```js
191+
```ts
192192
function foo() {
193193
console.log('Hello');
194194
return 42;
@@ -404,7 +404,7 @@ Each Observable must define how to dispose resources of that execution when we c
404404

405405
For instance, this is how we clear an interval execution set with `setInterval`:
406406

407-
```js
407+
```ts
408408
const observable = new Observable(function subscribe(subscriber) {
409409
// Keep track of the interval resource
410410
const intervalId = setInterval(() => {
@@ -420,7 +420,7 @@ const observable = new Observable(function subscribe(subscriber) {
420420

421421
Just like `observable.subscribe` resembles `new Observable(function subscribe() {...})`, the `unsubscribe` we return from `subscribe` is conceptually equal to `subscription.unsubscribe`. In fact, if we remove the ReactiveX types surrounding these concepts, we're left with rather straightforward JavaScript.
422422

423-
```js
423+
```ts
424424
function subscribe(subscriber) {
425425
const intervalId = setInterval(() => {
426426
subscriber.next('hi');

doc/observer.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
**What is an Observer?** An Observer is a consumer of values delivered by an Observable. Observers are simply a set of callbacks, one for each type of notification delivered by the Observable: `next`, `error`, and `complete`. The following is an example of a typical Observer object:
44

5-
```js
6-
var observer = {
5+
```ts
6+
const observer = {
77
next: x => console.log('Observer got a next value: ' + x),
88
error: err => console.error('Observer got an error: ' + err),
99
complete: () => console.log('Observer got a complete notification'),
@@ -13,7 +13,7 @@ var observer = {
1313
To use the Observer, provide it to the `subscribe` of an Observable:
1414

1515
<!-- skip-example -->
16-
```js
16+
```ts
1717
observable.subscribe(observer);
1818
```
1919

@@ -23,8 +23,8 @@ Observers in RxJS may also be *partial*. If you don't provide one of the callbac
2323

2424
The example below is an Observer without the `complete` callback:
2525

26-
```js
27-
var observer = {
26+
```ts
27+
const observer = {
2828
next: x => console.log('Observer got a next value: ' + x),
2929
error: err => console.error('Observer got an error: ' + err),
3030
};
@@ -33,14 +33,14 @@ var observer = {
3333
When subscribing to an Observable, you may also just provide the callbacks as arguments, without being attached to an Observer object, for instance like this:
3434

3535
<!-- skip-example -->
36-
```js
36+
```ts
3737
observable.subscribe(x => console.log('Observer got a next value: ' + x));
3838
```
3939

4040
Internally in `observable.subscribe`, it will create an Observer object using the first callback argument as the `next` handler. All three types of callbacks may be provided as arguments:
4141

4242
<!-- skip-example -->
43-
```js
43+
```ts
4444
observable.subscribe(
4545
x => console.log('Observer got a next value: ' + x),
4646
err => console.error('Observer got an error: ' + err),

doc/operator-creation.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ any way the developer sees fit, but here are some guidelines:
3030
### Example
3131

3232
<!-- skip-example -->
33-
```js
33+
```ts
3434
function mySimpleOperator(someCallback) {
35-
// We *could* do a `var self = this;` here to close over, but see next comment
35+
// We *could* do a `const self = this;` here to close over, but see next comment
3636
return Observable.create(subscriber => {
3737
// because we're in an arrow function `this` is from the outer scope.
38-
var source = this;
38+
const source = this;
3939

4040
// save our inner subscription
41-
var subscription = source.subscribe(value => {
41+
const subscription = source.subscribe(value => {
4242
// important: catch errors from user-provided callbacks
4343
try {
4444
subscriber.next(someCallback(value));
@@ -64,14 +64,14 @@ There are a few ways to do this. It's really down to needs and preference:
6464
1) Use the ES7 function bind operator (`::`) available in transpilers like [BabelJS](http://babeljs.io):
6565

6666
<!-- skip-example -->
67-
```js
67+
```ts
6868
someObservable::mySimpleOperator(x => x + '!');
6969
```
7070

7171
2) Create your own Observable subclass and override `lift` to return it:
7272

7373
<!-- skip-example -->
74-
```js
74+
```ts
7575
class MyObservable extends Observable {
7676
lift(operator) {
7777
const observable = new MyObservable(); //<-- important part here
@@ -93,7 +93,7 @@ MyObservable.prototype.mySimpleOperator = mySimpleOperator;
9393
3) Patch `Observable.prototype` directly:
9494

9595
<!-- skip-example -->
96-
```js
96+
```ts
9797
Observable.prototype.mySimpleOperator = mySimpleOperator;
9898

9999
// ... and later .../
@@ -108,12 +108,12 @@ If you don't want to patch the Observable prototype, you can also write the oper
108108
Example implementation:
109109

110110
<!-- skip-example -->
111-
```js
111+
```ts
112112
function mySimpleOperator(someCallback) {
113113
// notice that we return a function here
114114
return function mySimpleOperatorImplementation(source) {
115115
return Observable.create(subscriber => {
116-
var subscription = source.subscribe(value => {
116+
const subscription = source.subscribe(value => {
117117
try {
118118
subscriber.next(someCallback(value));
119119
} catch(err) {
@@ -132,7 +132,7 @@ function mySimpleOperator(someCallback) {
132132
This can now be used with the `pipe()` method on the Observable:
133133

134134
<!-- skip-example -->
135-
```js
135+
```ts
136136
const obs = someObservable.pipe(mySimpleOperator(x => x + '!'));
137137
```
138138

doc/operators.md

+24-16
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ Operators are **methods** on the Observable type, such as `.map(...)`, `.filter(
1010

1111
An Operator is essentially a pure function which takes one Observable as input and generates another Observable as output. Subscribing to the output Observable will also subscribe to the input Observable. In the following example, we create a custom operator function that multiplies each value received from the input Observable by 10:
1212

13-
```js
13+
```ts
14+
import {Observable, from} from 'rxjs';
15+
1416
function multiplyByTen(input) {
15-
var output = Rx.Observable.create(function subscribe(observer) {
17+
const output = Observable.create(function subscribe(observer) {
1618
input.subscribe({
1719
next: (v) => observer.next(10 * v),
1820
error: (err) => observer.error(err),
@@ -22,8 +24,8 @@ function multiplyByTen(input) {
2224
return output;
2325
}
2426

25-
var input = Rx.Observable.from([1, 2, 3, 4]);
26-
var output = multiplyByTen(input);
27+
const input = from([1, 2, 3, 4]);
28+
const output = multiplyByTen(input);
2729
output.subscribe(x => console.log(x));
2830
```
2931

@@ -42,10 +44,12 @@ Notice that a subscribe to `output` will cause `input` Observable to be subscrib
4244

4345
**What is an instance operator?** Typically when referring to operators, we assume *instance* operators, which are methods on Observable instances. For instance, if the operator `multiplyByTen` would be an official instance operator, it would look roughly like this:
4446

45-
```js
46-
Rx.Observable.prototype.multiplyByTen = function multiplyByTen() {
47-
var input = this;
48-
return Rx.Observable.create(function subscribe(observer) {
47+
```ts
48+
import {Observable} from 'rxjs';
49+
50+
Observable.prototype.multiplyByTen = function multiplyByTen() {
51+
const input = this;
52+
return Observable.create(function subscribe(observer) {
4953
input.subscribe({
5054
next: (v) => observer.next(10 * v),
5155
error: (err) => observer.error(err),
@@ -59,8 +63,9 @@ Rx.Observable.prototype.multiplyByTen = function multiplyByTen() {
5963

6064
Notice how the `input` Observable is not a function argument anymore, it is assumed to be the `this` object. This is how we would use such instance operator:
6165

62-
```js
63-
var observable = Rx.Observable.from([1, 2, 3, 4]).multiplyByTen();
66+
```ts
67+
import {Observable, from} from 'rxjs';
68+
const observable = from([1, 2, 3, 4]).multiplyByTen();
6469

6570
observable.subscribe(x => console.log(x));
6671
```
@@ -73,19 +78,22 @@ The most common type of static operators are the so-called *Creation Operators*.
7378

7479
A typical example of a static creation operator would be the `interval` function. It takes a number (not an Observable) as input argument, and produces an Observable as output:
7580

76-
```js
77-
var observable = Rx.Observable.interval(1000 /* number of milliseconds */);
81+
```ts
82+
import {interval} from 'rxjs';
83+
84+
const observable = interval(1000 /* number of milliseconds */);
7885
```
7986

8087
Another example of a creation operator is `create`, which we have been using extensively in previous examples. See the list of [all static creation operators here](#creation-operators).
8188

8289
However, static operators may be of different nature than simply creation. Some *Combination Operators* may be static, such as `merge`, `combineLatest`, `concat`, etc. These make sense as static operators because they take *multiple* Observables as input, not just one, for instance:
8390

84-
```js
85-
var observable1 = Rx.Observable.interval(1000);
86-
var observable2 = Rx.Observable.interval(400);
91+
```ts
92+
import {interval, merge} from 'rxjs';
93+
const observable1 = interval(1000);
94+
const observable2 = interval(400);
8795

88-
var merged = Rx.Observable.merge(observable1, observable2);
96+
const merged = merge(observable1, observable2);
8997
```
9098

9199
## Marble diagrams

doc/pipeable-operators.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ module.exports = {
170170
More complete configuration (closer to a real-world scenario):
171171

172172
<!-- skip-example -->
173-
```js
173+
```ts
174174
const webpack = require('webpack');
175175
const path = require('path');
176176
const HtmlWebpackPlugin = require('html-webpack-plugin');
@@ -179,7 +179,7 @@ const nodeEnv = process.env.NODE_ENV || 'development';
179179
const isProd = nodeEnv === 'production';
180180
const rxPaths = require('rxjs/_esm5/path-mapping');
181181

182-
var config = {
182+
const config = {
183183
devtool: isProd ? 'hidden-source-map' : 'cheap-eval-source-map',
184184
context: path.resolve('./src'),
185185
entry: {

doc/subject.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ setTimeout(() => {
315315

316316
The AsyncSubject is a variant where only the last value of the Observable execution is sent to its observers, and only when the execution completes.
317317

318-
```js
318+
```ts
319319
import { AsyncSubject } from 'rxjs';
320320
const subject = new AsyncSubject();
321321

0 commit comments

Comments
 (0)