Skip to content

Commit

Permalink
Use an Immutable Record as the root state (mastodon#25584)
Browse files Browse the repository at this point in the history
  • Loading branch information
renchap authored Jun 30, 2023
1 parent 9934949 commit 78ba12f
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion app/javascript/mastodon/reducers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Record as ImmutableRecord } from 'immutable';

import { loadingBarReducer } from 'react-redux-loading-bar';
import { combineReducers } from 'redux-immutable';

Expand Down Expand Up @@ -88,6 +90,22 @@ const reducers = {
followed_tags,
};

const rootReducer = combineReducers(reducers);
// We want the root state to be an ImmutableRecord, which is an object with a defined list of keys,
// so it is properly typed and keys can be accessed using `state.<key>` syntax.
// This will allow an easy conversion to a plain object once we no longer call `get` or `getIn` on the root state

// By default with `combineReducers` it is a Collection, so we provide our own implementation to get a Record
const initialRootState = Object.fromEntries(
Object.entries(reducers).map(([name, reducer]) => [
name,
reducer(undefined, {
// empty action
}),
])
);

const RootStateRecord = ImmutableRecord(initialRootState, 'RootState');

const rootReducer = combineReducers(reducers, RootStateRecord);

export { rootReducer };

0 comments on commit 78ba12f

Please sign in to comment.