Skip to content

Commit

Permalink
redux [nfc]: Simplify combined reducer (6/n): unroll the main loop.
Browse files Browse the repository at this point in the history
The list of our reducers is already right here in the same source
file, so we don't really need all this metaprogramming on it.  By
unrolling the loop and unpacking what it does, we make everything
more explicit, and in particular make it explicit to the type-checker
so that it's able to check the types and we no longer need any fixme
comments to turn it off.
  • Loading branch information
gnprice committed Jan 27, 2021
1 parent 61fc208 commit 378e1fd
Showing 1 changed file with 31 additions and 42 deletions.
73 changes: 31 additions & 42 deletions src/boot/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,6 @@ import timing from '../utils/timing';

const migrations = (state: MigrationsState = NULL_OBJECT): MigrationsState => state;

const reducers = {
migrations,
accounts,
alertWords,
caughtUp,
drafts,
fetching,
flags,
messages,
narrows,
mute,
outbox,
pmConversations,
presence,
realm,
session,
settings,
streams,
subscriptions,
topics,
typing,
unread,
userGroups,
userStatus,
users,
};

const reducerKeys = Object.keys(reducers);

const { enableReduxSlowReducerWarnings, slowReducersThreshold } = config;

function maybeLogSlowReducer(action, key, startMs, endMs) {
Expand Down Expand Up @@ -93,21 +64,39 @@ function applyReducer<Key: $Keys<GlobalState>, State>(
// Inlined just now from Redux upstream.
// We'll clean this up in the next few commits.
const combinedReducer = (state: void | GlobalState, action: Action): GlobalState => {
let hasChanged = false;
const nextState = {};
for (let i = 0; i < reducerKeys.length; i++) {
const key = reducerKeys[i];
const reducer = reducers[key];
const previousStateForKey = state?.[key];
// prettier-ignore
const nextState = {
migrations: applyReducer('migrations', migrations, state?.migrations, action),
accounts: applyReducer('accounts', accounts, state?.accounts, action),
alertWords: applyReducer('alertWords', alertWords, state?.alertWords, action),
caughtUp: applyReducer('caughtUp', caughtUp, state?.caughtUp, action),
drafts: applyReducer('drafts', drafts, state?.drafts, action),
fetching: applyReducer('fetching', fetching, state?.fetching, action),
flags: applyReducer('flags', flags, state?.flags, action),
messages: applyReducer('messages', messages, state?.messages, action),
narrows: applyReducer('narrows', narrows, state?.narrows, action),
mute: applyReducer('mute', mute, state?.mute, action),
outbox: applyReducer('outbox', outbox, state?.outbox, action),
pmConversations: applyReducer('pmConversations', pmConversations, state?.pmConversations, action),
presence: applyReducer('presence', presence, state?.presence, action),
realm: applyReducer('realm', realm, state?.realm, action),
session: applyReducer('session', session, state?.session, action),
settings: applyReducer('settings', settings, state?.settings, action),
streams: applyReducer('streams', streams, state?.streams, action),
subscriptions: applyReducer('subscriptions', subscriptions, state?.subscriptions, action),
topics: applyReducer('topics', topics, state?.topics, action),
typing: applyReducer('typing', typing, state?.typing, action),
unread: applyReducer('unread', unread, state?.unread, action),
userGroups: applyReducer('userGroups', userGroups, state?.userGroups, action),
userStatus: applyReducer('userStatus', userStatus, state?.userStatus, action),
users: applyReducer('users', users, state?.users, action),
};

// $FlowFixMe -- works because reducer and previousStateForKey are from same key
const nextStateForKey = applyReducer(key, reducer, previousStateForKey, action);

nextState[key] = nextStateForKey;
hasChanged = hasChanged || nextStateForKey !== previousStateForKey;
if (state && Object.keys(nextState).every(key => nextState[key] === state[key])) {
return state;
}
// $FlowFixMe -- works because we didn't mix up keys above
return hasChanged ? nextState : state;

return nextState;
};

export default enableBatching(combinedReducer);

0 comments on commit 378e1fd

Please sign in to comment.