Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Getposts optimistic (#208)
Browse files Browse the repository at this point in the history
* Make get posts actions retry if failed

* Add tests for post withRetry methods

* Reset attempts when request happens

* Change retries check
  • Loading branch information
csduarte authored and hmhealey committed Jul 27, 2017
1 parent f5b6233 commit e7a0ae8
Show file tree
Hide file tree
Showing 8 changed files with 576 additions and 13 deletions.
6 changes: 6 additions & 0 deletions src/action_types/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export default keyMirror({
OPEN_GRAPH_SUCCESS: null,
OPEN_GRAPH_FAILURE: null,

GET_POST_THREAD_WITH_RETRY_ATTEMPT: null,
GET_POSTS_WITH_RETRY_ATTEMPT: null,
GET_POSTS_SINCE_WITH_RETRY_ATTEMPT: null,
GET_POSTS_BEFORE_WITH_RETRY_ATTEMPT: null,
GET_POSTS_AFTER_WITH_RETRY_ATTEMPT: null,

RECEIVED_POST: null,
RECEIVED_POSTS: null,
RECEIVED_FOCUSED_POST: null,
Expand Down
239 changes: 239 additions & 0 deletions src/actions/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,54 @@ export function getPostThread(postId) {
};
}

export function getPostThreadWithRetry(postId) {
return async (dispatch, getState) => {
dispatch({
type: PostTypes.GET_POST_THREAD_REQUEST
});

dispatch({
type: PostTypes.GET_POST_THREAD_WITH_RETRY_ATTEMPT,
data: {},
meta: {
offline: {
effect: () => Client4.getPostThread(postId),
commit: (success, payload) => {
const {posts} = payload;
const post = posts[postId];
getProfilesAndStatusesForPosts(posts, dispatch, getState);

dispatch(batchActions([
{
type: PostTypes.RECEIVED_POSTS,
data: payload,
channelId: post.channel_id
},
{
type: PostTypes.GET_POST_THREAD_SUCCESS
}
]), getState);
},
maxRetry: 2,
cancel: true,
onRetryScheduled: () => {
dispatch({
type: PostTypes.GET_POST_THREAD_WITH_RETRY_ATTEMPT
});
},
rollback: (success, error) => {
forceLogoutIfNecessary(error, dispatch);
dispatch(batchActions([
{type: PostTypes.GET_POST_THREAD_FAILURE, error},
logError(error)(dispatch)
]), getState);
}
}
}
});
};
}

export function getPosts(channelId, page = 0, perPage = Posts.POST_CHUNK_SIZE) {
return async (dispatch, getState) => {
dispatch({type: PostTypes.GET_POSTS_REQUEST}, getState);
Expand Down Expand Up @@ -389,6 +437,53 @@ export function getPosts(channelId, page = 0, perPage = Posts.POST_CHUNK_SIZE) {
};
}

export function getPostsWithRetry(channelId, page = 0, perPage = Posts.POST_CHUNK_SIZE) {
return async (dispatch, getState) => {
dispatch({
type: PostTypes.GET_POSTS_REQUEST
});

dispatch({
type: PostTypes.GET_POSTS_WITH_RETRY_ATTEMPT,
data: {},
meta: {
offline: {
effect: () => Client4.getPosts(channelId, page, perPage),
commit: (success, payload) => {
const {posts} = payload;
getProfilesAndStatusesForPosts(posts, dispatch, getState);

dispatch(batchActions([
{
type: PostTypes.RECEIVED_POSTS,
data: payload,
channelId
},
{
type: PostTypes.GET_POSTS_SUCCESS
}
]), getState);
},
maxRetry: 2,
cancel: true,
onRetryScheduled: () => {
dispatch({
type: PostTypes.GET_POSTS_WITH_RETRY_ATTEMPT
});
},
rollback: (success, error) => {
forceLogoutIfNecessary(error, dispatch);
dispatch(batchActions([
{type: PostTypes.GET_POSTS_FAILURE, error},
logError(error)(dispatch)
]), getState);
}
}
}
});
};
}

export function getPostsSince(channelId, since) {
return async (dispatch, getState) => {
dispatch({type: PostTypes.GET_POSTS_SINCE_REQUEST}, getState);
Expand Down Expand Up @@ -421,6 +516,51 @@ export function getPostsSince(channelId, since) {
};
}

export function getPostsSinceWithRetry(channelId, since) {
return async (dispatch, getState) => {
dispatch({type: PostTypes.GET_POSTS_SINCE_REQUEST}, getState);

dispatch({
type: PostTypes.GET_POSTS_SINCE_WITH_RETRY_ATTEMPT,
data: {},
meta: {
offline: {
effect: () => Client4.getPostsSince(channelId, since),
commit: (success, payload) => {
const {posts} = payload;
getProfilesAndStatusesForPosts(posts, dispatch, getState);

dispatch(batchActions([
{
type: PostTypes.RECEIVED_POSTS,
data: payload,
channelId
},
{
type: PostTypes.GET_POSTS_SINCE_SUCCESS
}
]), getState);
},
maxRetry: 2,
cancel: true,
onRetryScheduled: () => {
dispatch({
type: PostTypes.GET_POSTS_SINCE_WITH_RETRY_ATTEMPT
});
},
rollback: (success, error) => {
forceLogoutIfNecessary(error, dispatch);
dispatch(batchActions([
{type: PostTypes.GET_POSTS_SINCE_FAILURE, error},
logError(error)(dispatch)
]), getState);
}
}
}
});
};
}

export function getPostsBefore(channelId, postId, page = 0, perPage = Posts.POST_CHUNK_SIZE) {
return async (dispatch, getState) => {
dispatch({type: PostTypes.GET_POSTS_BEFORE_REQUEST}, getState);
Expand Down Expand Up @@ -453,6 +593,53 @@ export function getPostsBefore(channelId, postId, page = 0, perPage = Posts.POST
};
}

export function getPostsBeforeWithRetry(channelId, postId, page = 0, perPage = Posts.POST_CHUNK_SIZE) {
return async (dispatch, getState) => {
dispatch({
type: PostTypes.GET_POSTS_BEFORE_REQUEST
});

dispatch({
type: PostTypes.GET_POSTS_BEFORE_WITH_RETRY_ATTEMPT,
data: {},
meta: {
offline: {
effect: () => Client4.getPostsBefore(channelId, postId, page, perPage),
commit: (success, payload) => {
const {posts} = payload;
getProfilesAndStatusesForPosts(posts, dispatch, getState);

dispatch(batchActions([
{
type: PostTypes.RECEIVED_POSTS,
data: payload,
channelId
},
{
type: PostTypes.GET_POSTS_BEFORE_SUCCESS
}
]), getState);
},
maxRetry: 2,
cancel: true,
onRetryScheduled: () => {
dispatch({
type: PostTypes.GET_POSTS_BEFORE_WITH_RETRY_ATTEMPT
});
},
rollback: (success, error) => {
forceLogoutIfNecessary(error, dispatch);
dispatch(batchActions([
{type: PostTypes.GET_POSTS_BEFORE_FAILURE, error},
logError(error)(dispatch)
]), getState);
}
}
}
});
};
}

export function getPostsAfter(channelId, postId, page = 0, perPage = Posts.POST_CHUNK_SIZE) {
return async (dispatch, getState) => {
dispatch({type: PostTypes.GET_POSTS_AFTER_REQUEST}, getState);
Expand Down Expand Up @@ -485,6 +672,53 @@ export function getPostsAfter(channelId, postId, page = 0, perPage = Posts.POST_
};
}

export function getPostsAfterWithRetry(channelId, postId, page = 0, perPage = Posts.POST_CHUNK_SIZE) {
return async (dispatch, getState) => {
dispatch({
type: PostTypes.GET_POSTS_AFTER_REQUEST
});

dispatch({
type: PostTypes.GET_POSTS_AFTER_WITH_RETRY_ATTEMPT,
data: {},
meta: {
offline: {
effect: () => Client4.getPostsAfter(channelId, postId, page, perPage),
commit: (success, payload) => {
const {posts} = payload;
getProfilesAndStatusesForPosts(posts, dispatch, getState);

dispatch(batchActions([
{
type: PostTypes.RECEIVED_POSTS,
data: payload,
channelId
},
{
type: PostTypes.GET_POSTS_AFTER_SUCCESS
}
]), getState);
},
maxRetry: 2,
cancel: true,
onRetryScheduled: () => {
dispatch({
type: PostTypes.GET_POSTS_AFTER_WITH_RETRY_ATTEMPT
});
},
rollback: (success, error) => {
forceLogoutIfNecessary(error, dispatch);
dispatch(batchActions([
{type: PostTypes.GET_POSTS_AFTER_FAILURE, error},
logError(error)(dispatch)
]), getState);
}
}
}
});
};
}

// Note that getProfilesAndStatusesForPosts can take either an array of posts or a map of ids to posts
export async function getProfilesAndStatusesForPosts(posts, dispatch, getState) {
if (!posts) {
Expand Down Expand Up @@ -641,9 +875,14 @@ export default {
deletePost,
removePost,
getPostThread,
getPostThreadWithRetry,
getPosts,
getPostsWithRetry,
getPostsSince,
getPostsSinceWithRetry,
getPostsBefore,
getPostsBeforeWithRetry,
getPostsAfter,
getPostsAfterWithRetry,
selectPost
};
Loading

0 comments on commit e7a0ae8

Please sign in to comment.