-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1d5e3c
commit 21ce767
Showing
5 changed files
with
74 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { TestSubscriptionCounter } from '../src'; | ||
import { BehaviorSubject } from "rxjs"; | ||
|
||
describe("TestSubscriptionCounter", () => { | ||
let counter: TestSubscriptionCounter<number>; | ||
let observable: BehaviorSubject<number> | ||
|
||
beforeEach(() => { | ||
observable = new BehaviorSubject<number>(0); | ||
counter = new TestSubscriptionCounter<number>(observable); | ||
}); | ||
|
||
describe("countedObservable$", () => { | ||
it("should return a counted observable", () => { | ||
expect(counter.countedObservable$).toBeTruthy(); | ||
const tempNumber = Math.floor(Math.random() * 10); | ||
observable.next(tempNumber); | ||
const subscription = counter.countedObservable$.subscribe((value) => { | ||
expect(value).toEqual(tempNumber) | ||
}); | ||
expect(counter.activeSubscriptionCount).toEqual(1); | ||
expect(counter.lifetimeSubscriptionCount).toEqual(1); | ||
expect(counter.allSubscriptionsFinalized).toEqual(false); | ||
expect(counter.hadSubscribers).toEqual(true); | ||
expect(counter.hasSubscribers).toEqual(true); | ||
subscription.unsubscribe(); | ||
expect(counter.activeSubscriptionCount).toEqual(0); | ||
expect(counter.allSubscriptionsFinalized).toEqual(true); | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
export { IMockClassOptions } from "./autoMocker"; | ||
export { | ||
readObservableErrorSynchronously, | ||
readObservableSynchronouslyAfterAction, | ||
readObservableSynchronously, | ||
readObservableCompletionSynchronously | ||
} from './read-observable-synchronously'; | ||
export { readObservableSynchronously, readObservableCompletionSynchronously, readObservableErrorSynchronously, readObservableSynchronouslyAfterAction } from './read-observable-synchronously'; | ||
export { TestSubscriptionCounter } from './test-subscription-counter'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Observable, finalize, Subscriber } from 'rxjs'; | ||
|
||
export class TestSubscriptionCounter<T> { | ||
private _lifetimeSubscriptionCount: number = 0; | ||
private _activeSubscriptionCount: number = 0; | ||
|
||
public readonly countedObservable$: Observable<T> = new Observable<T>((observer: Subscriber<T>) => { | ||
this._lifetimeSubscriptionCount++; | ||
this._activeSubscriptionCount++; | ||
|
||
const subscription = this.observable$ | ||
.pipe(finalize(() => this._activeSubscriptionCount--)) | ||
.subscribe(observer); | ||
|
||
return () => subscription.unsubscribe(); | ||
}); | ||
|
||
constructor(private readonly observable$: Observable<T>) {} | ||
|
||
public get lifetimeSubscriptionCount(): number { | ||
return this._lifetimeSubscriptionCount; | ||
} | ||
|
||
public get activeSubscriptionCount(): number { | ||
return this._activeSubscriptionCount; | ||
} | ||
|
||
public get hadSubscribers(): boolean { | ||
return this.lifetimeSubscriptionCount > 0; | ||
} | ||
|
||
public get hasSubscribers(): boolean { | ||
return this.activeSubscriptionCount > 0; | ||
} | ||
|
||
public get allSubscriptionsFinalized(): boolean { | ||
return this.activeSubscriptionCount === 0; | ||
} | ||
} |