Adding a few promise.all concurrent fetches to FetchInitialData. #2234 (#2270)

This commit is contained in:
Dessalines 2023-12-05 14:22:16 -05:00 committed by GitHub
parent f245d2b517
commit fc234716b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 21 deletions

View file

@ -253,8 +253,10 @@ export class Community extends Component<
const sort = getSortTypeFromQuery(urlSort); const sort = getSortTypeFromQuery(urlSort);
let postsResponse: RequestState<GetPostsResponse> = EMPTY_REQUEST; let postsFetch: Promise<RequestState<GetPostsResponse>> =
let commentsResponse: RequestState<GetCommentsResponse> = EMPTY_REQUEST; Promise.resolve(EMPTY_REQUEST);
let commentsFetch: Promise<RequestState<GetCommentsResponse>> =
Promise.resolve(EMPTY_REQUEST);
if (dataType === DataType.Post) { if (dataType === DataType.Post) {
const getPostsForm: GetPosts = { const getPostsForm: GetPosts = {
@ -266,7 +268,7 @@ export class Community extends Component<
saved_only: false, saved_only: false,
}; };
postsResponse = await client.getPosts(getPostsForm); postsFetch = client.getPosts(getPostsForm);
} else { } else {
const getCommentsForm: GetComments = { const getCommentsForm: GetComments = {
community_name: communityName, community_name: communityName,
@ -276,13 +278,21 @@ export class Community extends Component<
saved_only: false, 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 { return {
communityRes: await client.getCommunity(communityForm), communityRes,
commentsRes: commentsResponse, commentsRes,
postsRes: postsResponse, postsRes,
}; };
} }

View file

@ -323,8 +323,10 @@ export class Home extends Component<any, HomeState> {
site.site_view.local_site.default_post_listing_type; site.site_view.local_site.default_post_listing_type;
const sort = getSortTypeFromQuery(urlSort, site.my_user); const sort = getSortTypeFromQuery(urlSort, site.my_user);
let postsRes: RequestState<GetPostsResponse> = EMPTY_REQUEST; let postsFetch: Promise<RequestState<GetPostsResponse>> =
let commentsRes: RequestState<GetCommentsResponse> = EMPTY_REQUEST; Promise.resolve(EMPTY_REQUEST);
let commentsFetch: Promise<RequestState<GetCommentsResponse>> =
Promise.resolve(EMPTY_REQUEST);
if (dataType === DataType.Post) { if (dataType === DataType.Post) {
const getPostsForm: GetPosts = { const getPostsForm: GetPosts = {
@ -335,7 +337,7 @@ export class Home extends Component<any, HomeState> {
saved_only: false, saved_only: false,
}; };
postsRes = await client.getPosts(getPostsForm); postsFetch = client.getPosts(getPostsForm);
} else { } else {
const getCommentsForm: GetComments = { const getCommentsForm: GetComments = {
limit: fetchLimit, limit: fetchLimit,
@ -344,7 +346,7 @@ export class Home extends Component<any, HomeState> {
saved_only: false, saved_only: false,
}; };
commentsRes = await client.getComments(getCommentsForm); commentsFetch = client.getComments(getCommentsForm);
} }
const trendingCommunitiesForm: ListCommunities = { const trendingCommunitiesForm: ListCommunities = {
@ -353,10 +355,18 @@ export class Home extends Component<any, HomeState> {
limit: trendingFetchLimit, limit: trendingFetchLimit,
}; };
const trendingCommunitiesFetch = client.listCommunities(
trendingCommunitiesForm,
);
const [postsRes, commentsRes, trendingCommunitiesRes] = await Promise.all([
postsFetch,
commentsFetch,
trendingCommunitiesFetch,
]);
return { return {
trendingCommunitiesRes: await client.listCommunities( trendingCommunitiesRes,
trendingCommunitiesForm,
),
commentsRes, commentsRes,
postsRes, postsRes,
}; };

View file

@ -165,16 +165,21 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
constructor(props: any, context: any) { constructor(props: any, context: any) {
super(props, context); 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.handleEditPost = this.handleEditPost.bind(this);
this.handleEditCancel = this.handleEditCancel.bind(this); this.handleEditCancel = this.handleEditCancel.bind(this);
this.handleReportSubmit = this.handleReportSubmit.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) { componentWillReceiveProps(nextProps: PostListingProps) {
if (this.props !== nextProps) { if (this.props !== nextProps) {
this.setState({ this.setState({

View file

@ -263,9 +263,14 @@ export class Post extends Component<any, PostState> {
commentsForm.parent_id = id; commentsForm.parent_id = id;
} }
const [postRes, commentsRes] = await Promise.all([
client.getPost(postForm),
client.getComments(commentsForm),
]);
return { return {
postRes: await client.getPost(postForm), postRes,
commentsRes: await client.getComments(commentsForm), commentsRes,
}; };
} }