Skip to content
This repository was archived by the owner on Oct 1, 2018. It is now read-only.

Commit 895ccaa

Browse files
authored
Merge branch 'master' into docs-operator-mergemap
2 parents c3b189d + 6a90120 commit 895ccaa

40 files changed

+1007
-230
lines changed

DOCUMENTATION_GUIDELINES.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Documentation Guidelines
2+
3+
This document is a work in progress and contains the _current_ guidelines that should be followed when working on documentation.
4+
5+
This document is _not_ a holy scripture and we're always open for discussion.
6+
However when an agreement is found, you should follow this document to keep the documentation consistent.
7+
8+
If you're not sure, contact us on our Slack Channel (see [#4](https://github.com/ReactiveX/rxjs-docs/issues/4) or [#24](https://github.com/ReactiveX/rxjs-docs/issues/24) for more information on that).
9+
10+
## TL;DR
11+
>_Sorry, but you will need take your time reading the document if you want to know more about the guidelines._
12+
13+
This document talks about the [structure of the operator documentation](#documentation-format), [the method signature](#method-signature) and the guidelines in our [code examples](#code-examples).
14+
15+
## Documentation Format
16+
The documentation is created in a ts file under src/operator-docs.
17+
Normally the file for the operator should already have been created.
18+
If that's not the case, recheck all subfolders/categories to be sure (it's easy to overlook with all of those operators).
19+
If you still cannot find the operator, create the file in the category that most fits the use case.
20+
21+
The ts file contains an object that implements the __OperatorDoc__ interface. All properties are optional:
22+
23+
- __name__: the name of your operator
24+
- __operatorType__: any one of these strings 'combination'
25+
| 'conditional'
26+
| 'creation'
27+
| 'error handling'
28+
| 'filtering'
29+
| 'multicasting'
30+
| 'transformation'
31+
| 'utility'
32+
- __signature__: the signature of the operator method (without using generics)
33+
- __useInteractiveMarbles__: currently not used, but should be true for an interactive marble diagram
34+
- __marbleUrl__: the url for a marble diagram (use the existing image url found on http://reactivex.io/rxjs for a static diagram)
35+
- __parameters__: OperatorParameters[]. Every OperatorParameters implementation takes
36+
- the _name_ of the parameter
37+
- the _type_ (string, number, Observable,...)
38+
- the _attribute_ (optional,...)
39+
- the _description_ explaining what the parameter is used for
40+
- __shortDescription__: {
41+
description: string;
42+
extras?: OperatorExtra[]
43+
}
44+
- [HTML] the short _description_ of what the operator does (a TL;DR text)
45+
- _extras_ containing a type of 'Tip'|'Warning' and some text for it. It will show up with a little icon to make the type of extra clear.
46+
- __walkthrough__: {
47+
description: string;
48+
extras?: OperatorExtra[]
49+
}
50+
- [HTML] the long and well explained _description_ of what the operator does (the technically correct description and explanation)
51+
- _extras_ containing a type of 'Tip'|'Warning' and some text for it. It will show up with a little icon to make the type of extra clear.
52+
- __examples__: one or more examples demonstrating the use of the operator
53+
- _name_: Text explaining what the example code does
54+
- _code_: The code used in your example. This code will get copied if the user clicks on the copy icon above the code example.
55+
- _externalLink_: an object containing a type of 'JSBin'|'JSFiddle' and the url towards a working example. (Talks are underway to also support Stackblitz in the near future)
56+
- __additionalResources__: OperatorReference[]
57+
- _url_ towards an additional resource explaining the operator (like a blog post, youtube video,...)
58+
- _description_ used as text for the link
59+
- _author_: the author of the resource (currently not used/shown)
60+
- __relatedOperators__: names of other operators related to the current one
61+
62+
## Method signature
63+
The signature of the method should contain the correct name, parameters and return type of the method.
64+
We don't want to show any generics (like <T>) in the signature, because in most cases it does not add any value.
65+
See [#196](https://github.com/ReactiveX/rxjs-docs/pull/196#discussion_r155799478) for the discussion on this topic.
66+
67+
## Code examples
68+
In the example code we want to make use of ES6/TS imports as well as the pipeable operators.
69+
See [#196](https://github.com/ReactiveX/rxjs-docs/pull/196#discussion_r157232289) for a discussion on this topic.
70+
However, neither jsbin nor jsfiddle support this format. When the project gets support from Stackblitz, the newer syntax should be available on that platform and is of course preferred.
71+
72+
Currently to have the best of both worlds, try to follow this guideline:
73+
- The code created directly in the ts file (which will get copied when you click on the icon) should follow the ES6/TS imports and pipeable operators.
74+
- The code in the running example on jsbin/jsfiddle should work, so in other words, use the older syntax.
75+
76+
The example code should also contain a comment which shows the expected output on the console.
77+
This comment should be put after/below the subscribe call on the observable.
78+
79+
In our examples we are currently not making use of the finnish notation ($ suffix). To be consistent with the other examples, neither should you.
80+
81+
Here's an example for both the inline code as well as one on jsbin:
82+
83+
__INLINE__
84+
```javascript
85+
import { fromEvent } from 'rxjs/observable/fromEvent';
86+
import { bufferCount } from 'rxjs/operators';
87+
88+
const clicks = fromEvent(document, 'click', e => ({x: e.clientX, y: e.clientY}));
89+
const buffered = clicks.pipe(
90+
bufferCount(2)
91+
);
92+
buffered.subscribe(x => console.log(x));
93+
94+
/*
95+
Example console output:
96+
97+
[[object Object] {
98+
x: 235,
99+
y: 140
100+
}, [object Object] {
101+
x: 63,
102+
y: 45
103+
}]
104+
105+
[[object Object] {
106+
x: 199,
107+
y: 74
108+
}, [object Object] {
109+
x: 133,
110+
y: 181
111+
}]
112+
113+
[[object Object] {
114+
x: 343,
115+
y: 174
116+
}, [object Object] {
117+
x: 274,
118+
y: 82
119+
}]
120+
*/
121+
122+
```
123+
124+
__JSBIN__
125+
```javascript
126+
const clicks = Rx.Observable.fromEvent(document, 'click', e => ({x: e.clientX, y: e.clientY}));
127+
const buffered = clicks.bufferCount(2);
128+
buffered.subscribe(x => console.log(x));
129+
/*
130+
Example console output:
131+
132+
[[object Object] {
133+
x: 235,
134+
y: 140
135+
}, [object Object] {
136+
x: 63,
137+
y: 45
138+
}]
139+
140+
[[object Object] {
141+
x: 199,
142+
y: 74
143+
}, [object Object] {
144+
x: 133,
145+
y: 181
146+
}]
147+
148+
[[object Object] {
149+
x: 343,
150+
y: 174
151+
}, [object Object] {
152+
x: 274,
153+
y: 82
154+
}]
155+
*/
156+
157+
```
158+
159+
160+

src/operator-docs/combination/combineAll.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,19 @@ export const combineAll: OperatorDoc = {
5151
name:
5252
'Map two click events to a finite interval Observable, then apply <span class="markdown-code">combineAll</span>',
5353
code: `
54-
const clicks = Rx.Observable.fromEvent(document, 'click');
55-
const higherOrder = clicks.map(ev =>
56-
Rx.Observable.interval(Math.random()*2000).take(3)
57-
)
58-
.take(2);
59-
const result = higherOrder.combineAll();
54+
import { map, combineAll, take } from 'rxjs/operators';
55+
import { fromEvent } from 'rxjs/observable/fromEvent';
56+
57+
const clicks = fromEvent(document, 'click');
58+
const higherOrder = clicks.pipe(
59+
map(ev =>
60+
interval(Math.random()*2000).pipe(take(3))
61+
),
62+
take(2)
63+
);
64+
const result = higherOrder.pipe(
65+
combineAll()
66+
);
6067
result.subscribe(x => console.log(x));
6168
`,
6269
externalLink: {

src/operator-docs/combination/combineLatest.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,14 @@ export const combineLatest: OperatorDoc = {
5656
name:
5757
'Dynamically calculate the Body-Mass Index from an Observable of weight and one for height',
5858
code: `
59-
const weight = Rx.Observable.of(70, 72, 76, 79, 75);
60-
const height = Rx.Observable.of(1.76, 1.77, 1.78);
61-
const bmi = weight.combineLatest(height, (w, h) => w / (h * h));
59+
import { combineLatest } from 'rxjs/operators;
60+
import { of } from 'rxjs/observable/of';
61+
62+
const weight = of(70, 72, 76, 79, 75);
63+
const height = of(1.76, 1.77, 1.78);
64+
const bmi = weight.pipe(
65+
combineLatest(height, (w, h) => w / (h * h))
66+
);
6267
/*
6368
Output:
6469
BMI is 24.212293388429753

src/operator-docs/combination/concat.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,14 @@ export const concat: OperatorDoc = {
7878
name:
7979
'Concatenate a timer counting from 0 to 3 with a synchronous sequence from 1 to 10',
8080
code: `
81-
const timer = Rx.Observable.interval(1000).take(4);
82-
const sequence = Rx.Observable.range(1, 10);
83-
const result = Rx.Observable.concat(timer, sequence);
81+
import { take } from 'rxjs/operators';
82+
import { interval } from 'rxjs/observable/interval';
83+
import { range } from 'rxjs/observable/range';
84+
import { concat } from 'rxjs/observable/concat';
85+
86+
const timer = interval(1000).pipe(take(4));
87+
const sequence = range(1, 10);
88+
const result = concat(timer, sequence);
8489
result.subscribe(x => console.log(x));
8590
8691
// results in:
@@ -94,10 +99,13 @@ export const concat: OperatorDoc = {
9499
{
95100
name: 'Concatenate an array of 3 Observables',
96101
code: `
97-
const timer1 = Rx.Observable.interval(1000).take(10);
98-
const timer2 = Rx.Observable.interval(2000).take(6);
99-
const timer3 = Rx.Observable.interval(500).take(10);
100-
const result = timer1.concat(timer2, timer3);
102+
import { take, concat } from 'rxjs/operators';
103+
import { interval } from 'rxjs/observable/interval';
104+
105+
const timer1 = interval(1000).pipe(take(10));
106+
const timer2 = interval(2000).pipe(take(6));
107+
const timer3 = interval(500).pipe(take(10));
108+
const result = timer1.pipe(concat(timer2, timer3));
101109
result.subscribe(x => console.log(x));
102110
103111
// results in the following:

src/operator-docs/combination/concatAll.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,15 @@ export const concatAll: OperatorDoc = {
4242
name:
4343
'For each click event, tick every second from 0 to 3, with no concurrency',
4444
code: `
45-
const clicks = Rx.Observable.fromEvent(document, 'click');
46-
const higherOrder = clicks.map(ev => Rx.Observable.interval(1000).take(4));
47-
const firstOrder = higherOrder.concatAll();
45+
import { map, take, concatAll } from 'rxjs/operators';
46+
import { fromEvent } from 'rxjs/observable/fromEvent';
47+
import { interval } from 'rxjs/observable/interval';
48+
49+
const clicks = fromEvent(document, 'click');
50+
const higherOrder = clicks.pipe(
51+
map(ev => interval(1000).pipe(take(4)))
52+
);
53+
const firstOrder = higherOrder.pipe(concatAll());
4854
firstOrder.subscribe(x => console.log(x));
4955
`,
5056
externalLink: {

src/operator-docs/combination/forkJoin.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,12 @@ export const forkJoin: OperatorDoc = {
7373
{
7474
name: 'Use forkJoin with operator emitting immediately',
7575
code: `
76-
const observable = Rx.Observable.forkJoin(
77-
Rx.Observable.of(1, 2, 3, 4),
78-
Rx.Observable.of(5, 6, 7, 8)
76+
import { forkJoin } from 'rxjs/observable/forkJoin';
77+
import { of } from 'rxjs/observable/of';
78+
79+
const observable = forkJoin(
80+
of(1, 2, 3, 4),
81+
of(5, 6, 7, 8)
7982
);
8083
observable.subscribe(
8184
value => console.log(value),
@@ -94,9 +97,13 @@ export const forkJoin: OperatorDoc = {
9497
{
9598
name: 'Use forkJoin with operator emitting after some time',
9699
code: `
97-
const observable = Rx.Observable.forkJoin(
98-
Rx.Observable.interval(1000).take(3), // emit 0, 1, 2 every second and complete
99-
Rx.Observable.interval(500).take(4) // emit 0, 1, 2, 3 every half a second and complete
100+
import { take } from 'rxjs/operators';
101+
import { forkJoin } from 'rxjs/observable/forkJoin';
102+
import { interval } from 'rxjs/observable/interval';
103+
104+
const observable = forkJoin(
105+
interval(1000).pipe(take(3)), // emit 0, 1, 2 every second and complete
106+
interval(500).pipe(take(4)) // emit 0, 1, 2, 3 every half a second and complete
100107
);
101108
observable.subscribe(
102109
value => console.log(value),

src/operator-docs/combination/merge.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,13 @@ export const merge: OperatorDoc = {
4646
{
4747
name: 'Merge together two Observables: 1s interval and clicks',
4848
code: `
49-
const clicks = Rx.Observable.fromEvent(document, 'click');
50-
const timer = Rx.Observable.interval(1000);
51-
const clicksOrTimer = clicks.merge(timer);
49+
import { merge } from 'rxjs/operators';
50+
import { fromEvent } from 'rxjs/observable/fromEvent';
51+
import { interval } from 'rxjs/observable/interval';
52+
53+
const clicks = fromEvent(document, 'click');
54+
const timer = interval(1000);
55+
const clicksOrTimer = clicks.pipe(merge(timer));
5256
clicksOrTimer.subscribe(x => console.log(x));
5357
`,
5458
externalLink: {
@@ -59,11 +63,15 @@ export const merge: OperatorDoc = {
5963
{
6064
name: 'Merge together 3 Observables, but only 2 run concurrently',
6165
code: `
62-
const timer1 = Rx.Observable.interval(1000).take(10);
63-
const timer2 = Rx.Observable.interval(2000).take(6);
64-
const timer3 = Rx.Observable.interval(500).take(10);
66+
import { take } from 'rxjs/operators';
67+
import { merge } from 'rxjs/observable/merge';
68+
import { interval } from 'rxjs/observable/interval';
69+
70+
const timer1 = interval(1000).pipe(take(10));
71+
const timer2 = interval(2000).pipe(take(6));
72+
const timer3 = interval(500).pipe(take(10));
6573
const concurrent = 2; // the argument
66-
const merged = timer1.merge(timer2, timer3, concurrent);
74+
const merged = timer1.pipe(merge(timer2, timer3, concurrent));
6775
merged.subscribe(x => console.log(x));
6876
`,
6977
externalLink: {

src/operator-docs/combination/mergeAll.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ export const mergeAll: OperatorDoc = {
3838
name:
3939
'Spawn a new interval Observable for each click event, and blend their outputs as one Observable',
4040
code: `
41-
const clicks = Rx.Observable.fromEvent(document, 'click');
42-
const higherOrder = clicks.map((ev) => Rx.Observable.interval(1000));
43-
const firstOrder = higherOrder.mergeAll();
41+
import { mergeAll, map } from 'rxjs/operators';
42+
import { fromEvent } from 'rxjs/observable/fromEvent';
43+
import { interval } from 'rxjs/observable/interval';
44+
45+
const clicks = fromEvent(document, 'click');
46+
const higherOrder = clicks.pipe(map((ev) => interval(1000)));
47+
const firstOrder = higherOrder.pipe(mergeAll());
4448
firstOrder.subscribe(x => console.log(x));
4549
`,
4650
externalLink: {
@@ -52,9 +56,15 @@ export const mergeAll: OperatorDoc = {
5256
name:
5357
'Count from 0 to 9 every second for each click, but only allow 2 concurrent timers',
5458
code: `
55-
const clicks = Rx.Observable.fromEvent(document, 'click');
56-
const higherOrder = clicks.map((ev) => Rx.Observable.interval(1000).take(10));
57-
const firstOrder = higherOrder.mergeAll(2);
59+
import { mergeAll, map } from 'rxjs/operators';
60+
import { fromEvent } from 'rxjs/observable/fromEvent';
61+
import { interval } from 'rxjs/observable/interval';
62+
63+
const clicks = fromEvent(document, 'click');
64+
const higherOrder = clicks.pipe(
65+
map((ev) => interval(1000).pipe(take(10)))
66+
);
67+
const firstOrder = higherOrder.pipe(mergeAll(2));
5868
firstOrder.subscribe(x => console.log(x));
5969
`,
6070
externalLink: {

src/operator-docs/combination/pairwise.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,20 @@ export const pairwise: OperatorDoc = {
3030
name:
3131
'On every click (starting from the second), emit the relative distance to the previous click',
3232
code: `
33-
const clicks = Rx.Observable.fromEvent(document, 'click');
34-
const pairs = clicks.pairwise();
35-
const distance = pairs.map(pair => {
36-
const x0 = pair[0].clientX;
37-
const y0 = pair[0].clientY;
38-
const x1 = pair[1].clientX;
39-
const y1 = pair[1].clientY;
40-
return Math.sqrt(Math.pow(x0 - x1, 2) + Math.pow(y0 - y1, 2));
41-
});
33+
import { pairwise, map } from 'rxjs/operators';
34+
import { fromEvent } from 'rxjs/observable/fromEvent';
35+
36+
const clicks = fromEvent(document, 'click');
37+
const pairs = clicks.pipe(pairwise());
38+
const distance = pairs.pipe(
39+
map(pair => {
40+
const x0 = pair[0].clientX;
41+
const y0 = pair[0].clientY;
42+
const x1 = pair[1].clientX;
43+
const y1 = pair[1].clientY;
44+
return Math.sqrt(Math.pow(x0 - x1, 2) + Math.pow(y0 - y1, 2));
45+
})
46+
);
4247
distance.subscribe(x => console.log(x));
4348
`,
4449
externalLink: {

0 commit comments

Comments
 (0)