[Glitch] Memoize ancestorIds and descendantIds in detailed status view

Port eda4094171 to glitch-soc
th-downstream
ThibG 5 years ago committed by Thibaut Girka
parent b8d7f860ca
commit 447e13fec3

@ -4,6 +4,7 @@ import { connect } from 'react-redux';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import classNames from 'classnames'; import classNames from 'classnames';
import ImmutablePropTypes from 'react-immutable-proptypes'; import ImmutablePropTypes from 'react-immutable-proptypes';
import { createSelector } from 'reselect';
import { fetchStatus } from 'flavours/glitch/actions/statuses'; import { fetchStatus } from 'flavours/glitch/actions/statuses';
import MissingIndicator from 'flavours/glitch/components/missing_indicator'; import MissingIndicator from 'flavours/glitch/components/missing_indicator';
import DetailedStatus from './components/detailed_status'; import DetailedStatus from './components/detailed_status';
@ -61,39 +62,58 @@ const messages = defineMessages({
const makeMapStateToProps = () => { const makeMapStateToProps = () => {
const getStatus = makeGetStatus(); const getStatus = makeGetStatus();
const mapStateToProps = (state, props) => { const getAncestorsIds = createSelector([
const status = getStatus(state, { id: props.params.statusId }); (_, { id }) => id,
state => state.getIn(['contexts', 'inReplyTos']),
], (statusId, inReplyTos) => {
let ancestorsIds = Immutable.List(); let ancestorsIds = Immutable.List();
ancestorsIds = ancestorsIds.withMutations(mutable => {
let id = statusId;
while (id) {
mutable.unshift(id);
id = inReplyTos.get(id);
}
});
return ancestorsIds;
});
const getDescendantsIds = createSelector([
(_, { id }) => id,
state => state.getIn(['contexts', 'replies']),
], (statusId, contextReplies) => {
let descendantsIds = Immutable.List(); let descendantsIds = Immutable.List();
descendantsIds = descendantsIds.withMutations(mutable => {
const ids = [statusId];
if (status) { while (ids.length > 0) {
ancestorsIds = ancestorsIds.withMutations(mutable => { let id = ids.shift();
let id = status.get('in_reply_to_id'); const replies = contextReplies.get(id);
while (id) { if (statusId !== id) {
mutable.unshift(id); mutable.push(id);
id = state.getIn(['contexts', 'inReplyTos', id]);
} }
});
descendantsIds = descendantsIds.withMutations(mutable => { if (replies) {
const ids = [status.get('id')]; replies.reverse().forEach(reply => {
ids.unshift(reply);
});
}
}
});
while (ids.length > 0) { return descendantsIds;
let id = ids.shift(); });
const replies = state.getIn(['contexts', 'replies', id]);
if (status.get('id') !== id) { const mapStateToProps = (state, props) => {
mutable.push(id); const status = getStatus(state, { id: props.params.statusId });
} let ancestorsIds = Immutable.List();
let descendantsIds = Immutable.List();
if (replies) { if (status) {
replies.reverse().forEach(reply => { ancestorsIds = getAncestorsIds(state, { id: status.get('in_reply_to_id') });
ids.unshift(reply); descendantsIds = getDescendantsIds(state, { id: status.get('id') });
});
}
}
});
} }
return { return {

Loading…
Cancel
Save