Skip to content

Commit

Permalink
feat(Subject.create): Removed the deprecated Subject.create method.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
benlesh committed Nov 22, 2023
1 parent ae005b4 commit d62ce6e
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 163 deletions.
126 changes: 0 additions & 126 deletions packages/rxjs/spec/Subject-spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<any> = 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<any> = 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<number>();
Expand Down Expand Up @@ -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<any>) => {
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;
});
});
37 changes: 0 additions & 37 deletions packages/rxjs/src/internal/Subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,6 @@ export class Subject<T> extends Observable<T> 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 = <T>(destination: Observer<T>, source: Observable<T>): AnonymousSubject<T> => {
return new AnonymousSubject<T>(destination, source);
};

constructor() {
// NOTE: This must be here to obscure Observable's constructor.
super();
Expand Down Expand Up @@ -146,31 +137,3 @@ export class Subject<T> extends Observable<T> implements SubscriptionLike {
return new Observable((subscriber) => this.subscribe(subscriber));
}
}

export class AnonymousSubject<T> extends Subject<T> {
constructor(
/** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */
public destination?: Observer<T>,
/** @internal */
protected _source?: Observable<T>
) {
super();
}

next(value: T) {
this.destination?.next?.(value);
}

error(err: any) {
this.destination?.error?.(err);
}

complete() {
this.destination?.complete?.();
}

/** @internal */
protected _subscribe(subscriber: Subscriber<T>): Subscription {
return this._source?.subscribe(subscriber) ?? Subscription.EMPTY;
}
}

0 comments on commit d62ce6e

Please sign in to comment.