本文整理匯總了TypeScript中rxjs.BehaviorSubject.pipe方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript BehaviorSubject.pipe方法的具體用法?TypeScript BehaviorSubject.pipe怎麽用?TypeScript BehaviorSubject.pipe使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類rxjs.BehaviorSubject
的用法示例。
在下文中一共展示了BehaviorSubject.pipe方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: getMovies
getMovies(start: BehaviorSubject<string>): Observable<any[]> {
return start.pipe(
switchMap(startText => {
const endText = startText + '\uf8ff';
return this.db
.list('/movies', ref =>
ref
.orderByChild('Title')
.limitToFirst(10)
.startAt(startText)
.endAt(endText)
)
.snapshotChanges()
.pipe(
debounceTime(200),
distinctUntilChanged(),
map(changes => {
return changes.map(c => {
return { key: c.payload.key, ...c.payload.val() };
});
})
);
})
);
}
示例2: it
it('should handle dynamic queries that return empty sets', async (done) => {
const ITEMS = 10;
let count = 0;
let firstIndex = 0;
let pricefilter$ = new BehaviorSubject<number|null>(null);
const randomCollectionName = randomName(afs.firestore);
const ref = afs.firestore.collection(`${randomCollectionName}`);
let names = await createRandomStocks(afs.firestore, ref, ITEMS);
const sub = pricefilter$.pipe(switchMap(price => {
return afs.collection(randomCollectionName, ref => price ? ref.where('price', '==', price) : ref).valueChanges()
})).subscribe(data => {
count = count + 1;
// the first time should all be 'added'
if(count === 1) {
expect(data.length).toEqual(ITEMS);
pricefilter$.next(-1);
}
// on the second round, we should have filtered out everything
if(count === 2) {
expect(data.length).toEqual(0);
sub.unsubscribe();
deleteThemAll(names, ref).then(done).catch(done.fail);
}
});
});
示例3: constructor
constructor(
private http: HttpClient,
private api: APIService,
) {
this.tokenSubject = new BehaviorSubject(localStorage.getItem("token"));
this.tokenSubject.pipe(
filter(token => token != localStorage.getItem("token"))
).subscribe(token => {
if (token == "") {
localStorage.removeItem("token");
} else {
localStorage.setItem("token", token);
localStorage.setItem("token_age", new Date().toString())
}
});
if (!localStorage.getItem("token_age") && localStorage.getItem("token")) {
localStorage.setItem("token_age", new Date().toString());
}
// Renew the token once every 5 days
this.tokenSubject.pipe(
switchMap(t => t == "" ? never() : timer(0, 3600000)),
map(v => new Date(localStorage.getItem("token_age"))),
filter(date => new Date().getTime() - date.getTime() > 432000000),
switchMap(
v => {
console.log("Renewing user token");
return this.api.rawPost("user/token").pipe(
map(r => r.headers.get("Authorization")),
filter(token => token != ""),
catchError(err => {
console.log("Error generating new user token: ", err);
return never();
}),
);
}
),
).subscribe(token => this.tokenSubject.next(token));
}
示例4:
// I return the given top-level state key as a stream (will always emit the current
// key value as the first item in the stream).
public select<K extends keyof StateType>( key: K ) : Observable<StateType[K]> {
var selectStream = this.stateSubject.pipe(
map(
( state: StateType ) => {
return( state[ key ] );
}
),
distinctUntilChanged()
);
return( selectStream );
}
示例5: filter
function getUiMetadata$(): Observable<UIMetadata> {
if (uiMetadata$ !== null) {
return uiMetadata$;
}
const actionsCompleteError$ = action$.pipe(
filter(
uiAction =>
uiAction.action === UIActions.Completed ||
uiAction.action === UIActions.Error
)
);
const metadata$ = merge(pollingTimer$, actionsCompleteError$).pipe(
switchMap(() => queryUIServiceForMetadata()),
publishReplay(1),
refCount()
);
uiMetadata$ = metadata$;
return metadata$;
}
示例6: test
test('stops services if consequent logger upgrade fails', async () => {
const onShutdown = new BehaviorSubject<string | null>(null);
const mockOnShutdown = jest.fn(() => {
onShutdown.next('completed');
onShutdown.complete();
});
const mockLoggingConfig$ = new BehaviorSubject({ someValue: 'foo' });
mockConfigService.atPath.mockReturnValue(mockLoggingConfig$);
const root = new Root(config$, env, mockOnShutdown);
await root.start();
expect(mockOnShutdown).not.toHaveBeenCalled();
expect(mockLoggingService.stop).not.toHaveBeenCalled();
expect(mockServer.stop).not.toHaveBeenCalled();
const loggingUpgradeError = new Error('logging config consequent upgrade failed');
mockLoggingService.upgrade.mockImplementation(() => {
throw loggingUpgradeError;
});
mockLoggingConfig$.next({ someValue: 'bar' });
// Wait for shutdown to be called.
await onShutdown
.pipe(
filter(e => e !== null),
first()
)
.toPromise();
expect(mockOnShutdown).toHaveBeenCalledTimes(1);
expect(mockOnShutdown).toHaveBeenCalledWith(loggingUpgradeError);
expect(mockLoggingService.stop).toHaveBeenCalledTimes(1);
expect(mockServer.stop).toHaveBeenCalledTimes(1);
expect(mockConsoleError.mock.calls).toMatchSnapshot();
});
示例7: it
it('should handle dynamic queries that return empty sets', done => {
const ITEMS = 10;
let count = 0;
let firstIndex = 0;
let namefilter$ = new BehaviorSubject<number|null>(null);
const aref = createRef(rando());
aref.set(batch);
namefilter$.pipe(switchMap(name => {
const filteredRef = name ? aref.child('name').equalTo(name) : aref
return snapshotChanges(filteredRef);
}),take(2)).subscribe(data => {
count = count + 1;
// the first time should all be 'added'
if(count === 1) {
expect(Object.keys(data).length).toEqual(3);
namefilter$.next(-1);
}
// on the second round, we should have filtered out everything
if(count === 2) {
expect(Object.keys(data).length).toEqual(0);
}
}).add(done);
});
示例8:
function getLoadingCount$() {
return loadingCount$.pipe(distinctUntilChanged());
}
示例9: subscribe
public subscribe(next: () => void): sourcegraph.Unsubscribable {
// Do not emit until the configuration is available.
return this.data.pipe(filter(data => data !== null)).subscribe(next)
}
示例10: getState
// ---
// PUBLIC METHODS.
// ---
// I get the current state as a stream (will always emit the current state value as
// the first item in the stream).
public getState(): Observable<StateType> {
return( this.stateSubject.pipe( distinctUntilChanged() ) );
}