Skip to content

Commit b6218db

Browse files
committed
model: Factor out new RESET_ACCOUNT_DATA action
As Greg proposed in #4446, so the pattern of consistently clearing data on all ways of leaving an account gets maintained naturally: (except we chose the name RESET_ACCOUNT_DATA instead of CLEAR_ACCOUNT_DATA) > * I think it'd be a useful refactor to consolidate one action type > like `CLEAR_ACCOUNT_DATA`. > * The action creators for these three (in `accountActions.js`) can > dispatch that first, then also a `LOGOUT` etc. > * Almost all the reducers would respond only to the > `CLEAR_ACCOUNT_DATA`. > * There are a couple of exceptions, like `accountReducers` and > `sessionReducers`, that actually want to treat the three cases > differently. Those would be the only ones to continue responding > to `LOGOUT` etc.; and they'd stand out better as a result. > * Then there are further changes we might make to those, but > that'll be easier to see after that. Fixes: #4446
1 parent 4736d8f commit b6218db

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+128
-231
lines changed

src/__tests__/lib/exampleData.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { CreateWebPublicStreamPolicy, EmailAddressVisibility } from '../../api/p
2525
import type {
2626
AccountSwitchAction,
2727
LoginSuccessAction,
28+
ResetAccountDataAction,
2829
RegisterCompleteAction,
2930
MessageFetchStartAction,
3031
MessageFetchCompleteAction,
@@ -44,6 +45,7 @@ import { ZulipVersion } from '../../utils/zulipVersion';
4445
import {
4546
ACCOUNT_SWITCH,
4647
LOGIN_SUCCESS,
48+
RESET_ACCOUNT_DATA,
4749
REGISTER_COMPLETE,
4850
EVENT_NEW_MESSAGE,
4951
MESSAGE_FETCH_START,
@@ -705,6 +707,7 @@ export const action = Object.freeze({
705707
email: selfAccount.email,
706708
apiKey: selfAccount.apiKey,
707709
}): LoginSuccessAction),
710+
reset_account_data: (deepFreeze({ type: RESET_ACCOUNT_DATA }): ResetAccountDataAction),
708711

709712
/**
710713
* A minimal well-typed REGISTER_COMPLETE action from a recent server.

src/account/accountActions.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { registerAndStartPolling } from '../events/eventActions';
1111
import { resetToMainTabs } from '../nav/navActions';
1212
import { sendOutbox } from '../outbox/outboxActions';
1313
import { initNotifications } from '../notification/notifTokens';
14+
import { resetAccountData } from './logoutActions';
1415

1516
export const dismissServerPushSetupNotice = (): PerAccountAction => ({
1617
type: DISMISS_SERVER_PUSH_SETUP_NOTICE,
@@ -30,6 +31,13 @@ export const accountSwitch =
3031
(index: number): GlobalThunkAction<Promise<void>> =>
3132
async (dispatch, getState, { activeAccountDispatch }) => {
3233
NavigationService.dispatch(resetToMainTabs());
34+
35+
// Clear out the space we use for the active account's server data, to
36+
// make way for a new active account.
37+
// TODO(#5006): When each account has its own space to hold server data,
38+
// we won't have to do this.
39+
activeAccountDispatch(resetAccountData());
40+
3341
dispatch(accountSwitchPlain(index));
3442

3543
// Now dispatch some actions on the new, post-switch active account.
@@ -60,6 +68,14 @@ export const loginSuccess =
6068
(realm: URL, email: string, apiKey: string): GlobalThunkAction<Promise<void>> =>
6169
async (dispatch, getState, { activeAccountDispatch }) => {
6270
NavigationService.dispatch(resetToMainTabs());
71+
72+
// In case there's already an active account, clear out the space we use
73+
// for the active account's server data, to make way for a new active
74+
// account.
75+
// TODO(#5006): When each account has its own space to hold server data,
76+
// we won't have to do this.
77+
activeAccountDispatch(resetAccountData());
78+
6379
dispatch(loginSuccessPlain(realm, email, apiKey));
6480

6581
// Now dispatch some actions on the new, post-login active account.

src/account/logoutActions.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
/* @flow strict-local */
22
import * as NavigationService from '../nav/NavigationService';
3-
import type { AllAccountsAction, ThunkAction } from '../types';
4-
import { LOGOUT } from '../actionConstants';
3+
import type { PerAccountAction, AllAccountsAction, ThunkAction } from '../types';
4+
import { LOGOUT, RESET_ACCOUNT_DATA } from '../actionConstants';
55
import { resetToAccountPicker } from '../nav/navActions';
66

7+
/**
8+
* Reset per-account server data and some client-side data (drafts/outbox).
9+
*/
10+
// In this file just to prevent import cycles.
11+
export const resetAccountData = (): PerAccountAction => ({
12+
type: RESET_ACCOUNT_DATA,
13+
});
14+
715
const logoutPlain = (): AllAccountsAction => ({
816
type: LOGOUT,
917
});
@@ -14,5 +22,6 @@ const logoutPlain = (): AllAccountsAction => ({
1422
// In its own file just to prevent import cycles.
1523
export const logout = (): ThunkAction<Promise<void>> => async (dispatch, getState) => {
1624
NavigationService.dispatch(resetToAccountPicker());
25+
dispatch(resetAccountData());
1726
dispatch(logoutPlain());
1827
};

src/actionConstants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export const REGISTER_START: 'REGISTER_START' = 'REGISTER_START';
1515
export const REGISTER_ABORT: 'REGISTER_ABORT' = 'REGISTER_ABORT';
1616
export const REGISTER_COMPLETE: 'REGISTER_COMPLETE' = 'REGISTER_COMPLETE';
1717

18+
export const RESET_ACCOUNT_DATA: 'RESET_ACCOUNT_DATA' = 'RESET_ACCOUNT_DATA';
19+
1820
export const REFRESH_SERVER_EMOJI_DATA: 'REFRESH_SERVER_EMOJI_DATA' = 'REFRESH_SERVER_EMOJI_DATA';
1921

2022
export const DISMISS_SERVER_PUSH_SETUP_NOTICE: 'DISMISS_SERVER_PUSH_SETUP_NOTICE' =

src/actionTypes.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ACCOUNT_REMOVE,
1111
LOGIN_SUCCESS,
1212
LOGOUT,
13+
RESET_ACCOUNT_DATA,
1314
DISMISS_SERVER_PUSH_SETUP_NOTICE,
1415
GOT_PUSH_TOKEN,
1516
UNACK_PUSH_TOKEN,
@@ -170,6 +171,10 @@ type LogoutAction = $ReadOnly<{|
170171
type: typeof LOGOUT,
171172
|}>;
172173

174+
export type ResetAccountDataAction = $ReadOnly<{|
175+
type: typeof RESET_ACCOUNT_DATA,
176+
|}>;
177+
173178
type DismissServerPushSetupNoticeAction = $ReadOnly<{|
174179
type: typeof DISMISS_SERVER_PUSH_SETUP_NOTICE,
175180
date: Date,
@@ -646,6 +651,7 @@ export type PerAccountAction =
646651
// The grouping here is completely arbitrary; don't worry about it.
647652
| EventAction
648653
| LoadingAction
654+
| ResetAccountDataAction
649655
| RefreshServerEmojiDataAction
650656
| MessageAction
651657
| OutboxAction
@@ -739,6 +745,7 @@ export type DispatchableWithoutAccountAction =
739745
/** True just if the action is a PerAccountApplicableAction. */
740746
export function isPerAccountApplicableAction(action: Action): boolean {
741747
switch (action.type) {
748+
case RESET_ACCOUNT_DATA:
742749
case EVENT:
743750
case EVENT_ALERT_WORDS:
744751
case EVENT_MESSAGE_DELETE:

src/alertWords/alertWordsReducer.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
/* @flow strict-local */
22
import type { AlertWordsState, PerAccountApplicableAction } from '../types';
3-
import {
4-
REGISTER_COMPLETE,
5-
EVENT_ALERT_WORDS,
6-
ACCOUNT_SWITCH,
7-
LOGOUT,
8-
LOGIN_SUCCESS,
9-
} from '../actionConstants';
3+
import { REGISTER_COMPLETE, EVENT_ALERT_WORDS, RESET_ACCOUNT_DATA } from '../actionConstants';
104
import { NULL_ARRAY } from '../nullObjects';
115

126
const initialState = NULL_ARRAY;
@@ -16,9 +10,7 @@ export default (
1610
action: PerAccountApplicableAction,
1711
): AlertWordsState => {
1812
switch (action.type) {
19-
case LOGOUT:
20-
case ACCOUNT_SWITCH:
21-
case LOGIN_SUCCESS:
13+
case RESET_ACCOUNT_DATA:
2214
return initialState;
2315

2416
case REGISTER_COMPLETE:

src/caughtup/caughtUpReducer.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
import type { CaughtUpState, PerAccountApplicableAction } from '../types';
33
import {
44
REGISTER_COMPLETE,
5-
LOGOUT,
6-
LOGIN_SUCCESS,
7-
ACCOUNT_SWITCH,
85
MESSAGE_FETCH_START,
96
MESSAGE_FETCH_ERROR,
107
MESSAGE_FETCH_COMPLETE,
118
EVENT_UPDATE_MESSAGE,
129
EVENT_UPDATE_MESSAGE_FLAGS,
10+
RESET_ACCOUNT_DATA,
1311
} from '../actionConstants';
1412
import { NULL_OBJECT } from '../nullObjects';
1513
import { DEFAULT_CAUGHTUP } from './caughtUpSelectors';
@@ -35,9 +33,7 @@ export default (
3533
): CaughtUpState => {
3634
switch (action.type) {
3735
case REGISTER_COMPLETE:
38-
case LOGOUT:
39-
case LOGIN_SUCCESS:
40-
case ACCOUNT_SWITCH:
36+
case RESET_ACCOUNT_DATA:
4137
return initialState;
4238

4339
case MESSAGE_FETCH_START: {

src/chat/__tests__/flagsReducer-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import deepFreeze from 'deep-freeze';
44

55
import * as eg from '../../__tests__/lib/exampleData';
66
import flagsReducer from '../flagsReducer';
7-
import { EVENT_UPDATE_MESSAGE_FLAGS, ACCOUNT_SWITCH } from '../../actionConstants';
7+
import { EVENT_UPDATE_MESSAGE_FLAGS } from '../../actionConstants';
88

99
describe('flagsReducer', () => {
1010
describe('MESSAGE_FETCH_COMPLETE', () => {
@@ -270,7 +270,7 @@ describe('flagsReducer', () => {
270270
});
271271
});
272272

273-
describe('ACCOUNT_SWITCH', () => {
273+
describe('RESET_ACCOUNT_DATA', () => {
274274
test('resets to initial state', () => {
275275
const message = eg.streamMessage();
276276

@@ -284,7 +284,7 @@ describe('flagsReducer', () => {
284284
historical: { [message.id]: true },
285285
});
286286

287-
expect(flagsReducer(prevState, deepFreeze({ type: ACCOUNT_SWITCH, index: 2 }))).toEqual(
287+
expect(flagsReducer(prevState, eg.action.reset_account_data)).toEqual(
288288
eg.baseReduxState.flags,
289289
);
290290
});

src/chat/fetchingReducer.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
/* @flow strict-local */
22
import type { FetchingState, PerAccountApplicableAction } from '../types';
33
import {
4-
LOGOUT,
5-
LOGIN_SUCCESS,
6-
ACCOUNT_SWITCH,
74
MESSAGE_FETCH_START,
85
MESSAGE_FETCH_ERROR,
96
MESSAGE_FETCH_COMPLETE,
7+
RESET_ACCOUNT_DATA,
108
} from '../actionConstants';
119
import { NULL_OBJECT } from '../nullObjects';
1210
import { DEFAULT_FETCHING } from './fetchingSelectors';
@@ -68,9 +66,7 @@ export default (
6866
action: PerAccountApplicableAction,
6967
): FetchingState => {
7068
switch (action.type) {
71-
case LOGOUT:
72-
case LOGIN_SUCCESS:
73-
case ACCOUNT_SWITCH:
69+
case RESET_ACCOUNT_DATA:
7470
return initialState;
7571

7672
case MESSAGE_FETCH_START:

src/chat/flagsReducer.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import {
88
MESSAGE_FETCH_COMPLETE,
99
EVENT_NEW_MESSAGE,
1010
EVENT_UPDATE_MESSAGE_FLAGS,
11-
LOGOUT,
12-
LOGIN_SUCCESS,
13-
ACCOUNT_SWITCH,
11+
RESET_ACCOUNT_DATA,
1412
} from '../actionConstants';
1513
import { deeperMerge } from '../utils/misc';
1614
import type { UserMessageFlag } from '../api/modelTypes';
@@ -134,9 +132,7 @@ export default (
134132
): FlagsState => {
135133
switch (action.type) {
136134
case REGISTER_COMPLETE:
137-
case LOGOUT:
138-
case LOGIN_SUCCESS:
139-
case ACCOUNT_SWITCH:
135+
case RESET_ACCOUNT_DATA:
140136
return initialState;
141137

142138
case MESSAGE_FETCH_COMPLETE:

src/chat/narrowsReducer.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@ import type { NarrowsState, PerAccountApplicableAction } from '../types';
77
import { ensureUnreachable } from '../types';
88
import {
99
REGISTER_COMPLETE,
10-
LOGOUT,
11-
LOGIN_SUCCESS,
12-
ACCOUNT_SWITCH,
1310
MESSAGE_FETCH_START,
1411
MESSAGE_FETCH_ERROR,
1512
MESSAGE_FETCH_COMPLETE,
1613
EVENT_NEW_MESSAGE,
1714
EVENT_MESSAGE_DELETE,
1815
EVENT_UPDATE_MESSAGE_FLAGS,
1916
EVENT_UPDATE_MESSAGE,
17+
RESET_ACCOUNT_DATA,
2018
} from '../actionConstants';
2119
import { LAST_MESSAGE_ANCHOR, FIRST_UNREAD_ANCHOR } from '../anchor';
2220
import {
@@ -197,9 +195,7 @@ export default (
197195
): NarrowsState => {
198196
switch (action.type) {
199197
case REGISTER_COMPLETE:
200-
case LOGOUT:
201-
case LOGIN_SUCCESS:
202-
case ACCOUNT_SWITCH:
198+
case RESET_ACCOUNT_DATA:
203199
return initialState;
204200

205201
case MESSAGE_FETCH_START: {

src/drafts/draftsReducer.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @flow strict-local */
22
import type { DraftsState, PerAccountApplicableAction } from '../types';
3-
import { DRAFT_UPDATE, LOGOUT, ACCOUNT_SWITCH, LOGIN_SUCCESS } from '../actionConstants';
3+
import { DRAFT_UPDATE, RESET_ACCOUNT_DATA } from '../actionConstants';
44
import { NULL_OBJECT } from '../nullObjects';
55
import { keyFromNarrow } from '../utils/narrow';
66

@@ -27,9 +27,7 @@ export default (
2727
action: PerAccountApplicableAction,
2828
): DraftsState => {
2929
switch (action.type) {
30-
case LOGOUT:
31-
case ACCOUNT_SWITCH:
32-
case LOGIN_SUCCESS:
30+
case RESET_ACCOUNT_DATA:
3331
return initialState;
3432

3533
case DRAFT_UPDATE:

src/message/messagesReducer.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@ import type {
1212
} from '../types';
1313
import {
1414
REGISTER_COMPLETE,
15-
LOGOUT,
16-
LOGIN_SUCCESS,
17-
ACCOUNT_SWITCH,
1815
MESSAGE_FETCH_COMPLETE,
1916
EVENT_NEW_MESSAGE,
2017
EVENT_SUBMESSAGE,
2118
EVENT_MESSAGE_DELETE,
2219
EVENT_REACTION_ADD,
2320
EVENT_REACTION_REMOVE,
2421
EVENT_UPDATE_MESSAGE,
22+
RESET_ACCOUNT_DATA,
2523
} from '../actionConstants';
2624
import { getNarrowsForMessage } from '../utils/narrow';
2725
import * as logging from '../utils/logging';
@@ -142,9 +140,7 @@ export default (
142140
): MessagesState => {
143141
switch (action.type) {
144142
case REGISTER_COMPLETE:
145-
case LOGOUT:
146-
case LOGIN_SUCCESS:
147-
case ACCOUNT_SWITCH:
143+
case RESET_ACCOUNT_DATA:
148144
return initialState;
149145

150146
case MESSAGE_FETCH_COMPLETE:

src/mute/__tests__/muteModel-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ describe('reducer', () => {
3838
});
3939
});
4040

41-
describe('ACCOUNT_SWITCH', () => {
41+
describe('RESET_ACCOUNT_DATA', () => {
4242
test('resets state to initial state', () => {
4343
const state = makeMuteState([[eg.stream, 'some_topic']]);
44-
expect(reducer(state, eg.action.account_switch, eg.plusReduxState)).toEqual(initialState);
44+
expect(reducer(state, eg.action.reset_account_data, eg.plusReduxState)).toEqual(initialState);
4545
});
4646
});
4747

src/mute/__tests__/mutedUsersReducer-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ describe('mutedUsersReducer', () => {
2424
});
2525
});
2626

27-
describe('ACCOUNT_SWITCH', () => {
27+
describe('RESET_ACCOUNT_DATA', () => {
2828
test('resets state to initial state', () => {
29-
expect(mutedUsersReducer(baseState, eg.action.account_switch)).toEqual(Immutable.Map());
29+
expect(mutedUsersReducer(baseState, eg.action.reset_account_data)).toEqual(Immutable.Map());
3030
});
3131
});
3232

src/mute/muteModel.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
/* @flow strict-local */
22

33
import type { MuteState, PerAccountApplicableAction, PerAccountState } from '../types';
4-
import {
5-
REGISTER_COMPLETE,
6-
LOGOUT,
7-
ACCOUNT_SWITCH,
8-
EVENT_MUTED_TOPICS,
9-
LOGIN_SUCCESS,
10-
} from '../actionConstants';
4+
import { REGISTER_COMPLETE, EVENT_MUTED_TOPICS, RESET_ACCOUNT_DATA } from '../actionConstants';
115
import { getStreamsByName } from '../subscriptions/subscriptionSelectors';
126
import * as logging from '../utils/logging';
137
import DefaultMap from '../utils/DefaultMap';
@@ -53,9 +47,7 @@ export const reducer = (
5347
globalState: PerAccountState,
5448
): MuteState => {
5549
switch (action.type) {
56-
case LOGOUT:
57-
case ACCOUNT_SWITCH:
58-
case LOGIN_SUCCESS:
50+
case RESET_ACCOUNT_DATA:
5951
return initialState;
6052

6153
case REGISTER_COMPLETE:

0 commit comments

Comments
 (0)