Giter Site home page Giter Site logo

kutlugsahin / redux-saga-callback Goto Github PK

View Code? Open in Web Editor NEW
21.0 3.0 2.0 374 KB

redux-saga helper functions to await dispatched actions

License: MIT License

JavaScript 52.83% TypeScript 47.17%
redux redux-saga higher-order-functions yield putwait

redux-saga-callback's People

Contributors

dependabot[bot] avatar kutlugsahin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

redux-saga-callback's Issues

Does redux-saga-callback swallow actions?

Firstly, thanks for this code. It's in TypeScript, which is a big win, and provides a valuable approach on the topic of saga composition.

In the case where I want the dispatched action to actually be processed by the reducers, will that happen?

My issue is that when I yield call(effect, action()), the action doesn't reach my redux store. In some cases, that's an issue. I could split my actions into saga triggering events and redux reducer actions, but that seems a bit convoluted, error prone, etc. I guess to work around this would require a redux middleware... ๐Ÿค”

Actions must be plain objects. Use custom middleware for async actions.

I'm a little stuck. I recieve the callback to the action creator but unable to send that to the intial call function at Login. Any help would be appreciated

Login

handleOnLogin = async (values, actions) => {
    const { email, password } = values;
    try {
      const response = await this.props.signIn({ email, password });
      if (response.authentication) {
        this.props.navigation.navigate("Home");
      }
  };

Actions

export const signIn = ({ email, password }) => {
  reduxStore.store.dispatch({
    type: Actions.SIGN_IN_REQUEST,
    payload: {
      email: _.trim(email),
      password: _.trim(password)
    },
    onComplete: ({ error, cancelled, data }) => {
      console.log("ACTIONS", { error, cancelled, data });
      return data
    }
  });
};

Sagas

const signIn = function* signIn({ payload }) {
  try {
    const { email, password } = payload;
    const response = yield call(API.signIn, { email, password });
    // yield putWait({ type: Actions.SIGN_IN_SUCCESS, payload: response });
    return { response, authentication: true };
  } catch (error) {
    // yield putWait({ type: Actions.SIGN_IN_FAIL, error });
    return { error, authentication: false };
  }
};

usage with takeLatest/takeLeading

First of all thanks for this library.
But looks like putWait won't work for

  • takeLatest(actiontype, withCallback(... ->
  • takeLeading(actiontype, withCallback(... ->

take latest example

function* fetchUsers() {
    const users = yield fetch('/users');

    // put users to store
    yield put(putUsers(users));

    // returned value will be passed to onComplete function as parameter
    // Exceptions will be handled by the withCallback and will also be passed to onComplete
    return users;
}

export function*(){
    yield all([
        takeLatest('FETCH_USERS', withCallback(fetchUsers))
    ])
}

function* sample() {
    yield fork(function* () {
         yield putWait(fetchUsers()); // will be completed before users loaded because first handler is cancelled
         const users = yield select(getUsers);  // here users will be null
    });

    yield fork(function* () {
         yield putWait(fetchUsers());
         const users = yield select(getUsers); 
    });
}

takeLeading example

....


export function*(){
    yield all([
        takeLeading('FETCH_USERS', withCallback(fetchUsers))
    ])
}

function* sample() {
    yield fork(function* () {
         yield putWait(fetchUsers()); 
         const users = yield select(getUsers);  
    });

    yield fork(function* () {
        // this putWait never completes because takeLeading ignore second action while first one is processing
         yield putWait(fetchUsers());  
         const users = yield select(getUsers); 
    });
}

Maybe you have any idea how to get working it in these cases. Thanks in advance!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.