Skip to content

Commit

Permalink
docs: update operator imports in examples
Browse files Browse the repository at this point in the history
Closes #6650
  • Loading branch information
jakovljevic-mladen committed Nov 19, 2021
1 parent c2b3e88 commit 57280bc
Show file tree
Hide file tree
Showing 132 changed files with 1,475 additions and 1,395 deletions.
3 changes: 2 additions & 1 deletion .prettierrc.json
Expand Up @@ -2,10 +2,11 @@
"trailingComma": "es5",
"singleQuote": true,
"printWidth": 140,
"arrowParens": "avoid",
"overrides": [{
"files": ["spec/**/*.ts", "spec-dtslint/**/*.ts"],
"options": {
"requirePragma": true
}
}]
}
}
24 changes: 19 additions & 5 deletions README.md
Expand Up @@ -36,15 +36,29 @@ By contributing or commenting on issues in this repository, whether you've read

### ES6 via npm

```sh
```shell
npm install rxjs
```

It's recommended to pull in the Observable creation methods you need directly from `'rxjs'` as shown below with `range`. And you can pull in any operator you need from one spot, under `'rxjs/operators'`.
It's recommended to pull in the Observable creation methods you need directly from `'rxjs'` as shown below with `range`.
If you're using RxJS version 7.2 or above, you can pull in any operator you need from the same spot, `'rxjs'`.

```ts
import { range } from "rxjs";
import { map, filter } from "rxjs/operators";
import { range, filter, map } from 'rxjs';

range(1, 200)
.pipe(
filter(x => x % 2 === 1),
map(x => x + x)
)
.subscribe(x => console.log(x));
```

If you're using RxJS version below 7.2, you can pull in any operator you need from one spot, under `'rxjs/operators'`.

```ts
import { range } from 'rxjs';
import { filter, map } from 'rxjs/operators';

range(1, 200)
.pipe(
Expand All @@ -64,7 +78,7 @@ The global namespace for rxjs is `rxjs`:

```js
const { range } = rxjs;
const { map, filter } = rxjs.operators;
const { filter, map } = rxjs.operators;

range(1, 200)
.pipe(
Expand Down
Expand Up @@ -3,8 +3,8 @@ module.exports = function cleanGeneratedFiles() {
return {
$runAfter: ['writing-files'],
$runBefore: ['writeFilesProcessor'],
$process: function() {
rimraf.sync('src/generated/{*.json}');
}
$process: function () {
rimraf.sync('src/generated/{docs,*.json}');
},
};
};
78 changes: 41 additions & 37 deletions src/internal/Observable.ts
Expand Up @@ -124,8 +124,10 @@ export class Observable<T> implements Subscribable<T> {
* by default emits all its values synchronously. Always check documentation for how given Observable
* will behave when subscribed and if its default behavior can be modified with a `scheduler`.
*
* ## Example
* ### Subscribe with an Observer
* ## Examples
*
* Subscribe with an {@link guide/observer Observer}
*
* ```ts
* import { of } from 'rxjs';
*
Expand All @@ -148,13 +150,14 @@ export class Observable<T> implements Subscribable<T> {
* .subscribe(sumObserver);
*
* // Logs:
* // "Adding: 1"
* // "Adding: 2"
* // "Adding: 3"
* // "Sum equals: 6"
* // 'Adding: 1'
* // 'Adding: 2'
* // 'Adding: 3'
* // 'Sum equals: 6'
* ```
*
* ### Subscribe with functions
* Subscribe with functions ({@link deprecations/subscribe-arguments deprecated})
*
* ```ts
* import { of } from 'rxjs'
*
Expand All @@ -170,13 +173,14 @@ export class Observable<T> implements Subscribable<T> {
* );
*
* // Logs:
* // "Adding: 1"
* // "Adding: 2"
* // "Adding: 3"
* // "Sum equals: 6"
* // 'Adding: 1'
* // 'Adding: 2'
* // 'Adding: 3'
* // 'Sum equals: 6'
* ```
*
* ### Cancel a subscription
* Cancel a subscription
*
* ```ts
* import { interval } from 'rxjs';
*
Expand All @@ -198,14 +202,14 @@ export class Observable<T> implements Subscribable<T> {
* // Logs:
* // 0 after 1s
* // 1 after 2s
* // "unsubscribed!" after 2.5s
* // 'unsubscribed!' after 2.5s
* ```
*
* @param {Observer|Function} observerOrNext (optional) Either an observer with methods to be called,
* or the first of three possible handlers, which is the handler for each value emitted from the subscribed
* Observable.
* or the first of three possible handlers, which is the handler for each value emitted from the subscribed
* Observable.
* @param {Function} error (optional) A handler for a terminal event resulting from an error. If no error handler is provided,
* the error will be thrown asynchronously as unhandled.
* the error will be thrown asynchronously as unhandled.
* @param {Function} complete (optional) A handler for a terminal event resulting from successful completion.
* @return {Subscription} a subscription reference to the registered handlers
* @method subscribe
Expand Down Expand Up @@ -260,36 +264,36 @@ export class Observable<T> implements Subscribable<T> {
* this situation, look into adding something like {@link timeout}, {@link take},
* {@link takeWhile}, or {@link takeUntil} amongst others.
*
* ### Example:
* ## Example
*
* ```ts
* import { interval } from 'rxjs';
* import { take } from 'rxjs/operators';
* import { interval, take } from 'rxjs';
*
* const source$ = interval(1000).pipe(take(4));
*
* async function getTotal() {
* let total = 0;
* let total = 0;
*
* await source$.forEach(value => {
* total += value;
* console.log('observable -> ', value);
* });
* await source$.forEach(value => {
* total += value;
* console.log('observable -> ' + value);
* });
*
* return total;
* return total;
* }
*
* getTotal().then(
* total => console.log('Total:', total)
* )
* total => console.log('Total: ' + total)
* );
*
* // Expected:
* // "observable -> 0"
* // "observable -> 1"
* // "observable -> 2"
* // "observable -> 3"
* // "Total: 6"
* // 'observable -> 0'
* // 'observable -> 1'
* // 'observable -> 2'
* // 'observable -> 3'
* // 'Total: 6'
* ```
*
* @param next a handler for each value emitted by the observable
* @return a promise that either resolves on observable completion or
* rejects with the handled error
Expand Down Expand Up @@ -317,7 +321,7 @@ export class Observable<T> implements Subscribable<T> {
// accessing subscription below in the closure due to Temporal Dead Zone.
let subscription: Subscription;
subscription = this.subscribe(
(value) => {
value => {
try {
next(value);
} catch (err) {
Expand Down Expand Up @@ -421,18 +425,18 @@ export class Observable<T> implements Subscribable<T> {
* @return {Observable} the Observable result of all of the operators having
* been called in the order they were passed in.
*
* ### Example
* ## Example
*
* ```ts
* import { interval } from 'rxjs';
* import { map, filter, scan } from 'rxjs/operators';
* import { interval, filter, map, scan } from 'rxjs';
*
* interval(1000)
* .pipe(
* filter(x => x % 2 === 0),
* map(x => x + x),
* scan((acc, x) => acc + x)
* )
* .subscribe(x => console.log(x))
* .subscribe(x => console.log(x));
* ```
*/
pipe(...operations: OperatorFunction<any, any>[]): Observable<any> {
Expand Down

0 comments on commit 57280bc

Please sign in to comment.