Skip to content

Commit

Permalink
session: Clear all PerAccountSessionState when leaving account
Browse files Browse the repository at this point in the history
Really an instance of zulip#4446 that I forgot about in zulip#5606, oops.

Related: zulip#4446
  • Loading branch information
chrisbobbe committed Dec 14, 2022
1 parent 101569f commit 361c288
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/session/__tests__/sessionReducer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
DISMISS_SERVER_COMPAT_NOTICE,
REGISTER_START,
} from '../../actionConstants';
import sessionReducer from '../sessionReducer';
import sessionReducer, { initialPerAccountSessionState } from '../sessionReducer';
import * as eg from '../../__tests__/lib/exampleData';

describe('sessionReducer', () => {
Expand All @@ -25,12 +25,12 @@ describe('sessionReducer', () => {
loading: true,
});
const newState = sessionReducer(state, eg.action.account_switch);
expect(newState).toEqual({ ...baseState, loading: false });
expect(newState).toEqual({ ...baseState, ...initialPerAccountSessionState });
});

test('LOGIN_SUCCESS', () => {
const newState = sessionReducer(baseState, eg.action.login_success);
expect(newState).toEqual(baseState);
expect(newState).toEqual({ ...baseState, ...initialPerAccountSessionState });
});

test('DEAD_QUEUE', () => {
Expand All @@ -47,7 +47,7 @@ describe('sessionReducer', () => {
const newState = sessionReducer(state, deepFreeze({ type: LOGOUT }));
expect(newState).toEqual({
...baseState,
loading: false,
...initialPerAccountSessionState,
});
});

Expand Down
25 changes: 20 additions & 5 deletions src/session/sessionReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,30 @@ export type SessionState = $ReadOnly<{|
// PerAccountSessionState is inexact.)
(s: SessionState): PerAccountSessionState => s; // eslint-disable-line no-unused-expressions

const initialState: SessionState = {
eventQueueId: null,

const initialGlobalSessionState: $Exact<GlobalSessionState> = {
// This will be `null` on startup, while we wait to hear `true` or `false`
// from the native module over the RN bridge; so, have it start as `null`.
isOnline: null,

isHydrated: false,
loading: false,
orientation: 'PORTRAIT',
outboxSending: false,
pushToken: null,
debug: Object.freeze({}),
};

/** PRIVATE; exported only for tests. */
export const initialPerAccountSessionState: $Exact<PerAccountSessionState> = {
eventQueueId: null,
loading: false,
outboxSending: false,
hasDismissedServerCompatNotice: false,
};

const initialState: SessionState = {
...initialGlobalSessionState,
...initialPerAccountSessionState,
};

// eslint-disable-next-line default-param-last
export default (state: SessionState = initialState, action: Action): SessionState => {
switch (action.type) {
Expand All @@ -149,6 +157,9 @@ export default (state: SessionState = initialState, action: Action): SessionStat
case LOGIN_SUCCESS:
return {
...state,
loading: false,
outboxSending: false,
hasDismissedServerCompatNotice: false,

// We're about to request a new event queue; no use hanging on to
// any old one we might have.
Expand All @@ -159,6 +170,8 @@ export default (state: SessionState = initialState, action: Action): SessionStat
return {
...state,
loading: false,
outboxSending: false,
hasDismissedServerCompatNotice: false,

// Stop polling this event queue.
eventQueueId: null,
Expand All @@ -168,6 +181,8 @@ export default (state: SessionState = initialState, action: Action): SessionStat
return {
...state,
loading: false,
outboxSending: false,
hasDismissedServerCompatNotice: false,

// Stop polling this event queue. (We'll request a new one soon,
// for the new account.)
Expand Down

0 comments on commit 361c288

Please sign in to comment.