Skip to content

Commit

Permalink
model: Fix lots of reducers that weren't clearing data on LOGIN_SUCCESS
Browse files Browse the repository at this point in the history
Most of these state subtrees hold per-account data we get from the
server. In the current pre-zulip#5005 design, where we only store server
data for one account, these should already have been clearing out on
LOGIN_SUCCESS, like we do with messages, narrows, etc.

Two of the subtrees, `outbox` and `drafts`, hold client-side
per-account data. Just as with server data, we've only been keeping
these around for the active account, and like the other reducers
touched in this commit, we've forgotten to handle LOGIN_SUCCESS for
them. So, do that.

Now, all the server-data subtrees and `outbox` and `drafts` all
reset on ACCOUNT_SWITCH, LOGIN_SUCCESS, and LOGOUT. That's part of
Greg's proposed fix for zulip#4446.

Next, we'll do the next part:

> * 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-partly: zulip#4446
  • Loading branch information
chrisbobbe committed Dec 13, 2022
1 parent 3677788 commit 730c370
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/alertWords/alertWordsReducer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/* @flow strict-local */
import type { AlertWordsState, PerAccountApplicableAction } from '../types';
import { REGISTER_COMPLETE, EVENT_ALERT_WORDS, ACCOUNT_SWITCH, LOGOUT } from '../actionConstants';
import {
REGISTER_COMPLETE,
EVENT_ALERT_WORDS,
ACCOUNT_SWITCH,
LOGOUT,
LOGIN_SUCCESS,
} from '../actionConstants';
import { NULL_ARRAY } from '../nullObjects';

