本文整理匯總了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;
}