convert(entry: INamespace): Namespace { const namespace = new Namespace(entry); namespace.applications = this.applicationBindingService .getBoundApplications(namespace.getId()) .pipe( map(boundNamespaces => { const namespaces = boundNamespaces['applications']; return namespaces ? namespaces.length : 0; }), catchError(() => { return of(0); }), publishReplay(1), refCount() ); const servicesUrl = `${ AppConfig.k8sApiServerUrl }namespaces/${namespace.getId()}/services`; namespace.services = this.http.get<any>(servicesUrl).pipe( map(res => { return res.items.length; }), catchError(() => { return of(0); }), publishReplay(1), refCount() ); return namespace; }
constructor(ref: DocumentReference) { this.ref = ref; this.index = new PackedIndex(); this.data().subscribe(data => this.index.setInstitution(data)); this.gradeDistribution = this.data().pipe( switchMap(data => Axios.get(`${data.cloudFunctionsUrl}/api/grades`)), map( (response: { data: any }) => Object.freeze(response.data) as Distribution ), publishReplay(1), refCount() ); this.courseRanking = combineLatest( this.getGradeDistribution().pipe( map(distribution => { return Object.entries(distribution).reduce((obj, [course, data]) => { return { ...obj, [course]: 2 * Object.values(data).reduce((a, b) => a + b, 0) }; }, {}); }) ), this.index.getSequences().pipe( switchMap(sequences => { return combineLatest( sequences.map(sequence => this.index.getSparseSequence(sequence)) ); }) ) ).pipe( map(([courseRanking, sparseSequences]) => { sparseSequences.forEach(sequence => { // Promote the rank of each course in the sequence to the max. const max = sequence .map(course => courseRanking[course] | 0) .reduce((a, b) => Math.max(a, b)) + 1; sequence.forEach(course => (courseRanking[course] = max)); }); return courseRanking; }), publishReplay(1), refCount() ); }
it('should be repeatable using a ReplaySubject', () => { function subjectFactory() { return new ReplaySubject(1); } const source = cold('-1-2-3----4-| '); const sourceSubs = ['^ ! ', ' ^ ! ', ' ^ !']; const multicasted = source.pipe( multicast(subjectFactory), refCount() ); const subscribe1 = 's '; const expected1 = '-1-2-3----4--1-2-3----4--1-2-3----4-|'; const subscribe2 = ' s '; const expected2 = ' 23----4--1-2-3----4--1-2-3----4-|'; expectObservable( hot(subscribe1).pipe(tap(() => { expectObservable(multicasted.pipe(repeat(3))).toBe(expected1); })) ).toBe(subscribe1); expectObservable( hot(subscribe2).pipe(tap(() => { expectObservable(multicasted.pipe(repeat(3))).toBe(expected2); })) ).toBe(subscribe2); expectSubscriptions(source.subscriptions).toBe(sourceSubs); });
getNavData(): Observable<any> { if (!this._navData) { this._navData = this.httpClient.get(environment.serverUrl + 'application') .pipe(publishReplay(), refCount()); } return this._navData; }
it('should be retryable', () => { function subjectFactory() { return new Subject<string>(); } const source = cold('-1-2-3----4-# '); const sourceSubs = ['^ ! ', ' ^ ! ', ' ^ !']; const multicasted = source.pipe( multicast(subjectFactory), refCount() ); const subscribe1 = 's '; const expected1 = '-1-2-3----4--1-2-3----4--1-2-3----4-#'; const subscribe2 = ' s '; const expected2 = ' -3----4--1-2-3----4--1-2-3----4-#'; expectObservable( hot(subscribe1).pipe(tap(() => { expectObservable(multicasted.pipe(retry(2))).toBe(expected1); })) ).toBe(subscribe1); expectObservable( hot(subscribe2).pipe(tap(() => { expectObservable(multicasted.pipe(retry(2))).toBe(expected2); })) ).toBe(subscribe2); expectSubscriptions(source.subscriptions).toBe(sourceSubs); });
it('should be repeatable with ReplaySubject and cold source is synchronous', () => { function subjectFactory() { return new ReplaySubject(1); } const source = cold('(123|)'); const multicasted = source.pipe( multicast(subjectFactory), refCount() ); const subscribe1 = 's '; const expected1 = '(123123123123123|) '; const subscribe2 = ' s '; const expected2 = ' (123123123123123|)'; const sourceSubs = ['(^!)', '(^!)', '(^!)', '(^!)', '(^!)', ' (^!)', ' (^!)', ' (^!)', ' (^!)', ' (^!)']; expectObservable( hot(subscribe1).pipe(tap(() => { expectObservable(multicasted.pipe(repeat(5))).toBe(expected1); })) ).toBe(subscribe1); expectObservable(hot(subscribe2).pipe(tap(() => { expectObservable(multicasted.pipe(repeat(5))).toBe(expected2); }))).toBe(subscribe2); expectSubscriptions(source.subscriptions).toBe(sourceSubs); });
it('should be retryable when cold source is synchronous', () => { function subjectFactory() { return new Subject<string>(); } const source = cold('(123#)'); const multicasted = source.pipe( multicast(subjectFactory), refCount() ); const subscribe1 = 's '; const expected1 = '(123123123123#) '; const subscribe2 = ' s '; const expected2 = ' (123123123123#)'; const sourceSubs = ['(^!)', '(^!)', '(^!)', '(^!)', ' (^!)', ' (^!)', ' (^!)', ' (^!)']; expectObservable(hot(subscribe1).pipe(tap(() => { expectObservable(multicasted.pipe(retry(3))).toBe(expected1); }))).toBe(subscribe1); expectObservable(hot(subscribe2).pipe(tap(() => { expectObservable(multicasted.pipe(retry(3))).toBe(expected2); }))).toBe(subscribe2); expectSubscriptions(source.subscriptions).toBe(sourceSubs); });
it('should unsub from the source when all other subscriptions are unsubbed', (done: MochaDone) => { let unsubscribeCalled = false; const connectable = new Observable<boolean>(observer => { observer.next(true); return () => { unsubscribeCalled = true; }; }).pipe(publish()); const refCounted = connectable.pipe(refCount()); const sub1 = refCounted.subscribe(() => { //noop }); const sub2 = refCounted.subscribe(() => { //noop }); const sub3 = refCounted.subscribe((x: any) => { expect((connectable as any)._refCount).to.equal(1); }); sub1.unsubscribe(); sub2.unsubscribe(); sub3.unsubscribe(); expect((connectable as any)._refCount).to.equal(0); expect(unsubscribeCalled).to.be.true; done(); });
public getLogPerson(id: string): Observable<ActionLog[]> { if (!this._logs) { this._logs = this._http .get('action-log/list?id=' + id + '&context=person') .pipe(map(this.deserializeList), publishReplay(), refCount()); } return this._logs; }
'(dis)connecting hot one', () => { const source = cold('--1-2---3-4--5-|'); const sourceSubs = '^ !'; const expected = '--1-2---3-4--5-|'; const result = source.pipe( publish(), refCount() ); expectObservable(result).toBe(expected); expectSubscriptions(source.subscriptions).toBe(sourceSubs); });