Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic import promise channel action off by one #308

Open
ChrisChiasson opened this issue Jul 13, 2019 · 0 comments
Open

dynamic import promise channel action off by one #308

ChrisChiasson opened this issue Jul 13, 2019 · 0 comments

Comments

@ChrisChiasson
Copy link

In a project organized similarly to the example project, I've put rsf.js into a folder named rsf. Inside that folder, I've also put an index.js that dynamically import()s rsf.js at file scope. This system allows firebase and redux-saga-firebase to be in a separate bundle from the rest of the app for faster loading, which is important since firebase is huge and drops my mobile page speed score a lot.

// src/rsf/index.js
var p; //in case this code gets executed twice somehow
if(!p) p= import( /* webpackPreload: true */ "./rsf.js");
const prsf= (cb)=>p.then(cb);
export default prsf;

Imports of src/rsf in the sagas don't have to change, and the call sites can be transformed as follows:

// src/sagas/login.js (this is just a fragment of the file)
function* loginStatusWatcher() {
  //const channel = yield call(rsf.auth.channel); //original
  const channel = yield call(()=>prsf(({rsf})=>rsf.auth.channel)); //call unwraps promise
  while (true) {
    const r = yield take(channel);
    console.log("loginStatusWatcher",r); // log result r to console
    if (r && r.user) yield put(actions.loginSuccess(r.user));
    else yield put(actions.logoutSuccess()); // this line triggers logout... not important
  }
}

The thing I can't figure out is, this saga no longer works after the change. Instead of r being a user object, it is now always the last action object before the login status change. For example, when the app is first loaded, the last action is to check some messages from another channel. So, the result r is the messages action object, which is absolutely mind boggling. Later, if the user attempts to sign in anonymously, the console log shows r to be the login action object, not the user object. Screenshot below. I assume the promise has confused redux-saga, redux-saga-firebase, or perhaps just myself (quite possible since I'm a total newbie). Do you know how to get out of this but keep the separate firebase bundle? Any idea what might be causing it? I filed an issue here because other channels whose call sites have been similarly transformed to use prsf, such as the messages channel, are returning the expected QuerySnapshot object.
Screenshot 2019-07-13 at 1 03 41 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant