本文整理匯總了TypeScript中@ngrx/store.Reducer類的典型用法代碼示例。如果您正苦於以下問題:TypeScript Reducer類的具體用法?TypeScript Reducer怎麽用?TypeScript Reducer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Reducer類的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: 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;
}
};
示例2: 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;
}
};
示例3: 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;
}
};
示例4: 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;
}
};
示例5: 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: []
};
}
};
};
示例6: constructor
constructor(
dispatcher: DevtoolsDispatcher,
actions$: Dispatcher,
reducers$: Reducer,
initialState: any,
options: Options,
extension: Extension
) {
const liftedInitialState = liftInitialState(initialState, options.monitor);
const liftReducer = liftReducerWith(initialState, liftedInitialState, options.monitor, {
maxAge: options.maxAge
});
const liftedActions$ = actions$
.skip(1)
.merge(extension.actions$)
.map(liftAction)
.merge(dispatcher, extension.liftedActions$)
.observeOn(queue);
const liftedReducers$ = reducers$
.map(liftReducer);
const liftedState = liftedActions$
.withLatestFrom(liftedReducers$)
.scan((liftedState, [ action, reducer ]) => {
const nextState = reducer(liftedState, action);
extension.notify(action, nextState);
return nextState;
}, liftedInitialState)
.publishReplay(1)
.refCount();
const state = liftedState
.map(unliftState);
this.dispatcher = dispatcher;
this.liftedState = liftedState;
this.state = state;
}
示例7: 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;
}
};
示例8: 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: []
}
}
}
示例9: clock
return state.map((person) => {
if (payload === person)
return {
name: person.name,
time: clock(payload.time, {type: HOUR, payload: 6})
};
return person;
});
示例10: 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;
}
};