本文整理汇总了TypeScript中rxjs.Observable.scan方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Observable.scan方法的具体用法?TypeScript Observable.scan怎么用?TypeScript Observable.scan使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rxjs.Observable
的用法示例。
在下文中一共展示了Observable.scan方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: comments
export function comments(initState: Comment[], actions: Observable<Action>): Observable<Comment[]> {
return actions.scan((state: Comment[], action: Action) => {
if (action instanceof AddCommentAction) {
const { id, text, author } = action.payload;
const newComment = { id, text, author };
return [...state, newComment];
} else if (action instanceof FetchCommentAction) {
return action.payload.comments;
} else {
return state;
}
}, initState);
}
示例2: stateFactory
export function stateFactory (initialState: State, action: Observable<Action>): Observable<State> {
let state = action.scan((currentState: State, action: Action) => {
let state: State = {
items: Reduce.items(currentState.items, action),
uiState: Reduce.ui(currentState.uiState, action)
};
return state;
} , initialState);
let subject = new BehaviorSubject(initialState);
state.subscribe((newState: State) => {
return subject.next(newState);
});
return subject;
}