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

feat(Subject): add isObserved() api #6405

Merged
merged 4 commits into from May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions api_guard/dist/types/index.d.ts
Expand Up @@ -392,6 +392,7 @@ export declare class Subject<T> extends Observable<T> implements SubscriptionLik
closed: boolean;
hasError: boolean;
isStopped: boolean;
get observed(): boolean;
observers: Observer<T>[];
thrownError: any;
constructor();
Expand Down
24 changes: 24 additions & 0 deletions spec/Subject-spec.ts
Expand Up @@ -430,6 +430,30 @@ describe('Subject', () => {
done();
});

it('should expose observed status', () => {
const subject = new Subject();

expect(subject.observed).to.equal(false);

const sub1 = subject.subscribe(function (x) {
//noop
});

expect(subject.observed).to.equal(true);

const sub2 = subject.subscribe(function (x) {
//noop
});

expect(subject.observed).to.equal(true);
sub1.unsubscribe();
expect(subject.observed).to.equal(true);
sub2.unsubscribe();
expect(subject.observed).to.equal(false);
subject.unsubscribe();
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);
Expand Down
4 changes: 4 additions & 0 deletions src/internal/Subject.ts
Expand Up @@ -91,6 +91,10 @@ export class Subject<T> extends Observable<T> implements SubscriptionLike {
this.observers = null!;
}

get observed() {
return this.observers?.length > 0;
}

bbarry marked this conversation as resolved.
Show resolved Hide resolved
/** @internal */
protected _trySubscribe(subscriber: Subscriber<T>): TeardownLogic {
this._throwIfClosed();
Expand Down