From d62ce6ebe2b94b0ddf3890053134d5cf11469196 Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Mon, 20 Nov 2023 14:25:12 -0600 Subject: [PATCH] feat(Subject.create): Removed the deprecated `Subject.create` method. BREAKING CHANGE: Removed the deprecated `Subject.create` method. If you need to create an object that is "half Observable, half Observer", you'll need to either bolt `next`, `error`, and `complete` handlers onto an `Observable` and property type the return... or you'll need to create your own class that is backed by an `Observable`. In any case, if the `Observer` and the `Observable` are so unrelated that you have to bolt them together, you're probably better off with those two objects separately. This is why `Subject.create` has been deprecated for so long. --- packages/rxjs/spec/Subject-spec.ts | 126 -------------------------- packages/rxjs/src/internal/Subject.ts | 37 -------- 2 files changed, 163 deletions(-) diff --git a/packages/rxjs/spec/Subject-spec.ts b/packages/rxjs/spec/Subject-spec.ts index 7c6e5b2b5c..38c08869db 100644 --- a/packages/rxjs/spec/Subject-spec.ts +++ b/packages/rxjs/spec/Subject-spec.ts @@ -1,6 +1,5 @@ import { expect } from 'chai'; import { Subject, Observable, AsyncSubject, Observer, of, config, Subscription, Subscriber, noop, operate } from 'rxjs'; -import { AnonymousSubject } from 'rxjs/internal/Subject'; import { delay } from 'rxjs/operators'; import { TestScheduler } from 'rxjs/testing'; import { observableMatcher } from './helpers/observableMatcher'; @@ -448,104 +447,6 @@ describe('Subject', () => { expect(subject.observed).to.equal(false); }); - it('should have a static create function that works', () => { - expect(Subject.create).to.be.a('function'); - const source = of(1, 2, 3, 4, 5); - const nexts: number[] = []; - const output: any[] = []; - - let error: any; - let complete = false; - let outputComplete = false; - - const destination = { - closed: false, - next: function (x: number) { - nexts.push(x); - }, - error: function (err: any) { - error = err; - this.closed = true; - }, - complete: function () { - complete = true; - this.closed = true; - }, - }; - - const sub: Subject = Subject.create(destination, source); - - sub.subscribe({ - next: function (x: number) { - output.push(x); - }, - complete: () => { - outputComplete = true; - }, - }); - - sub.next('a'); - sub.next('b'); - sub.next('c'); - sub.complete(); - - expect(nexts).to.deep.equal(['a', 'b', 'c']); - expect(complete).to.be.true; - expect(error).to.be.a('undefined'); - - expect(output).to.deep.equal([1, 2, 3, 4, 5]); - expect(outputComplete).to.be.true; - }); - - it('should have a static create function that works also to raise errors', () => { - expect(Subject.create).to.be.a('function'); - const source = of(1, 2, 3, 4, 5); - const nexts: number[] = []; - const output: number[] = []; - - let error: any; - let complete = false; - let outputComplete = false; - - const destination = { - closed: false, - next: function (x: number) { - nexts.push(x); - }, - error: function (err: any) { - error = err; - this.closed = true; - }, - complete: function () { - complete = true; - this.closed = true; - }, - }; - - const sub: Subject = Subject.create(destination, source); - - sub.subscribe({ - next: function (x: number) { - output.push(x); - }, - complete: () => { - outputComplete = true; - }, - }); - - sub.next('a'); - sub.next('b'); - sub.next('c'); - sub.error('boom'); - - expect(nexts).to.deep.equal(['a', 'b', 'c']); - expect(complete).to.be.false; - expect(error).to.equal('boom'); - - expect(output).to.deep.equal([1, 2, 3, 4, 5]); - expect(outputComplete).to.be.true; - }); - it('should be an Observer which can be given to Observable.subscribe', (done) => { const source = of(1, 2, 3, 4, 5); const subject = new Subject(); @@ -781,30 +682,3 @@ describe('Subject', () => { expect(results).to.deep.equal([1, 1, 2, 2, 'complete']); }); }); - -describe('AnonymousSubject', () => { - it('should be exposed', () => { - expect(AnonymousSubject).to.be.a('function'); - }); - - it('should not be eager', () => { - let subscribed = false; - - const subject = Subject.create( - null, - new Observable((observer: Observer) => { - subscribed = true; - const subscription = of('x').subscribe(observer); - return () => { - subscription.unsubscribe(); - }; - }) - ); - - const observable = subject.asObservable(); - expect(subscribed).to.be.false; - - observable.subscribe(); - expect(subscribed).to.be.true; - }); -}); diff --git a/packages/rxjs/src/internal/Subject.ts b/packages/rxjs/src/internal/Subject.ts index 90f153bd39..885981e0bc 100644 --- a/packages/rxjs/src/internal/Subject.ts +++ b/packages/rxjs/src/internal/Subject.ts @@ -40,15 +40,6 @@ export class Subject extends Observable implements SubscriptionLike { /** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */ thrownError: any = null; - /** - * Creates a "subject" by basically gluing an observer to an observable. - * - * @deprecated Recommended you do not use. Will be removed at some point in the future. Plans for replacement still under discussion. - */ - static create: (...args: any[]) => any = (destination: Observer, source: Observable): AnonymousSubject => { - return new AnonymousSubject(destination, source); - }; - constructor() { // NOTE: This must be here to obscure Observable's constructor. super(); @@ -146,31 +137,3 @@ export class Subject extends Observable implements SubscriptionLike { return new Observable((subscriber) => this.subscribe(subscriber)); } } - -export class AnonymousSubject extends Subject { - constructor( - /** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */ - public destination?: Observer, - /** @internal */ - protected _source?: Observable - ) { - super(); - } - - next(value: T) { - this.destination?.next?.(value); - } - - error(err: any) { - this.destination?.error?.(err); - } - - complete() { - this.destination?.complete?.(); - } - - /** @internal */ - protected _subscribe(subscriber: Subscriber): Subscription { - return this._source?.subscribe(subscriber) ?? Subscription.EMPTY; - } -}