Skip to content

EiosH/pure-redux

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pure-redux

盗版 redux

已实现功能或 API

  1. redux
  • createStore
  • getState
  • dispatch
  • subscribe
  • replaceReducer
  • combineReducers
  • applyMiddleware
  1. redux-thunk
  2. redux-promise
  3. redux-saga
  • put
  • takeEvery
  • delay
  1. react-redux
  • connect
  • Provider
const addOne = {
  type: "ADD",
  payload: 1,
};

const subtractOne = {
  type: "SUBTRACT",
  payload: 1,
};

const reducer = (state = 1, action?: Action<number>) => {
  switch (action?.type) {
    case "ADD":
      return state + action.payload;
    case "SUBTRACT":
      return state - action.payload;
    default:
      return state;
  }
};

const sagaMiddleware = createSagaMiddleware();

const store = createStore(
  reducer,
  undefined,
  applyMiddleware(sagaMiddleware, thunk, reduxPromise, logger)
);

sagaMiddleware.run(rootSaga);

store.subscribe(() => {
  console.log("subscribe dispatch -------- >");
  console.log("state", store.getState());
});

store.dispatch({
  type: "ASYNC_ADD",
});

store.dispatch(addOne);

store.dispatch((dispatch) => {
  setTimeout(() => {
    dispatch(subtractOne);
  }, 3000);
});

const promiseAction = new Promise((resolve) => {
  setTimeout(() => {
    resolve(subtractOne);
  }, 2000);
});

store.dispatch(promiseAction);

// 运行结果
// state before dispatch
// subscribe dispatch -------- >
// state 1
// state after dispatch

// state before dispatch
// subscribe dispatch -------- >
// state 2
// state after dispatch

// state before dispatch
// subscribe dispatch -------- >
// state 3
// state after dispatch

// state before dispatch
// subscribe dispatch -------- >
// state 2
// state after dispatch

// state before dispatch
// subscribe dispatch -------- >
// state 1
// state after dispatch

About

盗版 redux + redux-saga + react-redux

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published