Skip to content

Commit 46d453d

Browse files
committed
session [nfc]: Refactor to use RESET_ACCOUNT_DATA
Related: zulip#4446
1 parent 361c288 commit 46d453d

File tree

2 files changed

+22
-59
lines changed

2 files changed

+22
-59
lines changed

src/session/__tests__/sessionReducer-test.js

+15-24
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import deepFreeze from 'deep-freeze';
33

44
import {
55
DEAD_QUEUE,
6-
LOGOUT,
76
APP_ONLINE,
87
REGISTER_ABORT,
98
APP_ORIENTATION,
@@ -19,18 +18,22 @@ import * as eg from '../../__tests__/lib/exampleData';
1918
describe('sessionReducer', () => {
2019
const baseState = eg.baseReduxState.session;
2120

22-
test('ACCOUNT_SWITCH', () => {
23-
const state = deepFreeze({
24-
...baseState,
25-
loading: true,
26-
});
27-
const newState = sessionReducer(state, eg.action.account_switch);
28-
expect(newState).toEqual({ ...baseState, ...initialPerAccountSessionState });
29-
});
21+
describe('RESET_ACCOUNT_DATA', () => {
22+
test('resets per-account state without touching global state', () => {
23+
const prevState = [
24+
// per-account
25+
eg.action.register_complete,
26+
{ type: DISMISS_SERVER_COMPAT_NOTICE },
3027

31-
test('LOGIN_SUCCESS', () => {
32-
const newState = sessionReducer(baseState, eg.action.login_success);
33-
expect(newState).toEqual({ ...baseState, ...initialPerAccountSessionState });
28+
// global
29+
{ type: GOT_PUSH_TOKEN, pushToken: '456' },
30+
{ type: APP_ORIENTATION, orientation: 'LANDSCAPE' },
31+
].reduce(sessionReducer, eg.baseReduxState.session);
32+
expect(sessionReducer(prevState, eg.action.reset_account_data)).toEqual({
33+
...prevState,
34+
...initialPerAccountSessionState,
35+
});
36+
});
3437
});
3538

3639
test('DEAD_QUEUE', () => {
@@ -39,18 +42,6 @@ describe('sessionReducer', () => {
3942
expect(newState).toEqual({ ...baseState, loading: false });
4043
});
4144

42-
test('LOGOUT', () => {
43-
const state = deepFreeze({
44-
...baseState,
45-
loading: true,
46-
});
47-
const newState = sessionReducer(state, deepFreeze({ type: LOGOUT }));
48-
expect(newState).toEqual({
49-
...baseState,
50-
...initialPerAccountSessionState,
51-
});
52-
});
53-
5445
test('REGISTER_COMPLETE', () => {
5546
const state = deepFreeze({ ...baseState, loading: true });
5647
const action = eg.mkActionRegisterComplete({ queue_id: '100' });

src/session/sessionReducer.js

+7-35
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ import type { Debug, Orientation, Action } from '../types';
33
import {
44
REHYDRATE,
55
DEAD_QUEUE,
6-
LOGIN_SUCCESS,
6+
RESET_ACCOUNT_DATA,
77
APP_ONLINE,
8-
ACCOUNT_SWITCH,
98
REGISTER_START,
109
REGISTER_ABORT,
1110
REGISTER_COMPLETE,
1211
APP_ORIENTATION,
1312
TOGGLE_OUTBOX_SENDING,
1413
DEBUG_FLAG_TOGGLE,
1514
GOT_PUSH_TOKEN,
16-
LOGOUT,
1715
DISMISS_SERVER_COMPAT_NOTICE,
1816
} from '../actionConstants';
1917

@@ -154,41 +152,15 @@ export default (state: SessionState = initialState, action: Action): SessionStat
154152
eventQueueId: null,
155153
};
156154

157-
case LOGIN_SUCCESS:
155+
case RESET_ACCOUNT_DATA:
158156
return {
159157
...state,
160-
loading: false,
161-
outboxSending: false,
162-
hasDismissedServerCompatNotice: false,
163-
164-
// We're about to request a new event queue; no use hanging on to
165-
// any old one we might have.
166-
eventQueueId: null,
167-
};
168-
169-
case LOGOUT:
170-
return {
171-
...state,
172-
loading: false,
173-
outboxSending: false,
174-
hasDismissedServerCompatNotice: false,
175158

176-
// Stop polling this event queue.
177-
eventQueueId: null,
178-
};
179-
180-
case ACCOUNT_SWITCH:
181-
return {
182-
...state,
183-
loading: false,
184-
outboxSending: false,
185-
hasDismissedServerCompatNotice: false,
186-
187-
// Stop polling this event queue. (We'll request a new one soon,
188-
// for the new account.)
189-
// TODO(#5005): Keep polling on accounts other than the active
190-
// account.
191-
eventQueueId: null,
159+
// Clear per-account session state. Importantly, stop polling on the
160+
// account's current event queue if we had one. In the polling loop,
161+
// after each server response, we check if we've dropped the queue
162+
// ID from this state and break out if so.
163+
...initialPerAccountSessionState,
192164
};
193165

194166
case REHYDRATE:

0 commit comments

Comments
 (0)