本文整理汇总了TypeScript中@ngrx/store.Reducer.default方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Reducer.default方法的具体用法?TypeScript Reducer.default怎么用?TypeScript Reducer.default使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类@ngrx/store.Reducer
的用法示例。
在下文中一共展示了Reducer.default方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: function
return function (state = initialState, action : Action) {
const { past, present, future } = state;
switch (action.type) {
case 'UNDO':
const previous = past[past.length - 1];
const newPast = past.slice(0, past.length - 1);
return {
past: newPast,
present: previous,
future: [ present, ...future ]
};
case 'REDO':
const next = future[0];
const newFuture = future.slice(1);
return {
past: [ ...past, present ],
present: next,
future: newFuture
};
default:
// Delegate handling the action to the passed reducer
const newPresent = reducer(present, action);
if (present === newPresent) {
return state
}
return {
past: [ ...past, present ],
present: newPresent,
future: []
}
}
}
示例2: return
return (state = initial, action: Action) => {
let next;
let type = action.type;
let historyReg = new RegExp('^(\\[HISTORY\\] [A-Z]+).*');
if(key && historyReg.test(type)) {
let keyReg = new RegExp(` \\[${key}\\]`);
if (!keyReg.test(type)) {
return state;
} else {
type = type.replace(historyReg, '$1');
}
}
switch(type) {
case History.undo:
if(past.length) {
next = past[past.length - 1];
past = past.slice(0, past.length - 1);
future = [state, ...future];
return next;
} else {
return state;
}
case History.redo:
if(future.length) {
next = future[0];
past = [...past, state];
future = future.slice(1);
return next;
} else {
return state;
}
case History.reset:
past = [...past, state];
future = future.slice(1);
return initial;
default:
next = reducer(state, action);
if (state === next) {
return state;
}
past = [...past, state];
future = [];
return next;
}
};
示例3: switch
export const postsByReddit : Reducer<RedditPosts> = (state: {} = {}, action : Action) => {
switch (action.type) {
case INVALIDATE_REDDIT:
case RECEIVE_POSTS:
case REQUEST_POSTS:
return Object.assign({}, state, {
[action.payload.reddit]: posts(state[action.payload.reddit], action)
});
default:
return state;
}
};
示例4: switch
export const todos : Reducer<Todo[]> = (state : Todo[] = [], action: Action) => {
switch(action.type) {
case ADD_TODO:
return [
...state,
todo(undefined, action)
];
case TOGGLE_TODO:
return state.map(t => todo(t, action));
default:
return state;
}
};
示例5: switch
export const games: Reducer<IGame[]> = (state: IGame[] = [], action: Action) => {
switch (action.type) {
case ADD_GAME:
return [
...state,
game(undefined, action)
];
case TOGGLE_GAME:
return state.map(t => game(t, action));
default:
return state;
}
};
示例6: switch
export const postsByReddit : Reducer<RedditPosts> = (state: {} = {}, {type, payload} : Action) => {
switch (type) {
case LOADING:
console.log('LOADING');
return Object.assign({}, state, {
isFetching: true,
didInvalidate: false,
});
case RECEIVE_POSTS:
return Object.assign({}, state, {
isFetching: false,
[payload.reddit]: posts(state[payload.reddit], {type, payload})
});
default:
return state;
}
};
示例7: undoable
export function undoable(reducer: Reducer<any>) {
// Call the reducer with empty action to populate the initial state
const initialState: UndoableState = {
past: [],
present: reducer(undefined, { type: '__INIT__' }),
future: []
};
// Return a reducer that handles undo and redo
return function (state = initialState, action: Action) {
const { past, present, future } = state;
switch (action.type) {
case 'UNDO':
if (past.length <= 1) return state;
const previous = past[past.length - 1];
const newPast = past.slice(0, past.length - 1);
return {
past: newPast,
present: previous,
future: [present, ...future]
};
case 'REDO':
if (future.length < 1) return state;
const next = future[0];
const newFuture = future.slice(1);
return {
past: [...past, present],
present: next,
future: newFuture
};
default:
// Delegate handling the action to the passed reducer
const newPresent = reducer(present, action);
if (present === newPresent) {
return state;
}
return {
past: [...past, present],
present: newPresent,
future: []
};
}
};
};
示例8: reducer
return (state: any, action: Action) => {
let reg = new RegExp(`^\\[${key}\\] (.+)`);
if(action.type === Store.init) {
return reducer(state, action);
} else if(reg.test(action.type)) {
action = Object.assign({}, action, {
'type': action.type.replace(reg, '$1')
});
return reducer(state, action);
} else {
return state;
}
};
示例9: clock
return state.map((person) => {
if (payload === person)
return {
name: person.name,
time: clock(payload.time, {type: HOUR, payload: 6})
};
return person;
});
示例10: todo
return state.map(t => todo(t, action));