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

Remove Subject.create, refactor WebSocketSubject #7381

Merged
merged 15 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading