From 678b6eda134c448a746a5485b28bc4ff7d2b748c Mon Sep 17 00:00:00 2001 From: demensky Date: Thu, 30 Jun 2022 01:28:11 +0300 Subject: [PATCH 1/2] feat(throwError): removed deprecated throwError(error) call pattern BREAKING CHANGE: `throwError(error)` call pattern is no longer available. Use `throwError(() => error)` --- spec-dtslint/observables/throwError-spec.ts | 13 ++++--------- spec/observables/throwError-spec.ts | 2 +- src/internal/observable/throwError.ts | 18 +++--------------- 3 files changed, 8 insertions(+), 25 deletions(-) diff --git a/spec-dtslint/observables/throwError-spec.ts b/spec-dtslint/observables/throwError-spec.ts index 2070a59139..c7a1f375e2 100644 --- a/spec-dtslint/observables/throwError-spec.ts +++ b/spec-dtslint/observables/throwError-spec.ts @@ -1,14 +1,9 @@ import { throwError, animationFrameScheduler } from 'rxjs'; -it('should accept any type and return never observable', () => { - const a = throwError(1); // $ExpectType Observable - const b = throwError('a'); // $ExpectType Observable - const c = throwError({ a: 1 }); // $ExpectType Observable - const d = throwError(() => ({ a: 2 })); // $ExpectType Observable -}); - -it('should support an error value and a scheduler', () => { - const a = throwError(1, animationFrameScheduler); // $ExpectType Observable +it('should error for incorrect errorFactory', () => { + const a = throwError(1); // $ExpectError + const b = throwError('a'); // $ExpectError + const c = throwError({ a: 1 }); // $ExpectError }); it('should accept any type and return never observable with support of factory', () => { diff --git a/spec/observables/throwError-spec.ts b/spec/observables/throwError-spec.ts index 8a590bf0da..940cf679fa 100644 --- a/spec/observables/throwError-spec.ts +++ b/spec/observables/throwError-spec.ts @@ -36,7 +36,7 @@ describe('throwError', () => { it('should accept scheduler', () => { rxTest.run(({ expectObservable }) => { - const e = throwError('error', rxTest); + const e = throwError(() => 'error', rxTest); expectObservable(e).toBe('#'); }); diff --git a/src/internal/observable/throwError.ts b/src/internal/observable/throwError.ts index a307f5ad95..5db4e8aa2e 100644 --- a/src/internal/observable/throwError.ts +++ b/src/internal/observable/throwError.ts @@ -1,7 +1,6 @@ import { Observable } from '../Observable'; import { Subscriber } from '../Subscriber'; import { SchedulerLike } from '../types'; -import { isFunction } from '../util/isFunction'; /** * Creates an observable that will create an error instance and push it to the consumer as an error @@ -97,29 +96,18 @@ import { isFunction } from '../util/isFunction'; */ export function throwError(errorFactory: () => any): Observable; -/** - * Returns an observable that will error with the specified error immediately upon subscription. - * - * @param error The error instance to emit - * @deprecated Support for passing an error value will be removed in v8. Instead, pass a factory function to `throwError(() => new Error('test'))`. This is - * because it will create the error at the moment it should be created and capture a more appropriate stack trace. If - * for some reason you need to create the error ahead of time, you can still do that: `const err = new Error('test'); throwError(() => err);`. - */ -export function throwError(error: any): Observable; - /** * Notifies the consumer of an error using a given scheduler by scheduling it at delay `0` upon subscription. * - * @param errorOrErrorFactory An error instance or error factory + * @param errorFactory An error instance or error factory * @param scheduler A scheduler to use to schedule the error notification * @deprecated The `scheduler` parameter will be removed in v8. * Use `throwError` in combination with {@link observeOn}: `throwError(() => new Error('test')).pipe(observeOn(scheduler));`. * Details: https://rxjs.dev/deprecations/scheduler-argument */ -export function throwError(errorOrErrorFactory: any, scheduler: SchedulerLike): Observable; +export function throwError(errorFactory: () => any, scheduler: SchedulerLike): Observable; -export function throwError(errorOrErrorFactory: any, scheduler?: SchedulerLike): Observable { - const errorFactory = isFunction(errorOrErrorFactory) ? errorOrErrorFactory : () => errorOrErrorFactory; +export function throwError(errorFactory: () => any, scheduler?: SchedulerLike): Observable { const init = (subscriber: Subscriber) => subscriber.error(errorFactory()); return new Observable(scheduler ? (subscriber) => scheduler.schedule(init as any, 0, subscriber) : init); } From 6d03f872474405c302063932f5cf910247d44bd5 Mon Sep 17 00:00:00 2001 From: demensky Date: Thu, 30 Jun 2022 01:36:48 +0300 Subject: [PATCH 2/2] feat(throwError): removed deprecated throwError(errorFactory, scheduler) call pattern BREAKING CHANGE: `throwError(errorFactory, scheduler)` call pattern is no longer available. [Read more](https://rxjs.dev/deprecations/scheduler-argument). --- spec-dtslint/observables/throwError-spec.ts | 6 +----- spec/observables/throwError-spec.ts | 8 -------- src/internal/observable/throwError.ts | 22 ++++----------------- 3 files changed, 5 insertions(+), 31 deletions(-) diff --git a/spec-dtslint/observables/throwError-spec.ts b/spec-dtslint/observables/throwError-spec.ts index c7a1f375e2..45432da32a 100644 --- a/spec-dtslint/observables/throwError-spec.ts +++ b/spec-dtslint/observables/throwError-spec.ts @@ -1,4 +1,4 @@ -import { throwError, animationFrameScheduler } from 'rxjs'; +import { throwError } from 'rxjs'; it('should error for incorrect errorFactory', () => { const a = throwError(1); // $ExpectError @@ -12,7 +12,3 @@ it('should accept any type and return never observable with support of factory', const c = throwError(() => ({ a: 1 })); // $ExpectType Observable const d = throwError(() => ({ a: 2 })); // $ExpectType Observable }); - -it('should support a factory and a scheduler', () => { - const a = throwError(() => 1, animationFrameScheduler); // $ExpectType Observable -}); diff --git a/spec/observables/throwError-spec.ts b/spec/observables/throwError-spec.ts index 940cf679fa..0bcbc899d6 100644 --- a/spec/observables/throwError-spec.ts +++ b/spec/observables/throwError-spec.ts @@ -34,14 +34,6 @@ describe('throwError', () => { }); }); - it('should accept scheduler', () => { - rxTest.run(({ expectObservable }) => { - const e = throwError(() => 'error', rxTest); - - expectObservable(e).toBe('#'); - }); - }); - it('should accept a factory function', () => { let calls = 0; let errors: any[] = []; diff --git a/src/internal/observable/throwError.ts b/src/internal/observable/throwError.ts index 5db4e8aa2e..7753575628 100644 --- a/src/internal/observable/throwError.ts +++ b/src/internal/observable/throwError.ts @@ -1,6 +1,4 @@ import { Observable } from '../Observable'; -import { Subscriber } from '../Subscriber'; -import { SchedulerLike } from '../types'; /** * Creates an observable that will create an error instance and push it to the consumer as an error @@ -94,20 +92,8 @@ import { SchedulerLike } from '../types'; * * @param errorFactory A factory function that will create the error instance that is pushed. */ -export function throwError(errorFactory: () => any): Observable; - -/** - * Notifies the consumer of an error using a given scheduler by scheduling it at delay `0` upon subscription. - * - * @param errorFactory An error instance or error factory - * @param scheduler A scheduler to use to schedule the error notification - * @deprecated The `scheduler` parameter will be removed in v8. - * Use `throwError` in combination with {@link observeOn}: `throwError(() => new Error('test')).pipe(observeOn(scheduler));`. - * Details: https://rxjs.dev/deprecations/scheduler-argument - */ -export function throwError(errorFactory: () => any, scheduler: SchedulerLike): Observable; - -export function throwError(errorFactory: () => any, scheduler?: SchedulerLike): Observable { - const init = (subscriber: Subscriber) => subscriber.error(errorFactory()); - return new Observable(scheduler ? (subscriber) => scheduler.schedule(init as any, 0, subscriber) : init); +export function throwError(errorFactory: () => any): Observable { + return new Observable((subscriber) => { + subscriber.error(errorFactory()); + }); }