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 1 commit
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
25 changes: 25 additions & 0 deletions spec/Subject-spec.ts
Expand Up @@ -430,6 +430,31 @@ describe('Subject', () => {
done();
});

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

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

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

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

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

expect(subject.isObeserved()).to.equal(true);
sub1.unsubscribe();
expect(subject.isObeserved()).to.equal(true);
sub2.unsubscribe();
expect(subject.isObeserved()).to.equal(false);
subject.unsubscribe();
expect(subject.isObeserved()).to.equal(false);
done();
bbarry marked this conversation as resolved.
Show resolved Hide resolved
});

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!;
}

isObeserved() {
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