const initialState = NULL_ARRAY;
Expand All @@ -12,6 +18,7 @@ export default (
switch (action.type) {
case LOGOUT:
case ACCOUNT_SWITCH:
case LOGIN_SUCCESS:
return initialState;

case REGISTER_COMPLETE:
Expand Down
3 changes: 2 additions & 1 deletion src/drafts/draftsReducer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* @flow strict-local */
import type { DraftsState, PerAccountApplicableAction } from '../types';
import { DRAFT_UPDATE, LOGOUT, ACCOUNT_SWITCH } from '../actionConstants';
import { DRAFT_UPDATE, LOGOUT, ACCOUNT_SWITCH, LOGIN_SUCCESS } from '../actionConstants';
import { NULL_OBJECT } from '../nullObjects';
import { keyFromNarrow } from '../utils/narrow';

Expand Down Expand Up @@ -29,6 +29,7 @@ export default (
switch (action.type) {
case LOGOUT:
case ACCOUNT_SWITCH:
case LOGIN_SUCCESS:
return initialState;

case DRAFT_UPDATE:
Expand Down
9 changes: 8 additions & 1 deletion src/mute/muteModel.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/* @flow strict-local */

import type { MuteState, PerAccountApplicableAction, PerAccountState } from '../types';
import { REGISTER_COMPLETE, LOGOUT, ACCOUNT_SWITCH, EVENT_MUTED_TOPICS } from '../actionConstants';
import {
REGISTER_COMPLETE,
LOGOUT,
ACCOUNT_SWITCH,
EVENT_MUTED_TOPICS,
LOGIN_SUCCESS,
} from '../actionConstants';
import { getStreamsByName } from '../subscriptions/subscriptionSelectors';
import * as logging from '../utils/logging';
import DefaultMap from '../utils/DefaultMap';
Expand Down Expand Up @@ -49,6 +55,7 @@ export const reducer = (
switch (action.type) {
case LOGOUT:
case ACCOUNT_SWITCH:
case LOGIN_SUCCESS:
return initialState;

case REGISTER_COMPLETE:
Expand Down
2 changes: 2 additions & 0 deletions src/outbox/outboxReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ACCOUNT_SWITCH,
DELETE_OUTBOX_MESSAGE,
MESSAGE_SEND_COMPLETE,
LOGIN_SUCCESS,
} from '../actionConstants';
import { NULL_ARRAY } from '../nullObjects';
import { filterArray } from '../utils/immutability';
Expand Down Expand Up @@ -44,6 +45,7 @@ export default (

case ACCOUNT_SWITCH:
case LOGOUT:
case LOGIN_SUCCESS:
return initialState;

default:
Expand Down
9 changes: 8 additions & 1 deletion src/streams/streamsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { EventTypes } from '../api/eventTypes';
import type { PerAccountApplicableAction, StreamsState, StreamUpdateEvent } from '../types';
import type { Stream, Subscription } from '../api/modelTypes';
import { ensureUnreachable } from '../types';
import { LOGOUT, ACCOUNT_SWITCH, EVENT, REGISTER_COMPLETE } from '../actionConstants';
import {
LOGOUT,
ACCOUNT_SWITCH,
EVENT,
REGISTER_COMPLETE,
LOGIN_SUCCESS,
} from '../actionConstants';
import { NULL_ARRAY } from '../nullObjects';
import { filterArray } from '../utils/immutability';

Expand Down Expand Up @@ -61,6 +67,7 @@ export default (

case LOGOUT:
case ACCOUNT_SWITCH:
case LOGIN_SUCCESS:
return initialState;

case EVENT: {
Expand Down
9 changes: 8 additions & 1 deletion src/topics/topicsReducer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/* @flow strict-local */
import type { TopicsState, PerAccountApplicableAction } from '../types';
import { LOGOUT, ACCOUNT_SWITCH, INIT_TOPICS, EVENT_NEW_MESSAGE } from '../actionConstants';
import {
LOGOUT,
ACCOUNT_SWITCH,
INIT_TOPICS,
EVENT_NEW_MESSAGE,
LOGIN_SUCCESS,
} from '../actionConstants';
import { NULL_OBJECT } from '../nullObjects';
import { replaceItemInArray } from '../utils/immutability';

Expand Down Expand Up @@ -43,6 +49,7 @@ export default (
switch (action.type) {
case LOGOUT:
case ACCOUNT_SWITCH:
case LOGIN_SUCCESS:
return initialState;

case INIT_TOPICS:
Expand Down
2 changes: 2 additions & 0 deletions src/unread/unreadHuddlesReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
EVENT_NEW_MESSAGE,
EVENT_MESSAGE_DELETE,
EVENT_UPDATE_MESSAGE_FLAGS,
LOGIN_SUCCESS,
} from '../actionConstants';
import {
pmUnreadsKeyFromMessage,
Expand Down Expand Up @@ -93,6 +94,7 @@ export default (
switch (action.type) {
case LOGOUT:
case ACCOUNT_SWITCH:
case LOGIN_SUCCESS:
return initialState;

case REGISTER_COMPLETE:
Expand Down
2 changes: 2 additions & 0 deletions src/unread/unreadMentionsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
EVENT_NEW_MESSAGE,
EVENT_MESSAGE_DELETE,
EVENT_UPDATE_MESSAGE_FLAGS,
LOGIN_SUCCESS,
} from '../actionConstants';
import { addItemsToArray, removeItemsFromArray } from '../utils/immutability';
import { NULL_ARRAY } from '../nullObjects';
Expand Down Expand Up @@ -51,6 +52,7 @@ export default (
switch (action.type) {
case LOGOUT:
case ACCOUNT_SWITCH:
case LOGIN_SUCCESS:
return initialState;

case REGISTER_COMPLETE:
Expand Down
3 changes: 2 additions & 1 deletion src/unread/unreadModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
EVENT_NEW_MESSAGE,
EVENT_UPDATE_MESSAGE,
EVENT_UPDATE_MESSAGE_FLAGS,
LOGIN_SUCCESS,
LOGOUT,
MESSAGE_FETCH_COMPLETE,
REGISTER_COMPLETE,
Expand Down Expand Up @@ -226,7 +227,7 @@ function streamsReducer(
switch (action.type) {
case LOGOUT:
case ACCOUNT_SWITCH:
// TODO(#4446) also LOGIN_SUCCESS, presumably
case LOGIN_SUCCESS:
return initialStreamsState;

case REGISTER_COMPLETE: {
Expand Down
2 changes: 2 additions & 0 deletions src/unread/unreadPmsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
EVENT_NEW_MESSAGE,
EVENT_MESSAGE_DELETE,
EVENT_UPDATE_MESSAGE_FLAGS,
LOGIN_SUCCESS,
} from '../actionConstants';
import { addItemsToPmArray, removeItemsDeeply } from './unreadHelpers';
import { NULL_ARRAY } from '../nullObjects';
Expand Down Expand Up @@ -93,6 +94,7 @@ export default (
switch (action.type) {
case LOGOUT:
case ACCOUNT_SWITCH:
case LOGIN_SUCCESS:
return initialState;

case REGISTER_COMPLETE:
Expand Down

0 comments on commit 730c370

Please sign in to comment.