Skip to content

Commit 443e2bf

Browse files
committed
redux: Use type inference in reducers.
Cutting these explicit type annotations makes our reducer modules a lot shorter and simpler to read, which I'm happy about. A crucial ingredient in making this work is that Flow now understands this code, following our recent fixes to reducer and action types: 7ea0b89 redux: Fix thoroughly-bogus type signatures on most reducers. ea2b119 actions: Make EventAction complete. e6868d8 actions: Clarify to Flow that these constants are type-tags. ... and some neighboring commits. In particular, this change means zero loss of type coverage. Both before and after, `yarn test:flow-coverage` shows: Percent Total Covered Uncovered 94 % 28871 27173 1698 --- To perform this refactor automatically, I wrote the following commands: perl -i -0pe ' s{^ const \s+ \w+ \s* = \s* \( \K \s* state (: \s* \w+)? , \s* action (: \s* [^)]+)? ,? \s* \)(: \s* \w+State)? } {state, action)}xmsg; s{^ ( import\ type\ .*? ) (?: ,? \s+ \w+Action )+ \b ( [^\}]* \}\ from\ .\. ) } {$1$2}xmsg ' \ src/*/*Reducer*.js tools/fmt
1 parent e32b952 commit 443e2bf

26 files changed

+139
-459
lines changed

src/account/accountsReducers.js

+8-22
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,12 @@ import {
99
ACCOUNT_REMOVE,
1010
} from '../actionConstants';
1111

12-
import type {
13-
AccountsState,
14-
Identity,
15-
Action,
16-
RealmAddAction,
17-
AccountSwitchAction,
18-
AccountRemoveAction,
19-
AckPushTokenAction,
20-
UnackPushTokenAction,
21-
LoginSuccessAction,
22-
LogoutAction,
23-
} from '../types';
12+
import type { AccountsState, Identity, Action } from '../types';
2413
import { NULL_ARRAY } from '../nullObjects';
2514

2615
const initialState = NULL_ARRAY;
2716

