當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript store.Reducer類代碼示例

本文整理匯總了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;
    }
};
開發者ID:Hongbo-Miao,項目名稱:ngrx-examples,代碼行數:12,代碼來源:reddit.ts

示例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;
  }
};
開發者ID:Hongbo-Miao,項目名稱:ngrx-examples,代碼行數:13,代碼來源:todos.ts

示例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;
  }
};
開發者ID:justdaft,項目名稱:cuddly-tribble,代碼行數:13,代碼來源:games.ts

示例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;
    }
};
開發者ID:andreyZavrazhnov,項目名稱:ngrx-examples,代碼行數:18,代碼來源:reddit.ts

示例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: []
        };
    }
  };
};
開發者ID:zhaoyiyi,項目名稱:tv-todo,代碼行數:44,代碼來源:undoable.ts

示例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;
  }
開發者ID:dballance,項目名稱:store-devtools,代碼行數:42,代碼來源:devtools.ts

示例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;
        }
    };
開發者ID:fellswoop,項目名稱:angular-project-template,代碼行數:15,代碼來源:protected.ts

示例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: []
                }
        }
    }
開發者ID:Hongbo-Miao,項目名稱:ngrx-examples,代碼行數:33,代碼來源:undoable.ts

示例9: clock

 return state.map((person) => {
     if (payload === person)
         return {
             name: person.name,
             time: clock(payload.time, {type: HOUR, payload: 6})
         };
     
     return person;
 });
開發者ID:kgosse,項目名稱:trg-ngrx-store,代碼行數:9,代碼來源:reducers.ts

示例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;
        }
    };
開發者ID:fellswoop,項目名稱:angular-project-template,代碼行數:57,代碼來源:history.ts


注:本文中的@ngrx/store.Reducer類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。