Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update operator imports in examples #6678

Merged
56 changes: 27 additions & 29 deletions src/internal/Observable.ts
@@ -1,6 +1,3 @@
/**
* @prettier
*/
import { Operator } from './Operator';
import { SafeSubscriber, Subscriber } from './Subscriber';
import { isSubscription, Subscription } from './Subscription';
Expand Down Expand Up @@ -150,10 +147,10 @@ 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 ({@link deprecations/subscribe-arguments deprecated})
Expand All @@ -173,10 +170,10 @@ 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
Expand All @@ -202,14 +199,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 @@ -272,27 +269,28 @@ export class Observable<T> implements Subscribable<T> {
* 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 @@ -435,7 +433,7 @@ export class Observable<T> implements Subscribable<T> {
* 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
31 changes: 24 additions & 7 deletions src/internal/ajax/ajax.ts
Expand Up @@ -19,9 +19,9 @@ export interface AjaxCreationMethod {
* import { map, catchError, of } from 'rxjs';
*
* const obs$ = ajax({
* method: 'GET',
* url: `https://api.github.com/users?per_page=5`,
* responseType: 'json',
* method: 'GET',
* url: 'https://api.github.com/users?per_page=5',
* responseType: 'json'
* }).pipe(
* map(userResponse => console.log('users: ', userResponse)),
* catchError(error => {
Expand All @@ -44,7 +44,7 @@ export interface AjaxCreationMethod {
* import { ajax } from 'rxjs/ajax';
* import { map, catchError, of } from 'rxjs';
*
* const obs$ = ajax(`https://api.github.com/users?per_page=5`).pipe(
* const obs$ = ajax('https://api.github.com/users?per_page=5').pipe(
* map(userResponse => console.log('users: ', userResponse)),
* catchError(error => {
* console.log('error: ', error);
Expand Down Expand Up @@ -181,13 +181,18 @@ function ajaxGetJSON<T>(url: string, headers?: Record<string, string>): Observab
* import { ajax } from 'rxjs/ajax';
* import { map, catchError, of } from 'rxjs';
*
* const obs$ = ajax(`https://api.github.com/users?per_page=5`).pipe(
* const obs$ = ajax('https://api.github.com/users?per_page=5').pipe(
* map(userResponse => console.log('users: ', userResponse)),
* catchError(error => {
* console.log('error: ', error);
* return of(error);
* })
* );
*
* obs$.subscribe({
* next: value => console.log(value),
* error: err => console.log(err)
* });
* ```
*
* Using `ajax.getJSON()` to fetch data from API
Expand All @@ -196,14 +201,18 @@ function ajaxGetJSON<T>(url: string, headers?: Record<string, string>): Observab
* import { ajax } from 'rxjs/ajax';
* import { map, catchError, of } from 'rxjs';
*
* const obs$ = ajax.getJSON(`https://api.github.com/users?per_page=5`).pipe(
* const obs$ = ajax.getJSON('https://api.github.com/users?per_page=5').pipe(
* map(userResponse => console.log('users: ', userResponse)),
* catchError(error => {
* console.log('error: ', error);
* return of(error);
* })
* );
*
* obs$.subscribe({
* next: value => console.log(value),
* error: err => console.log(err)
* });
* ```
*
* Using `ajax()` with object as argument and method POST with a two seconds delay
Expand All @@ -230,6 +239,10 @@ function ajaxGetJSON<T>(url: string, headers?: Record<string, string>): Observab
* })
* );
*
* users.subscribe({
* next: value => console.log(value),
* error: err => console.log(err)
* });
* ```
*
* Using `ajax()` to fetch. An error object that is being returned from the request
Expand All @@ -238,14 +251,18 @@ function ajaxGetJSON<T>(url: string, headers?: Record<string, string>): Observab
* import { ajax } from 'rxjs/ajax';
* import { map, catchError, of } from 'rxjs';
*
* const obs$ = ajax(`https://api.github.com/404`).pipe(
* const obs$ = ajax('https://api.github.com/404').pipe(
* map(userResponse => console.log('users: ', userResponse)),
* catchError(error => {
* console.log('error: ', error);
* return of(error);
* })
* );
*
* obs$.subscribe({
* next: value => console.log(value),
* error: err => console.log(err)
* });
* ```
*/
export const ajax: AjaxCreationMethod = (() => {
Expand Down
4 changes: 2 additions & 2 deletions src/internal/firstValueFrom.ts
Expand Up @@ -39,13 +39,13 @@ export function firstValueFrom<T>(source: Observable<T>): Promise<T>;
* async function execute() {
* const source$ = interval(2000);
* const firstNumber = await firstValueFrom(source$);
* console.log(`The first number is ${firstNumber}`);
* console.log(`The first number is ${ firstNumber }`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I personally never type the spaces here, and I've never worked with anyone who did. haha.

* }
*
* execute();
*
* // Expected output:
* // "The first number is 0"
* // 'The first number is 0'
* ```
*
* @see {@link lastValueFrom}
Expand Down
4 changes: 2 additions & 2 deletions src/internal/lastValueFrom.ts
Expand Up @@ -37,13 +37,13 @@ export function lastValueFrom<T>(source: Observable<T>): Promise<T>;
* async function execute() {
* const source$ = interval(2000).pipe(take(10));
* const finalNumber = await lastValueFrom(source$);
* console.log(`The final number is ${finalNumber}`);
* console.log(`The final number is ${ finalNumber }`);
* }
*
* execute();
*
* // Expected output:
* // "The final number is 9"
* // 'The final number is 9'
* ```
*
* @see {@link firstValueFrom}
Expand Down
12 changes: 6 additions & 6 deletions src/internal/observable/combineLatest.ts
Expand Up @@ -140,10 +140,10 @@ export function combineLatest<T extends Record<string, ObservableInput<any>>>(
* const combined = combineLatest(observables);
* combined.subscribe(value => console.log(value));
* // Logs
* // {a: 0, b: 0, c: 0} immediately
* // {a: 1, b: 0, c: 0} after 1s
* // {a: 1, b: 5, c: 0} after 5s
* // {a: 1, b: 5, c: 10} after 10s
* // { a: 0, b: 0, c: 0 } immediately
* // { a: 1, b: 0, c: 0 } after 1s
* // { a: 1, b: 5, c: 0 } after 5s
* // { a: 1, b: 5, c: 10 } after 10s
* ```
*
* Combine an array of Observables
Expand All @@ -153,8 +153,8 @@ export function combineLatest<T extends Record<string, ObservableInput<any>>>(
*
* const observables = [1, 5, 10].map(
* n => of(n).pipe(
* delay(n * 1000), // emit 0 and then emit n after n seconds
* startWith(0),
* delay(n * 1000), // emit 0 and then emit n after n seconds
* startWith(0)
* )
* );
* const combined = combineLatest(observables);
Expand Down
17 changes: 6 additions & 11 deletions src/internal/observable/concat.ts
Expand Up @@ -89,18 +89,17 @@ export function concat<T extends readonly unknown[]>(
* const timer = interval(1000).pipe(take(2));
*
* concat(timer, timer) // concatenating the same Observable!
* .subscribe(
* value => console.log(value),
* err => {},
* () => console.log('...and it is done!')
* );
* .subscribe({
* next: value => console.log(value),
* complete: () => console.log('...and it is done!')
* });
*
* // Logs:
* // 0 after 1s
* // 1 after 2s
* // 0 after 3s
* // 1 after 4s
* // "...and it is done!" also after 4s
* // '...and it is done!' also after 4s
* ```
*
* @see {@link concatAll}
Expand All @@ -109,11 +108,7 @@ export function concat<T extends readonly unknown[]>(
* @see {@link startWith}
* @see {@link endWith}
*
* @param input1 An input Observable to concatenate with others.
* @param input2 An input Observable to concatenate with others.
* More than one input Observables may be given as argument.
* @param scheduler An optional {@link SchedulerLike} to schedule each
* Observable subscription on.
* @param args Input Observables to concatenate.
*/
export function concat(...args: any[]): Observable<unknown> {
return concatAll()(from(args, popScheduler(args)));
Expand Down
2 changes: 1 addition & 1 deletion src/internal/observable/defer.ts
Expand Up @@ -27,7 +27,7 @@ import { innerFrom } from './innerFrom';
* ```ts
* import { defer, fromEvent, interval } from 'rxjs';
*
* const clicksOrInterval = defer(function () {
* const clicksOrInterval = defer(() => {
* return Math.random() > 0.5
* ? fromEvent(document, 'click')
* : interval(1000);
Expand Down