本文整理匯總了TypeScript中rxjs/operators.scan函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript scan函數的具體用法?TypeScript scan怎麽用?TypeScript scan使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了scan函數的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: initialize
public initialize() {
return {
view: {
base: this.intent.test().pipe(
scan((acc, next: any) => acc + 1, 1),
share(),
startWith(1),
),
test: this.intent.test().pipe(
scan((acc, next: any) => acc + 1, 1),
share(),
startWith(1),
),
test2: this.subject.pipe(
map(({ type, payload, states }) => {
switch (type) {
case 'subject-test':
return payload + states.test2;
default:
return payload;
}
}),
startWith(1),
),
},
};
}
示例2: higherOrderScan
export function scan<T, R>(this: Observable<T>,
accumulator: (acc: T | Array<T> | R, value: T, index: number) => T | Array<T> | R,
seed?: T | R): Observable<R> {
if (arguments.length >= 2) {
return higherOrderScan(accumulator, seed)(this) as Observable<R>;
}
return higherOrderScan(accumulator)(this) as Observable<R>;
}
示例3: fromCollectionRef
export function sortedChanges<T>(query: Query, events: DocumentChangeType[]): Observable<DocumentChangeAction<T>[]> {
return fromCollectionRef(query)
.pipe(
map(changes => changes.payload.docChanges()),
scan((current, changes) => combineChanges(current, changes, events), []),
map(changes => changes.map(c => ({ type: c.type, payload: c } as DocumentChangeAction<T>))));
}
示例4: switchMap
return switchMap(obs => {
return combineLatest(
...obs.map(o => o.pipe(take(1), startWith(INITIAL_VALUE as INTERIM_VALUES))))
.pipe(
scan(
(acc: INTERIM_VALUES, list: INTERIM_VALUES[]) => {
let isPending = false;
return list.reduce((innerAcc, val, i: number) => {
if (innerAcc !== INITIAL_VALUE) return innerAcc;
// Toggle pending flag if any values haven't been set yet
if (val === INITIAL_VALUE) isPending = true;
// Any other return values are only valid if we haven't yet hit a pending call.
// This guarantees that in the case of a guard at the bottom of the tree that
// returns a redirect, we will wait for the higher priority guard at the top to
// finish before performing the redirect.
if (!isPending) {
// Early return when we hit a `false` value as that should always cancel
// navigation
if (val === false) return val;
if (i === list.length - 1 || val instanceof UrlTree) {
return val;
}
}
return innerAcc;
}, acc);
},
INITIAL_VALUE),
filter(item => item !== INITIAL_VALUE), take(1)) as Observable<boolean|UrlTree>;
});
示例5: takeUntil2
takeUntil2() {
// emit value every 1s
const source = interval(1000);
// is number even?
const isEven = val => val % 2 === 0;
// only allow values that are even
const evenSource = source.pipe(filter(isEven));
// keep a running total of the number of even numbers out
const evenNumberCount = evenSource.pipe(scan((acc, _) => acc + 1, 0));
// do not emit until 5 even numbers have been emitted
const fiveEvenNumbers = evenNumberCount.pipe(filter(val => val > 5));
const example = evenSource.pipe(
// also give me the current even number count for display
withLatestFrom(evenNumberCount),
map(([val, count]) => `Even number (${count}) : ${val}`),
// when five even numbers have been emitted, complete source observable
takeUntil(fiveEvenNumbers)
);
/*
Even number (1) : 0,
Even number (2) : 2
Even number (3) : 4
Even number (4) : 6
Even number (5) : 8
*/
const subscribe = example.subscribe(val => console.log(val));
}
示例6: it
it('should pass current index to accumulator', () => {
const values = {
a: 1, b: 3, c: 5,
x: 1, y: 4, z: 9
};
let idx = [0, 1, 2];
const e1 = hot('--a--b--c--|', values);
const e1subs = '^ !';
const expected = '--x--y--z--|';
const scanFunction = (o: number, value: number, index: number) => {
expect(index).to.equal(idx.shift());
return o + value;
};
const scanObs = e1.pipe(
scan(scanFunction, 0),
finalize(() => {
expect(idx).to.be.empty;
})
);
expectObservable(scanObs).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
示例7:
export function auditTrail<T>(query: DatabaseQuery, events?: ChildEvent[]): Observable<SnapshotAction<T>[]> {
const auditTrail$ = stateChanges<T>(query, events)
.pipe(
scan<SnapshotAction<T>>((current, action) => [...current, action], [])
);
return waitForLoaded<T>(query, auditTrail$);
}
示例8: multicats
@Get('broadcast')
multicats() {
return this.client.send<number>({ cmd: 'broadcast' }, {})
.pipe(
scan((a, b) => a + b),
take(2),
);
}
示例9: scan1
scan1() {
const source = of(1, 2, 3);
// basic scan example, sum over time starting with zero
const example = source.pipe(scan((acc, curr) => acc + curr, 0));
// log accumulated values
// output: 1,3,6
const subscribe = example.subscribe(val => console.log(val));
}
示例10: omit
useEpic((actor$) => {
return actor$.pipe(
rxFilter(RequestActor.isRequestActor),
rxScan(
(counts, actor: any) => {
const parentActorType = actor.opts.parentActor.type;
const count = counts[parentActorType] || 0;
if (actor.stage === AsyncStage.STARTED) {
return {
...counts,
[parentActorType]: count + 1,
};
}
if (count > 1) {
return {
...counts,
[parentActorType]: count - 1,
};
}
return omit(counts, parentActorType);
},
{} as Dictionary<number>,
),
rxTap((nextRequests) => {
requesting$.next(size(nextRequests) > 0);
}),
rxIgnoreElements(),
);
});