Skip to content

Commit

Permalink
Memoize ancestorIds and descendantIds in detailed status view (mastod…
Browse files Browse the repository at this point in the history
  • Loading branch information
ClearlyClaire authored and hiyuki2578 committed Oct 2, 2019
1 parent f64bdd3 commit d371a7b
Showing 1 changed file with 44 additions and 24 deletions.
68 changes: 44 additions & 24 deletions app/javascript/mastodon/features/status/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import ImmutablePropTypes from 'react-immutable-proptypes';
import { createSelector } from 'reselect';
import { fetchStatus } from '../../actions/statuses';
import MissingIndicator from '../../components/missing_indicator';
import DetailedStatus from './components/detailed_status';
Expand Down Expand Up @@ -63,39 +64,58 @@ const messages = defineMessages({
const makeMapStateToProps = () => {
const getStatus = makeGetStatus();

const mapStateToProps = (state, props) => {
const status = getStatus(state, { id: props.params.statusId });
const getAncestorsIds = createSelector([
(_, { id }) => id,
state => state.getIn(['contexts', 'inReplyTos']),
], (statusId, inReplyTos) => {
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();
descendantsIds = descendantsIds.withMutations(mutable => {
const ids = [statusId];

if (status) {
ancestorsIds = ancestorsIds.withMutations(mutable => {
let id = status.get('in_reply_to_id');
while (ids.length > 0) {
let id = ids.shift();
const replies = contextReplies.get(id);

while (id) {
mutable.unshift(id);
id = state.getIn(['contexts', 'inReplyTos', id]);
if (statusId !== id) {
mutable.push(id);
}
});

descendantsIds = descendantsIds.withMutations(mutable => {
const ids = [status.get('id')];
if (replies) {
replies.reverse().forEach(reply => {
ids.unshift(reply);
});
}
}
});

while (ids.length > 0) {
let id = ids.shift();
const replies = state.getIn(['contexts', 'replies', id]);
return descendantsIds;
});

if (status.get('id') !== id) {
mutable.push(id);
}
const mapStateToProps = (state, props) => {
const status = getStatus(state, { id: props.params.statusId });
let ancestorsIds = Immutable.List();
let descendantsIds = Immutable.List();

if (replies) {
replies.reverse().forEach(reply => {
ids.unshift(reply);
});
}
}
});
if (status) {
ancestorsIds = getAncestorsIds(state, { id: status.get('in_reply_to_id') });
descendantsIds = getDescendantsIds(state, { id: status.get('id') });
}

return {
Expand Down

0 comments on commit d371a7b

Please sign in to comment.