Skip to content

Commit

Permalink
reduxTypes: Mark GlobalState's properties as read-only.
Browse files Browse the repository at this point in the history
We already treat them as read-only everywhere except for one case,
touched in this commit, where we've had to suppress several
different Flow errors [0]. So, mark them as read-only with
`$ReadOnly` [1].

Treating `GlobalState`'s properties as read-only (except for that
one exceptional case) is likely to be very stable: we treat them as
read-only because the Redux state is a tree of objects that are all
treated as immutable; see the Redux docs [2] and our architecture
doc [3].

[0] See Greg's comments on this at
    zulip#4709 (comment)

[1] https://flow.org/en/docs/types/utilities/#toc-readonly

[2] http://redux.js.org

[3] ./docs/architecture.md
  • Loading branch information
chrisbobbe committed May 27, 2021
1 parent 3d93aee commit a67c23e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
12 changes: 5 additions & 7 deletions src/boot/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,11 @@ export const cacheKeys: Array<$Keys<GlobalState>> = [
function dropCache(state: GlobalState): $Shape<GlobalState> {
const result: $Shape<GlobalState> = {};
storeKeys.forEach(key => {
// $FlowFixMe[incompatible-indexer]
// $FlowFixMe[incompatible-exact]
// $FlowFixMe[prop-missing]
// $FlowFixMe[incompatible-variance]
// $FlowFixMe[incompatible-type-arg]
/* $FlowFixMe[incompatible-type]
This is well-typed only because it's the same `key` twice. */
/* $FlowFixMe[cannot-write]
This is well-typed only because it's the same `key` twice. It seems
like we should have to suppress an error about not having that
guarantee, in addition to the more minor-looking `cannot-write`. Not
sure why we don't. */
result[key] = state[key];
});
return result;
Expand Down
4 changes: 2 additions & 2 deletions src/reduxTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export type UsersState = User[];
* See in particular `discardKeys`, `storeKeys`, and `cacheKeys`, which
* identify which subtrees are persisted and which are not.
*/
export type GlobalState = {|
export type GlobalState = $ReadOnly<{|
accounts: AccountsState,
alertWords: AlertWordsState,
caughtUp: CaughtUpState,
Expand All @@ -355,7 +355,7 @@ export type GlobalState = {|
userGroups: UserGroupsState,
userStatus: UserStatusState,
users: UsersState,
|};
|}>;

/** A selector returning TResult, with extra parameter TParam. */
// Seems like this should be OutputSelector... but for whatever reason,
Expand Down

0 comments on commit a67c23e

Please sign in to comment.