Skip to content

Commit

Permalink
Adding a few promise.all concurrent fetches to FetchInitialData. #2234 (
Browse files Browse the repository at this point in the history
  • Loading branch information
dessalines authored Dec 5, 2023
1 parent f245d2b commit fc23471
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
24 changes: 17 additions & 7 deletions src/shared/components/community/community.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,10 @@ export class Community extends Component<

const sort = getSortTypeFromQuery(urlSort);

let postsResponse: RequestState<GetPostsResponse> = EMPTY_REQUEST;
let commentsResponse: RequestState<GetCommentsResponse> = EMPTY_REQUEST;
let postsFetch: Promise<RequestState<GetPostsResponse>> =
Promise.resolve(EMPTY_REQUEST);
let commentsFetch: Promise<RequestState<GetCommentsResponse>> =
Promise.resolve(EMPTY_REQUEST);

if (dataType === DataType.Post) {
const getPostsForm: GetPosts = {
Expand All @@ -266,7 +268,7 @@ export class Community extends Component<
saved_only: false,
};

postsResponse = await client.getPosts(getPostsForm);
postsFetch = client.getPosts(getPostsForm);
} else {
const getCommentsForm: GetComments = {
community_name: communityName,
Expand All @@ -276,13 +278,21 @@ export class Community extends Component<
saved_only: false,
};

commentsResponse = await client.getComments(getCommentsForm);
commentsFetch = client.getComments(getCommentsForm);
}

const communityFetch = client.getCommunity(communityForm);

const [communityRes, commentsRes, postsRes] = await Promise.all([
communityFetch,
commentsFetch,
postsFetch,
]);

return {
communityRes: await client.getCommunity(communityForm),
commentsRes: commentsResponse,
postsRes: postsResponse,
communityRes,
commentsRes,
postsRes,
};
}

Expand Down
24 changes: 17 additions & 7 deletions src/shared/components/home/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,10 @@ export class Home extends Component<any, HomeState> {
site.site_view.local_site.default_post_listing_type;
const sort = getSortTypeFromQuery(urlSort, site.my_user);

let postsRes: RequestState<GetPostsResponse> = EMPTY_REQUEST;
let commentsRes: RequestState<GetCommentsResponse> = EMPTY_REQUEST;
let postsFetch: Promise<RequestState<GetPostsResponse>> =
Promise.resolve(EMPTY_REQUEST);
let commentsFetch: Promise<RequestState<GetCommentsResponse>> =
Promise.resolve(EMPTY_REQUEST);

if (dataType === DataType.Post) {
const getPostsForm: GetPosts = {
Expand All @@ -335,7 +337,7 @@ export class Home extends Component<any, HomeState> {
saved_only: false,
};

postsRes = await client.getPosts(getPostsForm);
postsFetch = client.getPosts(getPostsForm);
} else {
const getCommentsForm: GetComments = {
limit: fetchLimit,
Expand All @@ -344,7 +346,7 @@ export class Home extends Component<any, HomeState> {
saved_only: false,
};

commentsRes = await client.getComments(getCommentsForm);
commentsFetch = client.getComments(getCommentsForm);
}

const trendingCommunitiesForm: ListCommunities = {
Expand All @@ -353,10 +355,18 @@ export class Home extends Component<any, HomeState> {
limit: trendingFetchLimit,
};

const trendingCommunitiesFetch = client.listCommunities(
trendingCommunitiesForm,
);

const [postsRes, commentsRes, trendingCommunitiesRes] = await Promise.all([
postsFetch,
commentsFetch,
trendingCommunitiesFetch,
]);

return {
trendingCommunitiesRes: await client.listCommunities(
trendingCommunitiesForm,
),
trendingCommunitiesRes,
commentsRes,
postsRes,
};
Expand Down
15 changes: 10 additions & 5 deletions src/shared/components/post/post-listing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,21 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
constructor(props: any, context: any) {
super(props, context);

if (UserService.Instance.myUserInfo) {
this.state.imageExpanded =
UserService.Instance.myUserInfo.local_user_view.local_user.auto_expand;
}

this.handleEditPost = this.handleEditPost.bind(this);
this.handleEditCancel = this.handleEditCancel.bind(this);
this.handleReportSubmit = this.handleReportSubmit.bind(this);
}

componentDidMount(): void {
if (UserService.Instance.myUserInfo) {
this.setState({
imageExpanded:
UserService.Instance.myUserInfo.local_user_view.local_user
.auto_expand,
});
}
}

componentWillReceiveProps(nextProps: PostListingProps) {
if (this.props !== nextProps) {
this.setState({
Expand Down
9 changes: 7 additions & 2 deletions src/shared/components/post/post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,14 @@ export class Post extends Component<any, PostState> {
commentsForm.parent_id = id;
}

const [postRes, commentsRes] = await Promise.all([
client.getPost(postForm),
client.getComments(commentsForm),
]);

return {
postRes: await client.getPost(postForm),
commentsRes: await client.getComments(commentsForm),
postRes,
commentsRes,
};
}

Expand Down

0 comments on commit fc23471

Please sign in to comment.