本文整理汇总了TypeScript中rxjs.timer函数的典型用法代码示例。如果您正苦于以下问题:TypeScript timer函数的具体用法?TypeScript timer怎么用?TypeScript timer使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了timer函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
it('should raises error when promise rejects', (done: MochaDone) => {
const e1 = concat(
of(1),
timer(10).pipe(mapTo(2)),
timer(10).pipe(mapTo(3)),
timer(100).pipe(mapTo(4))
);
const expected = [1, 2];
const error = new Error('error');
e1.pipe(
debounce((x: number) => {
if (x === 3) {
return new Promise((resolve: any, reject: any) => { reject(error); });
} else {
return new Promise((resolve: any) => { resolve(42); });
}
})
).subscribe((x: number) => {
expect(x).to.equal(expected.shift()); },
(err: any) => {
expect(err).to.be.an('error', 'error');
expect(expected.length).to.equal(0);
done();
}, () => {
done(new Error('should not be called'));
});
});
示例2: combineLatest1
combineLatest1() {
// timerOne emits first value at 1s, then once every 4s
const timerOne = timer(1000, 4000);
// timerTwo emits first value at 2s, then once every 4s
const timerTwo = timer(2000, 4000);
// timerThree emits first value at 3s, then once every 4s
const timerThree = timer(3000, 4000);
// when one timer emits, emit the latest values from each timer as an array
const combined = combineLatest(timerOne, timerTwo, timerThree);
const subscribe = combined.subscribe(
([timerValOne, timerValTwo, timerValThree]) => {
/*
Example:
timerOne first tick: 'Timer One Latest: 1, Timer Two Latest:0, Timer Three Latest: 0
timerTwo first tick: 'Timer One Latest: 1, Timer Two Latest:1, Timer Three Latest: 0
timerThree first tick: 'Timer One Latest: 1, Timer Two Latest:1, Timer Three Latest: 1
*/
console.log(
`Timer One Latest: ${timerValOne},
Timer Two Latest: ${timerValTwo},
Timer Three Latest: ${timerValThree}`
);
}
);
}
示例3: constructor
constructor() {
this.everySecond$ = timer(1000, 1000).pipe(
share(),
);
this.everyHalfSecond$ = timer(500, 500).pipe(
share(),
);
}
示例4: it
it('should throttle events multiple times', () => {
const expected = ['1-0', '2-0'];
concat(
timer(0, 10, rxTestScheduler).pipe(take(3), map((x: number) => '1-' + x)),
timer(80, 10, rxTestScheduler).pipe(take(5), map((x: number) => '2-' + x))
).pipe(
throttleTime(50, rxTestScheduler)
).subscribe((x: string) => {
expect(x).to.equal(expected.shift());
});
rxTestScheduler.flush();
});
示例5: it
it('should emit a single value immediately', () => {
const dueTime = time('|');
const expected = '(x|)';
const source = timer(dueTime, rxTestScheduler);
expectObservable(source).toBe(expected, {x: 0});
});
示例6: windowWhen1
windowWhen1() {
// emit immediately then every 1s
const source = timer(0, 1000);
const example = source.pipe(
// close window every 5s and emit observable of collected values from source
windowWhen(() => interval(5000)),
tap(_ => console.log('NEW WINDOW!'))
);
const subscribeTwo = example
.pipe(
// window emits nested observable
mergeAll()
/*
output:
"NEW WINDOW!"
0
1
2
3
4
"NEW WINDOW!"
5
6
7
8
9
*/
)
.subscribe(val => console.log(val));
}
示例7: of
const source = of(expiresAt).pipe(flatMap(expiration => {
const now = Date.now();
// Use the delay in a timer to
// run the refresh at the proper time
return timer(Math.max(1, expiresAt - now));
}));
示例8: timer
registry.register('job-obs-async', createJobHandler<number, number, number>(arg => {
return timer(1).pipe(
take(3),
take(1),
map(() => arg + 1),
);
}));
示例9: observableTimer
.pipe(catchError((error : Error, source) => {
if (!shouldRetry(error)) {
throw error;
}
const currentError = error instanceof RequestError &&
isOfflineRequestError(error) ? ERROR_TYPES.OFFLINE : ERROR_TYPES.REGULAR;
const maxRetry = currentError === ERROR_TYPES.OFFLINE ?
maxRetryOffline : maxRetryRegular;
if (currentError !== lastError) {
retryCount = 0;
lastError = currentError;
}
if (++retryCount > maxRetry) {
throw error;
}
if (onRetry) {
onRetry(error, retryCount);
}
const delay = Math.min(
baseDelay * Math.pow(2, retryCount - 1),
maxDelay
);
const fuzzedDelay = getFuzzedDelay(delay);
return observableTimer(fuzzedDelay).pipe(
mergeMap(() => source));
}));