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

fix(Subject): Limit Subject interface public accessibility #2233

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/ReplaySubject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class ReplaySubject<T> extends Subject<T> {
} else if (this.isStopped) {
subscription = Subscription.EMPTY;
} else {
this.observers.push(subscriber);
this._observers.push(subscriber);
subscription = new SubjectSubscription(this, subscriber);
}

Expand Down
36 changes: 22 additions & 14 deletions src/Subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,23 @@ export class Subject<T> extends Observable<T> implements ISubscription {
return new SubjectSubscriber(this);
}

observers: Observer<T>[] = [];

closed = false;
protected _observers: Array<Observer<T>> = [];
public get observers(): ReadonlyArray<Observer<T>> {
return this._observers;
}

isStopped = false;
protected _closed: boolean = false;
public get closed(): boolean {
return this._closed;
}

hasError = false;
protected _isStopped: boolean = false;
public get isStopped(): boolean {
return this._isStopped;
}

thrownError: any = null;
protected hasError: boolean = false;
protected thrownError: any = null;

constructor() {
super();
Expand Down Expand Up @@ -69,34 +77,34 @@ export class Subject<T> extends Observable<T> implements ISubscription {
}
this.hasError = true;
this.thrownError = err;
this.isStopped = true;
this._isStopped = true;
const { observers } = this;
const len = observers.length;
const copy = observers.slice();
for (let i = 0; i < len; i++) {
copy[i].error(err);
}
this.observers.length = 0;
this._observers.length = 0;
}

complete() {
if (this.closed) {
throw new ObjectUnsubscribedError();
}
this.isStopped = true;
this._isStopped = true;
const { observers } = this;
const len = observers.length;
const copy = observers.slice();
for (let i = 0; i < len; i++) {
copy[i].complete();
}
this.observers.length = 0;
this._observers.length = 0;
}

unsubscribe() {
this.isStopped = true;
this.closed = true;
this.observers = null;
this._isStopped = true;
this._closed = true;
this._observers = null;
}

protected _subscribe(subscriber: Subscriber<T>): Subscription {
Expand All @@ -109,7 +117,7 @@ export class Subject<T> extends Observable<T> implements ISubscription {
subscriber.complete();
return Subscription.EMPTY;
} else {
this.observers.push(subscriber);
this._observers.push(subscriber);
return new SubjectSubscription(this, subscriber);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/SubjectSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class SubjectSubscription<T> extends Subscription {
this.closed = true;

const subject = this.subject;
const observers = subject.observers;
const observers = (subject as any)._observers;

this.subject = null;

Expand Down