Skip to content

Commit

Permalink
fix(Subject): Limit Subject interface public accessibility
Browse files Browse the repository at this point in the history
BREAKING CHANGE: if there's code change state of Subject via publicly exposed properties, it won't

work anymore
  • Loading branch information
kwonoj committed Jan 1, 2017
1 parent 6922b16 commit 9357d8a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
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

0 comments on commit 9357d8a

Please sign in to comment.