Skip to content

Commit

Permalink
storage: Account explicitly for first startup, too.
Browse files Browse the repository at this point in the history
Based on Robert Hönig's observation of what happens on first startup
of the app.
  • Loading branch information
gnprice committed Aug 1, 2018
1 parent 744b3b8 commit 5075ff6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
7 changes: 4 additions & 3 deletions src/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ import type {

export type RehydrateAction = {
type: typeof REHYDRATE,
// The payload will have `null` at each key where an error was encountered
// in reading the persisted store.
payload: GlobalState | { accounts: null },
// The payload will be empty on first startup or if the persisted store is
// just missing keys, and will have `null` at each key where an error was
// encountered in reading the persisted store.
payload: GlobalState | { accounts: null } | {},
error: ?Object,
};

Expand Down
9 changes: 5 additions & 4 deletions src/nav/navReducers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* @flow */
import type {
GlobalState,
NavigationState,
NavAction,
RehydrateAction,
Expand All @@ -22,12 +23,12 @@ import {
const initialState = getStateForRoute('loading') || NULL_NAV_STATE;

const rehydrate = (state: NavigationState, action: RehydrateAction): NavigationState => {
// If there's an error reading the persisted state, we'll get a
// RehydrateAction with `null` at each key.
if (action.payload.accounts === null) {
if (!action.payload || !action.payload.accounts) {
return getStateForRoute('welcome') || state;
}
return getInitialNavState(action.payload) || state;
// $FlowFixMe: Flow oddly doesn't see the refinement from the condition above.
const rehydratedState = (action.payload: GlobalState);
return getInitialNavState(rehydratedState) || state;
};

const accountSwitch = (state: NavigationState, action: AccountSwitchAction): NavigationState =>
Expand Down
20 changes: 12 additions & 8 deletions src/session/sessionReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,18 @@ const loginSuccess = (state: SessionState, action: LoginSuccessAction): SessionS
needsInitialFetch: true,
});

const rehydrate = (state: SessionState, action: RehydrateAction): SessionState => ({
...state,
isHydrated: true,
// On rehydration, do an initial fetch if we have access to an account
// (indicated by the presence of an api key). Otherwise, the initial fetch
// will be initiated on loginSuccess.
needsInitialFetch: !!(action.payload.accounts && getAuth(action.payload).apiKey),
});
const rehydrate = (state: SessionState, action: RehydrateAction): SessionState => {
const payload = { action };
const haveApiKey = !!(payload && payload.accounts && getAuth(payload).apiKey);
return {
...state,
isHydrated: true,
// On rehydration, do an initial fetch if we have access to an account
// (indicated by the presence of an api key). Otherwise, the initial fetch
// will be initiated on loginSuccess.
needsInitialFetch: haveApiKey,
};
};

const realmInit = (state: SessionState, action: RealmInitAction): SessionState => ({
...state,
Expand Down

0 comments on commit 5075ff6

Please sign in to comment.