Skip to content

Commit 067d18d

Browse files
gnpricechrisbobbe
andcommitted
redux [nfc]: Add an activeAccountDispatch for global thunk actions
And convert to using this in one spot where we were already doing the equivalent ad hoc. Co-authored-by: Chris Bobbe <[email protected]>
1 parent 9b81292 commit 067d18d

File tree

3 files changed

+26
-36
lines changed

3 files changed

+26
-36
lines changed

src/account/accountActions.js

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
/* @flow strict-local */
22
import * as NavigationService from '../nav/NavigationService';
3-
import type {
4-
Dispatch,
5-
PerAccountAction,
6-
AllAccountsAction,
7-
ThunkAction,
8-
GlobalThunkAction,
9-
} from '../types';
3+
import type { PerAccountAction, AllAccountsAction, ThunkAction, GlobalThunkAction } from '../types';
104
import {
115
ACCOUNT_SWITCH,
126
ACCOUNT_REMOVE,
@@ -34,30 +28,13 @@ const accountSwitchPlain = (index: number): AllAccountsAction => ({
3428

3529
export const accountSwitch =
3630
(index: number): GlobalThunkAction<Promise<void>> =>
37-
async (dispatch, getState) => {
31+
async (dispatch, getState, { activeAccountDispatch }) => {
3832
NavigationService.dispatch(resetToMainTabs());
3933
dispatch(accountSwitchPlain(index));
4034

41-
/* $FlowFixMe[incompatible-type]
42-
43-
This is really a GlobalDispatch, because we're in a GlobalThunkAction.
44-
(It's global because it needs to know about all the accounts to set the
45-
pointer to the active one). But here, we pretend it's a Dispatch.
46-
That's OK, for now, because:
47-
48-
- Our Dispatch function is secretly the same value as our
49-
GlobalDispatch.
50-
- The PerAccountState that Dispatch currently acts on is the one
51-
belonging to the active account.
52-
- We want this dispatch to act on the active account -- the new,
53-
post-switch active account.
54-
- It will act on that account, because at this point we've already
55-
dispatched `accountSwitchPlain`.
56-
57-
TODO(#5006): perhaps have an `activeAccountDispatch: Dispatch` in
58-
a new GlobalThunkExtras, modeled on ThunkExtras?
59-
*/
60-
const activeAccountDispatch: Dispatch = dispatch;
35+
// Now dispatch some actions on the new, post-switch active account.
36+
// Because we just dispatched `accountSwitchPlain`, that new account
37+
// is now the active account, so `activeAccountDispatch` will act on it.
6138

6239
await activeAccountDispatch(registerAndStartPolling());
6340

src/boot/store.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import CompressedAsyncStorage from '../storage/CompressedAsyncStorage';
2020
import createMigration from '../redux-persist-migrate/index';
2121
import { getGlobalSession, getGlobalSettings } from '../directSelectors';
2222
import { migrations } from '../storage/migrations';
23-
import type { GlobalThunkExtras } from '../reduxTypes';
23+
import type { Dispatch, GlobalThunkExtras } from '../reduxTypes';
2424

2525
if (process.env.NODE_ENV === 'development') {
2626
// Chrome dev tools for Immutable.
@@ -73,17 +73,29 @@ export const cacheKeys: $ReadOnlyArray<$Keys<GlobalState>> = [
7373
'realm', 'streams', 'subscriptions', 'unread', 'userGroups', 'users',
7474
];
7575

76+
/* eslint-disable no-use-before-define */
77+
7678
const thunkExtras: $Exact<ThunkExtras> = {
77-
// eslint-disable-next-line no-use-before-define
7879
getGlobalSession: () => getGlobalSession(store.getState()),
79-
80-
// eslint-disable-next-line no-use-before-define
8180
getGlobalSettings: () => getGlobalSettings(store.getState()),
8281
};
8382

84-
const globalThunkExtras: $Exact<GlobalThunkExtras> = Object.freeze({
85-
// TODO add things
86-
});
83+
const globalThunkExtras: $Exact<GlobalThunkExtras> = {
84+
// $FlowFixMe[escaped-generic]
85+
// $FlowFixMe[incompatible-type]
86+
/* $FlowFixMe[incompatible-cast]
87+
The `store` type isn't complete: in particular it ignores thunk actions.
88+
89+
We're also using here the fact that the one function `store.dispatch`
90+
secretly plays both the role of our `GlobalDispatch` type and our
91+
`Dispatch` type... and that in the latter role, the PerAccountState
92+
that it acts on is the one belonging to the active account.
93+
94+
TODO(#5006): We'll have to add more logic here when per-account and
95+
global state become distinct.
96+
*/
97+
activeAccountDispatch: action => (store.dispatch: Dispatch)(action),
98+
};
8799

88100
const combinedThunkExtras: ThunkExtras & GlobalThunkExtras = {
89101
...thunkExtras,

src/reduxTypes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,8 @@ export type ThunkAction<T> = (Dispatch, () => PerAccountState, ThunkExtras) => T
638638

639639
/** The extras object passed to a global thunk action. */
640640
export type GlobalThunkExtras = $ReadOnly<{
641-
// TODO add things
641+
/** A per-account `dispatch` that acts on the active account. */
642+
activeAccountDispatch: Dispatch,
642643
...
643644
}>;
644645

0 commit comments

Comments
 (0)