Skip to content

Commit

Permalink
subscriptionSelectors: Fix caching in "for streamId" selectors.
Browse files Browse the repository at this point in the history
part of #3015
  • Loading branch information
jainkuniya committed Feb 16, 2019
1 parent 0674ea4 commit 3a909aa
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/streams/EditStreamScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ class EditStreamScreen extends PureComponent<Props> {
}

export default connect((state: GlobalState, props) => ({
stream: getStreamFromId(props.navigation.state.params.streamId)(state),
stream: getStreamFromId(state, props.navigation.state.params.streamId),
}))(EditStreamScreen);
2 changes: 1 addition & 1 deletion src/streams/InviteUsersScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ class InviteUsersScreen extends PureComponent<Props, State> {

export default connect((state: GlobalState, props) => ({
auth: getAuth(state),
stream: getStreamFromId(props.navigation.state.params.streamId)(state),
stream: getStreamFromId(state, props.navigation.state.params.streamId),
}))(InviteUsersScreen);
4 changes: 2 additions & 2 deletions src/streams/StreamScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ class StreamScreen extends PureComponent<Props> {

export default connect((state: GlobalState, props) => ({
isAdmin: getIsAdmin(state),
stream: getStreamFromId(props.navigation.state.params.streamId)(state),
subscription: getSubscriptionFromId(props.navigation.state.params.streamId)(state),
stream: getStreamFromId(state, props.navigation.state.params.streamId),
subscription: getSubscriptionFromId(state, props.navigation.state.params.streamId),
}))(StreamScreen);
20 changes: 12 additions & 8 deletions src/subscriptions/subscriptionSelectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,24 @@ export const getSubscribedStreams: Selector<Subscription[]> = createSelector(
})),
);

export const getStreamFromId = (streamId: string): Selector<Stream> =>
createSelector([getStreams], (streams, params) => {
export const getStreamFromId: Selector<Stream, number> = createSelector(
(state, streamId) => streamId,
state => getStreams(state),
(streamId, streams, params) => {
const stream = streams.find(x => x.stream_id === streamId);
if (!stream) {
throw new Error(`getStreamFromId: missing stream: id ${streamId}`);
}
return stream;
});
},
);

export const getSubscriptionFromId = (streamId: string): Selector<Subscription> =>
createSelector(
[getSubscriptions],
subscriptions => subscriptions.find(x => x.stream_id === streamId) || NULL_SUBSCRIPTION,
);
export const getSubscriptionFromId: Selector<Subscription, number> = createSelector(
(state, streamId) => streamId,
state => getSubscriptions(state),
(streamId, subscriptions) =>
subscriptions.find(x => x.stream_id === streamId) || NULL_SUBSCRIPTION,
);

export const getIsActiveStreamAnnouncementOnly: Selector<boolean, Narrow> = createSelector(
(state, narrow) => narrow,
Expand Down
2 changes: 1 addition & 1 deletion src/topics/TopicListScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ class TopicListScreen extends PureComponent<Props, State> {
}

export default connect((state: GlobalState, props) => ({
stream: getStreamFromId(props.navigation.state.params.streamId)(state),
stream: getStreamFromId(state, props.navigation.state.params.streamId),
topics: getTopicsForStream(state, props.navigation.state.params.streamId),
}))(TopicListScreen);

0 comments on commit 3a909aa

Please sign in to comment.