Skip to content

Commit f47425d

Browse files
bbarrybenlesh
andauthored
feat: All subjects now have an observed property. This will allow users to check whether a subject has current subscribers without us allowing access to the observers array, which is going to be made private in future versions. (#6405)
* feat(Subject): add isObserved() api * fix(Subject): typo, remove done from sync test * feat(subject): change observed method to property * chore: update api_guardian files Co-authored-by: Ben Lesh <[email protected]>
1 parent f7ea211 commit f47425d

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

api_guard/dist/types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ export declare class Subject<T> extends Observable<T> implements SubscriptionLik
392392
closed: boolean;
393393
hasError: boolean;
394394
isStopped: boolean;
395+
get observed(): boolean;
395396
observers: Observer<T>[];
396397
thrownError: any;
397398
constructor();

spec/Subject-spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,30 @@ describe('Subject', () => {
430430
done();
431431
});
432432

433+
it('should expose observed status', () => {
434+
const subject = new Subject();
435+
436+
expect(subject.observed).to.equal(false);
437+
438+
const sub1 = subject.subscribe(function (x) {
439+
//noop
440+
});
441+
442+
expect(subject.observed).to.equal(true);
443+
444+
const sub2 = subject.subscribe(function (x) {
445+
//noop
446+
});
447+
448+
expect(subject.observed).to.equal(true);
449+
sub1.unsubscribe();
450+
expect(subject.observed).to.equal(true);
451+
sub2.unsubscribe();
452+
expect(subject.observed).to.equal(false);
453+
subject.unsubscribe();
454+
expect(subject.observed).to.equal(false);
455+
});
456+
433457
it('should have a static create function that works', () => {
434458
expect(Subject.create).to.be.a('function');
435459
const source = of(1, 2, 3, 4, 5);

src/internal/Subject.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ export class Subject<T> extends Observable<T> implements SubscriptionLike {
9191
this.observers = null!;
9292
}
9393

94+
get observed() {
95+
return this.observers?.length > 0;
96+
}
97+
9498
/** @internal */
9599
protected _trySubscribe(subscriber: Subscriber<T>): TeardownLogic {
96100
this._throwIfClosed();

0 commit comments

Comments
 (0)