28-
const realmAdd = (state: AccountsState, action: RealmAddAction): AccountsState => {
17+
const realmAdd = (state, action) => {
2918
const accountIndex = state.findIndex(account => account.realm === action.realm);
3019

3120
if (accountIndex !== -1) {
@@ -43,7 +32,7 @@ const realmAdd = (state: AccountsState, action: RealmAddAction): AccountsState =
4332
];
4433
};
4534

46-
const accountSwitch = (state: AccountsState, action: AccountSwitchAction): AccountsState => {
35+
const accountSwitch = (state, action) => {
4736
if (action.index === 0) {
4837
return state;
4938
}
@@ -58,7 +47,7 @@ const findAccount = (state: AccountsState, identity: Identity): number => {
5847
);
5948
};
6049

61-
const loginSuccess = (state: AccountsState, action: LoginSuccessAction): AccountsState => {
50+
const loginSuccess = (state, action) => {
6251
const { realm, email, apiKey } = action;
6352
const accountIndex = findAccount(state, { realm, email });
6453
if (accountIndex === -1) {
@@ -71,7 +60,7 @@ const loginSuccess = (state: AccountsState, action: LoginSuccessAction): Account
7160
];
7261
};
7362

74-
const ackPushToken = (state: AccountsState, action: AckPushTokenAction): AccountsState => {
63+
const ackPushToken = (state, action) => {
7564
const { pushToken: ackedPushToken, identity } = action;
7665
const accountIndex = findAccount(state, identity);
7766
if (accountIndex === -1) {
@@ -84,7 +73,7 @@ const ackPushToken = (state: AccountsState, action: AckPushTokenAction): Account
8473
];
8574
};
8675

87-
const unackPushToken = (state: AccountsState, action: UnackPushTokenAction): AccountsState => {
76+
const unackPushToken = (state, action) => {
8877
const { identity } = action;
8978
const accountIndex = findAccount(state, identity);
9079
if (accountIndex === -1) {
@@ -97,12 +86,9 @@ const unackPushToken = (state: AccountsState, action: UnackPushTokenAction): Acc
9786
];
9887
};
9988

100-
const logout = (state: AccountsState, action: LogoutAction): AccountsState => [
101-
{ ...state[0], apiKey: '' },
102-
...state.slice(1),
103-
];
89+
const logout = (state, action) => [{ ...state[0], apiKey: '' }, ...state.slice(1)];
10490

105-
const accountRemove = (state: AccountsState, action: AccountRemoveAction): AccountsState => {
91+
const accountRemove = (state, action) => {
10692
const newState = state.slice();
10793
newState.splice(action.index, 1);
10894
return newState;

src/alertWords/alertWordsReducer.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
/* @flow strict-local */
2-
import type { AlertWordsState, Action, RealmInitAction, EventAlertWordsAction } from '../types';
2+
import type { AlertWordsState, Action } from '../types';
33
import { REALM_INIT, INIT_ALERT_WORDS } from '../actionConstants';
44
import { NULL_ARRAY } from '../nullObjects';
55

66
const initialState = NULL_ARRAY;
77

8-
const realmInit = (state: AlertWordsState, action: RealmInitAction): AlertWordsState =>
9-
action.data.alert_words || initialState;
8+
const realmInit = (state, action) => action.data.alert_words || initialState;
109

11-
const initAlertWords = (state: AlertWordsState, action: EventAlertWordsAction): AlertWordsState =>
12-
action.alertWords || initialState;
10+
const initAlertWords = (state, action) => action.alertWords || initialState;
1311

1412
export default (state: AlertWordsState = initialState, action: Action): AlertWordsState => {
1513
switch (action.type) {

src/caughtup/caughtUpReducers.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* @flow strict-local */
2-
import type { CaughtUpState, Action, MessageFetchCompleteAction } from '../types';
2+
import type { CaughtUpState, Action } from '../types';
33
import {
44
DEAD_QUEUE,
55
LOGOUT,
@@ -12,10 +12,7 @@ import { NULL_CAUGHTUP, NULL_OBJECT } from '../nullObjects';
1212

1313
const initialState: CaughtUpState = NULL_OBJECT;
1414

15-
const messageFetchComplete = (
16-
state: CaughtUpState,
17-
action: MessageFetchCompleteAction,
18-
): CaughtUpState => {
15+
const messageFetchComplete = (state, action) => {
1916
const key = JSON.stringify(action.narrow);
2017

2118
if (action.anchor === LAST_MESSAGE_ANCHOR) {

src/chat/fetchingReducers.js

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
/* @flow strict-local */
2-
import type {
3-
FetchingState,
4-
Action,
5-
MessageFetchStartAction,
6-
MessageFetchCompleteAction,
7-
} from '../types';
2+
import type { FetchingState, Action } from '../types';
83
import {
94
DEAD_QUEUE,
105
LOGOUT,
@@ -18,10 +13,7 @@ import { NULL_FETCHING, NULL_OBJECT } from '../nullObjects';
1813

1914
const initialState: FetchingState = NULL_OBJECT;
2015

21-
const messageFetchStart = (
22-
state: FetchingState,
23-
action: MessageFetchStartAction,
24-
): FetchingState => {
16+
const messageFetchStart = (state, action) => {
2517
const key = JSON.stringify(action.narrow);
2618
const currentValue = state[key] || NULL_FETCHING;
2719

@@ -34,10 +26,7 @@ const messageFetchStart = (
3426
};
3527
};
3628

37-
const messageFetchComplete = (
38-
state: FetchingState,
39-
action: MessageFetchCompleteAction,
40-
): FetchingState => {
29+
const messageFetchComplete = (state, action) => {
4130
const key = JSON.stringify(action.narrow);
4231
const currentValue = state[key] || NULL_FETCHING;
4332

src/chat/flagsReducers.js

+5-18
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
/* @flow strict-local */
2-
import type {
3-
Action,
4-
FlagsState,
5-
Message,
6-
MessageFetchCompleteAction,
7-
EventNewMessageAction,
8-
EventUpdateMessageFlagsAction,
9-
MarkMessagesReadAction,
10-
} from '../types';
2+
import type { Action, FlagsState, Message } from '../types';
113
import {
124
DEAD_QUEUE,
135
MESSAGE_FETCH_COMPLETE,
@@ -88,16 +80,12 @@ const processFlagsForMessages = (state: FlagsState, messages: Message[]): FlagsS
8880
return stateChanged ? deeperMerge(state, newState) : state;
8981
};
9082

91-
const messageFetchComplete = (state: FlagsState, action: MessageFetchCompleteAction): FlagsState =>
92-
processFlagsForMessages(state, action.messages);
83+
const messageFetchComplete = (state, action) => processFlagsForMessages(state, action.messages);
9384

94-
const eventNewMessage = (state: FlagsState, action: EventNewMessageAction): FlagsState =>
85+
const eventNewMessage = (state, action) =>
9586
addFlagsForMessages(state, [action.message.id], action.message.flags);
9687

97-
const eventUpdateMessageFlags = (
98-
state: FlagsState,
99-
action: EventUpdateMessageFlagsAction,
100-
): FlagsState => {
88+
const eventUpdateMessageFlags = (state, action) => {
10189
if (action.all) {
10290
return addFlagsForMessages(initialState, Object.keys(action.allMessages).map(Number), ['read']);
10391
}
@@ -113,8 +101,7 @@ const eventUpdateMessageFlags = (
113101
return state;
114102
};
115103

116-
const markMessagesRead = (state: FlagsState, action: MarkMessagesReadAction): FlagsState =>
117-
addFlagsForMessages(state, action.messageIds, ['read']);
104+
const markMessagesRead = (state, action) => addFlagsForMessages(state, action.messageIds, ['read']);
118105

119106
export default (state: FlagsState = initialState, action: Action): FlagsState => {
120107
switch (action.type) {

src/chat/narrowsReducers.js

+5-21
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
/* @flow strict-local */
22
import union from 'lodash.union';
33

4-
import type {
5-
NarrowsState,
6-
Action,
7-
MessageFetchCompleteAction,
8-
EventNewMessageAction,
9-
EventMessageDeleteAction,
10-
EventUpdateMessageFlagsAction,
11-
} from '../types';
4+
import type { NarrowsState, Action } from '../types';
125
import {
136
DEAD_QUEUE,
147
LOGOUT,
@@ -25,10 +18,7 @@ import { NULL_OBJECT } from '../nullObjects';
2518

2619
const initialState: NarrowsState = NULL_OBJECT;
2720

28-
const messageFetchComplete = (
29-
state: NarrowsState,
30-
action: MessageFetchCompleteAction,
31-
): NarrowsState => {
21+
const messageFetchComplete = (state, action) => {
3222
const key = JSON.stringify(action.narrow);
3323
const fetchedMessageIds = action.messages.map(message => message.id);
3424
const replaceExisting =
@@ -41,7 +31,7 @@ const messageFetchComplete = (
4131
};
4232
};
4333

44-
const eventNewMessage = (state: NarrowsState, action: EventNewMessageAction): NarrowsState => {
34+
const eventNewMessage = (state, action) => {
4535
let stateChange = false;
4636
const newState = Object.keys(state).reduce((msg, key) => {
4737
const isInNarrow = isMessageInNarrow(action.message, JSON.parse(key), action.ownEmail);
@@ -59,10 +49,7 @@ const eventNewMessage = (state: NarrowsState, action: EventNewMessageAction): Na
5949
return stateChange ? newState : state;
6050
};
6151

62-
const eventMessageDelete = (
63-
state: NarrowsState,
64-
action: EventMessageDeleteAction,
65-
): NarrowsState => {
52+
const eventMessageDelete = (state, action) => {
6653
let stateChange = false;
6754
const newState = Object.keys(state).reduce((updatedState, key) => {
6855
updatedState[key] = state[key].filter(id => id !== action.messageId);
@@ -72,10 +59,7 @@ const eventMessageDelete = (
7259
return stateChange ? newState : state;
7360
};
7461

75-
const eventUpdateMessageFlags = (
76-
state: NarrowsState,
77-
action: EventUpdateMessageFlagsAction,
78-
): NarrowsState => {
62+
const eventUpdateMessageFlags = (state, action) => {
7963
const { messages, flag, operation } = action;
8064
const messagesSet = new Set(messages);
8165
const updates = [];

src/drafts/draftsReducers.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/* @flow strict-local */
2-
import type { DraftState, Action, DraftUpdateAction } from '../types';
2+
import type { DraftState, Action } from '../types';
33
import { DRAFT_UPDATE, LOGOUT } from '../actionConstants';
44
import { NULL_OBJECT } from '../nullObjects';
55

66
const initialState = NULL_OBJECT;
77

8-
const draftUpdate = (state: DraftState, action: DraftUpdateAction): DraftState => {
8+
const draftUpdate = (state, action) => {
99
const narrowStr = JSON.stringify(action.narrow);
1010

1111
if (action.content.trim().length === 0) {

src/loading/loadingReducers.js

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
/* @flow strict-local */
2-
import type {
3-
LoadingState,
4-
Action,
5-
InitialFetchStartAction,
6-
InitialFetchCompleteAction,
7-
InitStreamsAction,
8-
InitSubscriptionsAction,
9-
} from '../types';
2+
import type { LoadingState, Action } from '../types';
103
import {
114
DEAD_QUEUE,
125
ACCOUNT_SWITCH,
@@ -24,31 +17,28 @@ const initialState: LoadingState = {
2417
users: false,
2518
};
2619

27-
const initialFetchStart = (state: LoadingState, action: InitialFetchStartAction): LoadingState => ({
20+
const initialFetchStart = (state, action) => ({
2821
...state,
2922
presence: true,
3023
subscriptions: true,
3124
unread: true,
3225
users: true,
3326
});
3427

35-
const initialFetchComplete = (
36-
state: LoadingState,
37-
action: InitialFetchCompleteAction,
38-
): LoadingState => ({
28+
const initialFetchComplete = (state, action) => ({
3929
...state,
4030
presence: false,
4131
subscriptions: false,
4232
unread: false,
4333
users: false,
4434
});
4535

46-
const initStreams = (state: LoadingState, action: InitStreamsAction): LoadingState => ({
36+
const initStreams = (state, action) => ({
4737
...state,
4838
streams: false,
4939
});
5040

51-
const initSubscriptions = (state: LoadingState, action: InitSubscriptionsAction): LoadingState => ({
41+
const initSubscriptions = (state, action) => ({
5242
...state,
5343
subscriptions: false,
5444
});

0 commit comments

Comments
 (0)