Skip to content

Commit d371a7b

Browse files
ClearlyClairehiyuki2578
authored andcommitted
Memoize ancestorIds and descendantIds in detailed status view (mastodon#11234)
1 parent f64bdd3 commit d371a7b

1 file changed

Lines changed: 44 additions & 24 deletions

File tree

  • app/javascript/mastodon/features/status

app/javascript/mastodon/features/status/index.js

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { connect } from 'react-redux';
44
import PropTypes from 'prop-types';
55
import classNames from 'classnames';
66
import ImmutablePropTypes from 'react-immutable-proptypes';
7+
import { createSelector } from 'reselect';
78
import { fetchStatus } from '../../actions/statuses';
89
import MissingIndicator from '../../components/missing_indicator';
910
import DetailedStatus from './components/detailed_status';
@@ -63,39 +64,58 @@ const messages = defineMessages({
6364
const makeMapStateToProps = () => {
6465
const getStatus = makeGetStatus();
6566

66-
const mapStateToProps = (state, props) => {
67-
const status = getStatus(state, { id: props.params.statusId });
67+
const getAncestorsIds = createSelector([
68+
(_, { id }) => id,
69+
state => state.getIn(['contexts', 'inReplyTos']),
70+
], (statusId, inReplyTos) => {
6871
let ancestorsIds = Immutable.List();
72+
ancestorsIds = ancestorsIds.withMutations(mutable => {
73+
let id = statusId;
74+
75+
while (id) {
76+
mutable.unshift(id);
77+
id = inReplyTos.get(id);
78+
}
79+
});
80+
81+
return ancestorsIds;
82+
});
83+
84+
const getDescendantsIds = createSelector([
85+
(_, { id }) => id,
86+
state => state.getIn(['contexts', 'replies']),
87+
], (statusId, contextReplies) => {
6988
let descendantsIds = Immutable.List();
89+
descendantsIds = descendantsIds.withMutations(mutable => {
90+
const ids = [statusId];
7091

71-
if (status) {
72-
ancestorsIds = ancestorsIds.withMutations(mutable => {
73-
let id = status.get('in_reply_to_id');
92+
while (ids.length > 0) {
93+
let id = ids.shift();
94+
const replies = contextReplies.get(id);
7495

75-
while (id) {
76-
mutable.unshift(id);
77-
id = state.getIn(['contexts', 'inReplyTos', id]);
96+
if (statusId !== id) {
97+
mutable.push(id);
7898
}
79-
});
8099

81-
descendantsIds = descendantsIds.withMutations(mutable => {
82-
const ids = [status.get('id')];
100+
if (replies) {
101+
replies.reverse().forEach(reply => {
102+
ids.unshift(reply);
103+
});
104+
}
105+
}
106+
});
83107

84-
while (ids.length > 0) {
85-
let id = ids.shift();
86-
const replies = state.getIn(['contexts', 'replies', id]);
108+
return descendantsIds;
109+
});
87110

88-
if (status.get('id') !== id) {
89-
mutable.push(id);
90-
}
111+
const mapStateToProps = (state, props) => {
112+
const status = getStatus(state, { id: props.params.statusId });
113+
let ancestorsIds = Immutable.List();
114+
let descendantsIds = Immutable.List();
91115

92-
if (replies) {
93-
replies.reverse().forEach(reply => {
94-
ids.unshift(reply);
95-
});
96-
}
97-
}
98-
});
116+
if (status) {
117+
ancestorsIds = getAncestorsIds(state, { id: status.get('in_reply_to_id') });
118+
descendantsIds = getDescendantsIds(state, { id: status.get('id') });
99119
}
100120

101121
return {

0 commit comments

Comments
 (0)